Requirements
- Java 11+ (for Flink 1.17+) or 17+ (for Flink 2.0+)
- Apache Flink 1.17+
Flink version compatibility matrix
The connector is split into two artifacts to support both Flink 1.17+ and Flink 2.0+. Choose the artifact that matches your desired Flink version:The connector has not been tested against Flink versions earlier than 1.17.2
Installation & setup
Import as a dependency
For Flink 2.0+
- Maven
- Gradle
- SBT
For Flink 1.17+
- Maven
- Gradle
- SBT
Download the binary
The name pattern of the binary JAR is:flink_versionis one of2.0.0or1.17stable_versionis a stable artifact release version
Using the DataStream API
Snippet
Let’s say you want to insert raw CSV data into ClickHouse:- Java
Quick start example
We have created maven-based example for an easy start with the ClickHouse Sink: For more detailed instructions, see the Example GuideDataStream API connection options
ClickHouse client options
options and serverSettings should be passed to the client as Map<String, String>. An empty map for either will use client or server defaults, respectively.
All available Java client options are listed in ClientConfigProperties.java and this documentation page.All available server session settings are listed in this documentation page.
- Java
Sink options
The following options come directly from Flink’sAsyncSinkBase:
Supported data types
The table below provides a quick reference for converting data types when inserting from Flink into ClickHouse.Inserting data from Flink into ClickHouse
Notes:
- A
ZoneIdmust be provided when performing date operations. - Precision and scale must be provided when performing decimal operations.
- In order for ClickHouse to parse a Java String as JSON, you need to enable
enableJsonSupportAsStringinClickHouseClientConfig. - The connector requires an
ElementConvertorto map elements in the input DataStream to ClickHouse payloads. To this end, the connector providesClickHouseConvertorandPOJOConvertor, which you can use to implement this mapping using theDataWriterserialization methods above.
Supported input formats
You can find the list of available ClickHouse input formats in this documentation page and ClickHouseFormat.java. To specify the format the connector should use to serialize your DataStream to ClickHouse payloads, use thesetClickHouseFormat function. For example:
By default, the connector will use either RowBinaryWithDefaults or RowBinary if
setSupportDefault in ClickHouseClientConfig is explicitly set to true or false, respectively.Metrics
The connector exposes the following additional metrics on top of Flink’s existing metrics:Limitations
- The sink currently provides an at-least-once delivery guarantee. Work toward exactly-once semantics is being tracked here.
- The sink does not yet support a dead-letter queue (DLQ) for buffering unprocessable records. In the meantime, the connector will attempt to re-insert records that fail and drop them if unsuccessful. This feature is being tracked here.
- The sink does not yet support creation via Flink’s Table API or Flink SQL. This feature is being tracked here.
ClickHouse version compatibility and security
- The connector is tested against a range of recent ClickHouse versions, including latest and head, via a daily CI workflow. The tested versions are updated periodically as new ClickHouse releases become active. See here for the versions the connector is tested against daily.
- See the ClickHouse security policy for known security vulnerabilities and how to report a vulnerability.
- We recommend upgrading the connector continuously to not miss security fixes and new improvements.
- If you have an issue with migration, please create a GitHub issue and we will respond!
Advanced and recommended usage
- For optimal performance, ensure your DataStream element type is not a Generic type - see here for Flink’s type distinction. Non-generic elements will avoid the serialization overhead incurred by Kryo and improve throughput to ClickHouse.
- We recommend setting
maxBatchSizeto at least 1000 and ideally between 10,000 to 100,000. See this guide on bulk inserts for more information. - To do OLTP-style deduplication or upsert to ClickHouse, refer to this documentation page. Note: this is not to be confused with batch deduplication that happens on retries.
Troubleshooting
CANNOT_READ_ALL_DATA
The following error may occur:Low throughput
You may experience that the connector’s throughput does not scale with the job’s parallelism (Flink task count) when writing to ClickHouse. Cause: ClickHouse’s background part merging process may be slowing down inserts. This can happen when the configured batch size is too small, the connector is flushing too frequently, or a combination of the two. Solution: Monitor thenumRequestSubmitted and actualRecordsPerBatch metrics to help determine how to tune your batch size (maxBatchSize) and how frequently to flush. Also, see Advanced and recommended usage for batch sizing recommendations.
I am missing rows in my ClickHouse table
Cause: The batch(es) were dropped either because of a non-retryable failure or they could not be inserted in the configured number of retries (settable viaClickHouseClientConfig.setNumberOfRetries()). Note: by default, the connector will attempt to re-insert a batch up to 3 times before dropping it.
Solution: Inspect the TaskManager logs and/or stack trace(s) for the root cause.