1 |
pom.xml |
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.1-patch</version>
<classifier>shaded</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
|
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.2-patch11</version>
<classifier>all</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
|
2 |
driver class |
ru.yandex.clickhouse.ClickHouseDriver |
com.clickhouse.jdbc.ClickHouseDriver |
3 |
connection string |
jdbc:clickhouse://[user[:password]@]host:port[/database][?parameters]
|
jdbc:(ch|clickhouse)[:protocol]://endpoint[,endpoint][/database][?parameters][#tags]
endpoint: [protocol://]host[:port][/database][?parameters][#tags]
protocol: (grpc|grpcs|http|https|tcp|tcps)
|
4 |
custom settings |
String jdbcUrl = "jdbc:clickhouse://localhost:8123/default?socket_timeout=6000000"
// custom server settings
+ "&max_bytes_before_external_group_by=16000000000"
+ "&optimize_aggregation_in_order=0"
+ "&join_default_strictness=ANY"
+ "&join_algorithm=auto"
+ "&max_memory_usage=20000000000";
|
String jdbcUrl = "jdbc:clickhouse://localhost/default?socket_timeout=6000000"
// or properties.setProperty("custom_settings", "a=1,b=2,c=3")
+ "&custom_settings="
// url encoded settings separated by comma
+ "max_bytes_before_external_group_by%3D16000000000%2C"
+ "optimize_aggregation_in_order%3D0%2C"
+ "join_default_strictness%3DANY%2C"
+ "join_algorithm%3Dauto%2C"
+ "max_memory_usage%3D20000000000";
|
5 |
load balancing |
String connString = "jdbc:clickhouse://server1:8123,server2:8123,server3:8123/database";
BalancedClickhouseDataSource balancedDs = new BalancedClickhouseDataSource(
connString).scheduleActualization(5000, TimeUnit.MILLISECONDS);
ClickHouseConnection conn = balancedDs.getConnection("default", "");
|
String connString = "jdbc:ch://server1,server2,server3/database"
+ "?load_balancing_policy=random&health_check_interval=5000&failover=2";
ClickHouseDataSource ds = new ClickHouseDataSource(connString);
ClickHouseConnection conn = ds.getConnection("default", "");
|
6 |
DateTime |
try (PreparedStatement ps = conn.preparedStatement("insert into mytable(start_datetime, string_value) values(?,?)") {
ps.setObject(1, LocalDateTime.now());
ps.setString(2, "value");
ps.executeUpdate();
}
|
try (PreparedStatement ps = conn.preparedStatement("insert into mytable(start_datetime, string_value) values(?,?)") {
// resolution of DateTime32 or DateTime without scale is 1 second
ps.setObject(1, LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
ps.setString(2, "value");
ps.executeUpdate();
}
|
7 |
extended API |
ClickHouseStatement sth = connection.createStatement();
sth.write().send("INSERT INTO test.writer", new ClickHouseStreamCallback() {
@Override
public void writeTo(ClickHouseRowBinaryStream stream) throws IOException {
for (int i = 0; i < 10; i++) {
stream.writeInt32(i);
stream.writeString("Name " + i);
}
}
}, ClickHouseFormat.RowBinary); // RowBinary or Native are supported
|
// 0.3.2
Statement sth = connection.createStatement();
sth.unwrap(ClickHouseRequest.class).write().table("test.writer")
.format(ClickHouseFormat.RowBinary).data(out -> {
for (int i = 0; i < 10; i++) {
// write data into the piped stream in current thread
BinaryStreamUtils.writeInt32(out, i);
BinaryStreamUtils.writeString(out, "Name " + i);
}
}).sendAndWait();
// since 0.4
PreparedStatement ps = connection.preparedStatement("insert into test.writer format RowBinary");
ps.setObject(new ClickHouseWriter() {
@Override
public void write(ClickHouseOutputStream out) throws IOException {
for (int i = 0; i < 10; i++) {
// write data into the piped stream in current thread
BinaryStreamUtils.writeInt32(out, i);
BinaryStreamUtils.writeString(out, "Name " + i);
}
}
});
// ClickHouseWriter will be executed in a separate thread
ps.executeUpdate();
|