Skip to content

Instantly share code, notes, and snippets.

View jerolba's full-sized avatar

Jeronimo López jerolba

View GitHub Profile
com.mysql.jdbc.Connection unwrapped = connection.unwrap(com.mysql.jdbc.Connection.class);
unwrapped.setAllowLoadLocalInfile(true);
String LOADDATA = "LOAD DATA LOCAL INFILE '' INTO TABLE `bike_trip` "
+ "CHARACTER SET UTF8 FIELDS TERMINATED BY '\t' ENCLOSED BY '' "
+ "ESCAPED BY '\\\\' LINES TERMINATED BY '\n' STARTING BY '' "
+ "(tripduration, starttime, stoptime, start_station_id, start_station_name, "
+ "start_station_latitude, start_station_longitude, end_station_id, "
+ "end_station_name, end_station_latitude, end_station_longitude, "
+ "bike_id, user_type, birth_year, gender)";
int cont = 0;
if (cont % batchSize == 0) {
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
Statement statement = (Statement) unwrapped.createStatement();
statement.setLocalInfileInputStream(is);
statement.execute(LOADDATA);
sb.setLength(0);
}
}
PgConnection unwrapped = connection.unwrap(PgConnection.class);
CopyManager copyManager = unwrapped.getCopyAPI();
String COPY = "COPY bike_trip (tripduration, starttime, stoptime,"
+ "start_station_id, start_station_name, start_station_latitude, "
+ "start_station_longitude, end_station_id, end_station_name,"
+ "end_station_latitude, end_station_longitude, bike_id,"
+ "user_type, birth_year, gender)"
+ " FROM STDIN WITH (FORMAT TEXT, ENCODING 'UTF-8', DELIMITER '\t',"
+ " HEADER false)";
int cont = 0;
if (cont % batchSize == 0) {
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
copyManager.copyIn(COPY, is);
sb.setLength(0);
}
}
@jerolba
jerolba / HashMapPersonWithoutHashCode.java
Last active June 3, 2019 18:21
HashMap without hashCode
public class Person {
private Integer id;
private String name;
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
}
@jerolba
jerolba / Person.java
Created February 7, 2019 20:02
Person class with hashCode and equals
public class Person {
private Integer id;
private String name;
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
@jerolba
jerolba / Arrays.java
Created February 7, 2019 20:04
Arrays hashCode method
public static int hashCode(Object a[]) {
if (a == null) return 0;
int result = 1;
for (Object element : a)
result = 31 * result + (element == null ? 0 : element.hashCode());
return result;
}
@jerolba
jerolba / ComposedKeyObject.java
Created February 7, 2019 20:07
Object with composed key
public class MyObject {
private MyObjectKey key;
private String someAttribute;
public MyObject(int firstKey, int secondKey, String someAttribute) {
this.key = new MyObjectKey(firstKey, secondKey);
this.someAttribute = someAttribute;
}