Home

Awesome

ClickHouse Java Client & JDBC Driver

GitHub release (latest SemVer including pre-releases) Build Status(https://github.com/ClickHouse/clickhouse-jdbc/workflows/Build/badge.svg) Coverage

Java client and JDBC driver for ClickHouse. Java client is async, lightweight, and low-overhead library for ClickHouse; while JDBC driver is built on top of the Java client with more dependencies and extensions for JDBC-compliance.

Java 8 or higher is required in order to use Java client(clickhouse-client) and/or JDBC driver(clickhouse-jdbc). In addition, starting from 0.3.2, JDBC driver only works with ClickHouse 20.7 or above, so please consider to either downgrade the driver to 0.3.1-patch or upgrade server to one of active releases.


:exclamation: IMPORTANT

Maven groupId ru.yandex.clickhouse and legacy JDBC driver ru.yandex.clickhouse.ClickHouseDriver are deprecated.

Please use new groupId com.clickhouse and driver com.clickhouse.jdbc.ClickHouseDriver instead. It's highly recommended to upgrade to 0.3.2+ and start to integrate the new JDBC driver for improved performance and stability.

image Note: in general, the new driver(v0.3.2) is a few times faster with less memory usage. More information can be found at here.


Features

CategoryFeatureSupportedRemark
ProtocolHTTP:white_check_mark:recommended, defaults to java.net.HttpURLConnection and can be changed to java.net.http.HttpClient(faster but less stable)
gRPC:white_check_mark:experimental, still missing some features like LZ4 compression
TCP/Native:x:will be available in 0.3.3
CompatibilityServer < 20.7:x:use 0.3.1-patch(or 0.2.6 if you're stuck with JDK 7)
Server >= 20.7:white_check_mark:use 0.3.2 or above. All active releases are supported.
Data TypeAggregatedFunction:x:limited to groupBitmap
Array(*):white_check_mark:
Date*:white_check_mark:
DateTime*:white_check_mark:
Decimal*:white_check_mark:SET output_format_decimal_trailing_zeros=1 in 21.9+ for consistency
Enum*:white_check_mark:can be treated as both string and integer
Geo Types:white_check_mark:
Int*, UInt*:white_check_mark:UInt64 is mapped to long
IPv*:white_check_mark:
*String:white_check_mark:
Map(*):white_check_mark:
Nested(*):white_check_mark:
Tuple(*):white_check_mark:
UUID:white_check_mark:
FormatRowBinary:white_check_mark:RowBinaryWithNamesAndTypes for query and RowBinary for insertion
TabSeparated:white_check_mark:Does not support as many data types as RowBinary

Configuration

Examples

Java Client

<dependency>
    <groupId>com.clickhouse</groupId>
    <!-- or clickhouse-grpc-client if you prefer gRPC -->
    <artifactId>clickhouse-http-client</artifactId>
    <version>0.3.2-patch7</version>
</dependency>
// only HTTP and gRPC are supported at this point
ClickHouseProtocol preferredProtocol = ClickHouseProtocol.HTTP;
// you'll have to parse response manually if use different format
ClickHouseFormat preferredFormat = ClickHouseFormat.RowBinaryWithNamesAndTypes;

// connect to localhost, use default port of the preferred protocol
ClickHouseNode server = ClickHouseNode.builder().port(preferredProtocol).build();

try (ClickHouseClient client = ClickHouseClient.newInstance(preferredProtocol);
    ClickHouseResponse response = client.connect(server)
        .format(preferredFormat)
        .query("select * from numbers(:limit)")
        .params(1000).execute().get()) {
    // or resp.stream() if you prefer stream API
    for (ClickHouseRecord record : response.records()) {
        int num = record.getValue(0).asInteger();
        String str = record.getValue(0).asString();
    }

    ClickHouseResponseSummary summary = response.getSummary();
    long totalRows = summary.getTotalRowsToRead();
}

JDBC Driver

<dependency>
    <!-- will stop using ru.yandex.clickhouse starting from 0.4.0  -->
    <groupId>com.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>0.3.2-patch7</version>
    <!-- below is only needed when all you want is a shaded jar -->
    <classifier>http</classifier>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>
String url = "jdbc:ch://localhost/test";
Properties properties = new Properties();
// optionally set connection properties
properties.setProperty("client_name", "Agent #1");
...

ClickHouseDataSource dataSource = new ClickHouseDataSource(url, properties);
try (Connection conn = dataSource.getConnection();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from mytable")) {
    ...
}

Build with Maven

Use mvn clean verify to compile, test and generate shaded packages if you're using JDK 8. To create a multi-release jar file(see JEP-238), please use JDK 11 or above and follow instructions below:

Benchmark

To benchmark JDBC drivers:

cd clickhouse-benchmark
mvn -Drelease clean package
# single thread mode
java -DdbHost=localhost -jar target/benchmarks.jar -t 1 -p client=clickhouse-http-jdbc1 -p connection=reuse -p statement=prepared Query.selectInt8

It's time consuming to run all benchmarks against all drivers using different parameters for comparison. If you just need some numbers to understand performance, please refer to table below and some more details like CPU and memory usage mentioned at here(still have plenty of room to improve according to ranking at here).

Testing

By default, docker container will be created automatically during integration test. You can pass system property like -DclickhouseVersion=21.8 to specify version of ClickHouse.

In the case you prefer to test against an existing server, please follow instructions below: