ClickHouse Java Clients & JDBC Driver
Table of Contents
This is official Java Client and JDBC for ClickHouse Database (https://github.com/ClickHouse/Clickhouse). Java Client is the core component and provides API to interact with the database. In 2023 this component and its API was refactored into a new component client-v2
. Both version are available but older one will be deprecated soon. However it will receive security and critical bug fixes. New client-v2
has stable API and we are working on performance and feature parity to make it a production ready.
JDBC driver component is an implementation of JDBC API. It uses Java Client API to interact with the database server.
Benefits of using Client-V2:
Old client still be used when:
Component | Version | Comment |
---|---|---|
Clickhouse CLI Client (Java) | 0.7.0 | Please use clickhouse-client (see https://clickhouse.com/docs/en/interfaces/cli#clickhouse-client) |
ClickHouse GRPC Client | 0.7.0 | Please use the ClickHouse http client instead. GRPC protos still available https://github.com/ClickHouse/ClickHouse/tree/master/src/Server/grpc_protos |
Releases: Maven Central (web site https://mvnrepository.com/artifact/com.clickhouse)
Nightly Builds: https://s01.oss.sonatype.org/content/repositories/snapshots/com/clickhouse/
Component | Maven Central Link | Javadoc Link |
---|---|---|
ClickHouse Java Client V2 |
ClickHouse Version | Client Version | Comment |
---|---|---|
Server >= 23.0 | 0.6.2 |
Minimal client setup:
String endpoint = "https://<db-instance hostname>:8443/"
Client client = new Client.Builder()
.addEndpoint(endpoint)
.setUsername(user)
.setPassword(password)
.setDefaultDatabase(database)
.build();
Insert POJOs example:
client.register(
ArticleViewEvent.class, // your DTO class
client.getTableSchema(TABLE_NAME)); // corresponding table
List<ArticleViewEvents> events = // load data
try (InsertResponse response = client.insert(TABLE_NAME, events).get(1, TimeUnit.SECONDS)) {
// process results
}
Query results reader example:
// Default format is RowBinaryWithNamesAndTypesFormatReader so reader have all information about columns
try (QueryResponse response = client.query(sql).get(3, TimeUnit.SECONDS);) {
// Create a reader to access the data in a convenient way
ClickHouseBinaryFormatReader reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(),
response.getSettings());
while (reader.hasNext()) {
reader.next(); // Read the next record from stream and parse it
double id = reader.getDouble("id");
String title = reader.getString("title");
String url = reader.getString("url");
// result processing
}
}
Query result as list of object example:
// Data is read completely and returned as list of objects.
client.queryAll(sql).forEach(row -> {
double id = row.getDouble("id");
String title = row.getString("title");
String url = row.getString("url");
// result processing
});
Connecting to the ClickHouse Cloud instance or DB server having not a self-signed certificate:
Client client = new Client.Builder()
.addEndpoint("https://" + dbHost + ":8443")
.setUsername("default")
.setPassword("")
.build(),
Connecting to a database instance with self-signed certificate:
Client client = new Client.Builder()
.addEndpoint("https://" + dbHost + ":8443")
.setUsername("default")
.setPassword("")
.setRootCertificate("localhost.crt") // path to the CA certificate
//.setClientKey("user.key") // user private key
//.setClientCertificate("user.crt") // user public certificate
.build(),
Component | Maven Central Link | Javadoc Link |
---|---|---|
ClickHouse Java Unified Client | ||
ClickHouse Java HTTP Client | ||
ClickHouse JDBC Driver | ||
ClickHouse R2DBC Driver | ||
ClickHouse gRPC Driver |
See JDBC examples
Java Client V1 Docs :: ClickHouse website
JDBC Docs :: ClickHouse website.
Please see our contributing guide.