Skip to main content

Java Client (V2)

Implementation of a new API. It uses Apache Http Client to communicate with ClickHouse server. We have selected this http client because it has many built-in features and has proven itself in old client implementation. We are planning to support other http client libraries.

Note: Client-V2 is currently in the phase of active development and we are still working on it.

Setup

<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>client-v2</artifactId>
<version>0.6.5</version>
</dependency>

Initialization

Client object is initialized by com.clickhouse.client.api.Client.Builder#build(). Each client has own context and no objects are shared between them. Builder has configuration method for convinient setup.

Example:

 Client client = new Client.Builder()
.addEndpoint("https://clickhouse-cloud-instance:8443/")
.setUsername(user)
.setPassword(password)
.build;

Client is AutoCloseable and should be closed when not needed anymore.

Configuration

All settings are defined by instance methods (a.k.a configuration methods) that make scope and context of each value clear. Major configuration parameters are defined in one scope (client or operation) and do not override each other. Handling configuration overriding across is a very hard task so we doing our best to keep it simple.

This section describes only client wide settings. Each operation may have own and will be listed in the their sections.

Insert API

MethodDescription
CompletableFuture<InsertResponse> insert(String tableName, InputStream data, ClickHouseFormat format, InsertSettings settings)Sends write request to database. Input data is read from the input stream.
CompletableFuture<InsertResponse> insert(String tableName, List<?> data, InsertSettings settings)Sends write request to database. List of objects is converted into a most effective format and then is sent to a server. Class of the list items should be registed up-front using register(Class, TableSchema) method.

There is no much difference in performance between raw data insert and POJO list insert. The last one helps to avoid coding complex serialization into RowBinary format - client will handle it for you.

// Important step (done once) - register class to pre-compile object serializer according to the table schema. 
client.register(ArticleViewEvent.class, client.getTableSchema(TABLE_NAME));


List<ArtivleViewEvent> events = loadBatch();

try (InsertResponse response = client.insert(TABLE_NAME, events).get()) {
// handle response, then it will be closed and connection that served request will be released.
}

If data is already in the format that ClickHouse accepts, then:

try (InputStream dataStream = getDataStream()) {
try (InsertResponse response = client.insert(TABLE_NAME, dataStream, ClickHouseFormat.JSONEachRow,
insertSettings).get(3, TimeUnit.SECONDS)) {

log.info("Insert finished: {} rows written", response.getMetrics().getMetric(ServerMetrics.NUM_ROWS_WRITTEN).getLong());
} catch (Exception e) {
log.error("Failed to write JSONEachRow data", e);
throw new RuntimeException(e);
}
}

Query API

MethodDescription
CompletableFuture<QueryResponse> query(String sqlQuery, QuerySettings settings)Sends sqlQuery as is. Response format is set by query settings. QueryResponse will hold a reference to the response stream what should be consumer by a reader for supportig format
CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Object> queryParams, QuerySettings settings)Sends sqlQuery as is. Additionally will send query parameter so server can comple SQL expression.
ClickHouseBinaryFormatReader newBinaryFormatReader(QueryResponse response, TableSchema schema)Creates a reader for RowBinary* and Native*. Table schema is required for RowBinaryWithNames and RowBinary formats. 'Native' and RowBinaryWithNamesAndTypes has the schema within data stream.
List<GenericRecord> queryAll(String sqlQuery)Queries a data in RowBinaryWithNamesAndTypes format. Returns result as a collection. Read performance is the same as with reader but more memory required at a time to keep whole dataset.

QuerySettings

Configuration MethodDefault ValueDescription
QuerySettings setQueryId(String queryId)Sets query ID that will be assigned to the operation
QuerySettings setFormat(ClickHouseFormat format)RowBinaryWithNamesAndTypesSets response format. See RowBinaryWithNamesAndTypes for the full list.
QuerySettings setMaxExecutionTime(Integer maxExecutionTime)Sets operation execution time on server. Will not affect read timeout.
QuerySettings waitEndOfQuery(Boolean waitEndOfQuery)falseRequests the server to wait for the and of the query before sending response.
QuerySettings setUseServerTimeZone(Boolean useServerTimeZone)trueServer timezone (see client config) will be used to parse date/time types in the result of an operation.
QuerySettings setUseTimeZone(String timeZone)Requests server to use timeZone for time conversion. See (session_timezone)[https://clickhouse.com/docs/en/operations/settings/settings#session_timezone]

Examples