Skip to content

Instantly share code, notes, and snippets.

View ufuk's full-sized avatar
🤔

Ufuk Uzun ufuk

🤔
View GitHub Profile
@ufuk
ufuk / postgresql-export-tables-to-s3-as-csv-file.sh
Last active July 10, 2023 04:04
BASH script to export some tables from the PostgreSQL database to S3 as CSV files.
#!/bin/bash
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_DEFAULT_REGION=...
PG_HOST=...
PG_USER=...
PG_PASS='...'
PG_DB=...
@ufuk
ufuk / posgresql-lightspeed-alter-table.sql
Last active November 5, 2018 07:59
Increase performance of alter tables or any other expensive operations by increasing WORK_MEM parameter for current session in PostgreSQL
BEGIN;
LOCK TABLE ... IN SHARE MODE;
SET LOCAL WORK_MEM = '256MB';
ALTER TABLE ...;
COMMIT;
@ufuk
ufuk / TurkishNumberUtils.java
Last active November 13, 2020 07:55
Util for converting whole numbers to Turkish words
public final class TurkishNumberUtils {
private static final String SPACE = " ";
private static final String EMPTY = "";
private static final String[] PERIOD_NAMES = {EMPTY, "bin", "milyon", "milyar", "trilyon", "katrilyon", "kentilyon"};
private static final String[] UNITS_TEXTS = {EMPTY, "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz"};
@ufuk
ufuk / learn-which-process-has-allocated-the-tcp-port.sh
Last active March 23, 2018 11:30
Learn which process has allocated the TCP port (for example "8080")
lsof -i tcp:8080
# Example output:
# COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
# java 86935 ...
@ufuk
ufuk / postgres-create-drop-index-without-locking.sql
Last active March 10, 2018 20:43 — forked from okanmenevseoglu/postgres-create-drop-index-online.sql
Create/Drop a database index without locking the table's selects, inserts, etc. on PostgreSQL
CREATE INDEX CONCURRENTLY your_index_name on your_table_name (your_column_name);
DROP INDEX CONCURRENTLY your_index_name;
@ufuk
ufuk / postgresql-cryptography-functions.sql
Last active January 29, 2018 11:29
Enable cryptography functions (pgcrypto extension) in PostgreSQL.
CREATE EXTENSION pgcrypto;
-- Examples
SELECT ENCODE(DIGEST('password', 'md5'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha1'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha224'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha256'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha384'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha512'), 'base64');
@ufuk
ufuk / ScaleAndTranslateIntoRange.java
Created July 25, 2017 10:51
Scales and translates a value (x) into a new range [a, b].
/**
* Scales and translates a value (x) into a new range [a, b].
*
* Formula:
* f(x) = a + (b - a)(x - min)/(max - min)
*
* See also: https://stackoverflow.com/a/5295202
*/
public BigDecimal scaleAndTranslateIntoRange(BigDecimal value, BigDecimal actualMin, BigDecimal actualMax, BigDecimal desiredMin, BigDecimal desiredMax) {
return desiredMax.subtract(desiredMin).multiply(value.subtract(actualMin))
@ufuk
ufuk / postgresql-export-import-schema.sh
Last active June 11, 2018 12:50
Exporting and then importing full schema for PostgreSQL.
# export
PGPASSWORD="<PASSWORD>" pg_dump -U <USERNAME> -h <HOST> -d <DB_NAME> -n <SCHEMA_NAME> > export.sql
# import
PGPASSWORD="<PASSWORD>" psql -U <USERNAME> -h <HOST> -d <DB_NAME> -n <SCHEMA_NAME> -f export.sql
@ufuk
ufuk / SomeSpringBeanThatNeedsAResource.java
Last active July 11, 2017 07:50
Getting and using a file from classpath with Spring's @ Value annotation.
package ...;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import java.io.InputStream;
@Service
public class SomeSpringBeanThatNeedsAResource {
@ufuk
ufuk / concatenate-grep-and-split-by-delimeter.sh
Created July 10, 2017 09:08
Concatenate files, grep lines, split by a delimeter using awk and print some columns as sorted and uniqe. May be useful for parsing logs.
cat file1 file2 ... fileN | grep "<SEARCHING TEXT>" | awk -F'<DELIMETER>' '{print $1 $2 ... $n;}' | sort | uniq