clickhouse-jdbc 实现了标准的 JDBC 接口。它基于 clickhouse-client 构建,提供了自定义类型映射、事务支持以及标准同步 UPDATE 和 DELETE 语句等附加功能,因此可以轻松地与遗留应用程序和工具集成使用。
注意
最新的 JDBC(0.7.2)版本使用 Client-V1
clickhouse-jdbc API 是同步的,通常会产生更多开销(例如 SQL 解析和类型映射/转换等)。当性能至关重要或您希望以更直接的方式访问 ClickHouse 时,请考虑使用 clickhouse-client。
环境要求
<!-- https://mvnrepository.com/artifact/com.clickhouse/clickhouse-jdbc -->
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.7.2</version>
<!-- 使用包含所有依赖的 uber JAR 包,如需更小的 JAR 包可将 classifier 更改为 http -->
<classifier>shaded-all</classifier>
</dependency>
// https://mvnrepository.com/artifact/com.clickhouse/clickhouse-jdbc
// 使用包含所有依赖的 uber JAR 包,如需更小的 JAR 包可将 classifier 更改为 http
implementation("com.clickhouse:clickhouse-jdbc:0.7.2:shaded-all")
// https://mvnrepository.com/artifact/com.clickhouse/clickhouse-jdbc
// 使用包含所有依赖的 uber JAR 包,如需更小的 JAR 包可将 classifier 更改为 http
implementation 'com.clickhouse:clickhouse-jdbc:0.7.2:shaded-all'
从版本 0.5.0 开始,我们使用了打包在客户端中的 Apache HTTP Client。由于该包不存在共享版本,您需要将日志记录器作为依赖项添加。
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation("org.slf4j:slf4j-api:2.0.16")
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation 'org.slf4j:slf4j-api:2.0.16'
驱动程序类: com.clickhouse.jdbc.ClickHouseDriver
URL 语法:jdbc:(ch|clickhouse)[:<protocol>]://endpoint1[,endpoint2,...][/<database>][?param1=value1¶m2=value2][#tag1,tag2,...],示例如下:
jdbc:ch://localhost 与 jdbc:clickhouse:http://localhost:8123 是等价的
jdbc:ch:https://localhost 相当于 jdbc:clickhouse:http://localhost:8443?ssl=true&sslmode=STRICT
jdbc:ch:grpc://localhost 等同于 jdbc:clickhouse:grpc://localhost:9100
连接属性:
| 属性 | 默认值 | 说明 |
|---|
continueBatchOnError | false | 在发生错误时是否继续执行批处理 |
createDatabaseIfNotExist | false | 如果数据库不存在,是否创建该数据库 |
custom_http_headers | | 用逗号分隔的自定义 HTTP 头部,例如:User-Agent=client1,X-Gateway-Id=123 |
custom_http_params | | 以逗号分隔的自定义 HTTP 查询参数,例如:extremes=0,max_result_rows=100 |
nullAsDefault | 0 | 0 - 按原样处理 null 值,在向非 Nullable 列插入 null 时抛出异常;1 - 按原样处理 null 值,并在插入 null 时禁用 null 检查;2 - 在查询和插入时都将 null 替换为对应数据类型的默认值 |
jdbcCompliance | true | 是否支持标准的同步 UPDATE/DELETE 以及模拟事务 |
typeMappings | | 自定义 ClickHouse 数据类型与 Java 类之间的映射关系,这将影响 getColumnType() 和 getObject(Class<>?>) 的返回结果。例如:UInt128=java.lang.String,UInt256=java.lang.String |
wrapperObject | false | getObject() 在处理 Array / Tuple 时是否应返回 java.sql.Array / java.sql.Struct。 |
注意:更多信息请参考 JDBC 特定配置。
支持的数据类型
JDBC 驱动支持与客户端库相同的数据格式。
注意
- AggregatedFunction - ⚠️ 不支持执行
SELECT * FROM table ...
- Decimal - 在 21.9+ 中使用
SET output_format_decimal_trailing_zeros=1 以确保一致性
- 枚举 - 既可以作为字符串,也可以作为整数使用
- UInt64 - 在 client-v1 中映射为
long
创建连接
String url = "jdbc:ch://my-server/system"; // use http protocol and port 8123 by default
Properties properties = new Properties();
ClickHouseDataSource dataSource = new ClickHouseDataSource(url, properties);
try (Connection conn = dataSource.getConnection("default", "password");
Statement stmt = conn.createStatement()) {
}
简单语句
try (Connection conn = dataSource.getConnection(...);
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from numbers(50000)");
while(rs.next()) {
// ...
}
}
Insert
注意
- Use
PreparedStatement 而不是 Statement
它更易于使用,但性能比 input 函数慢(见下文):
try (PreparedStatement ps = conn.prepareStatement("insert into mytable(* except (description))")) {
ps.setString(1, "test"); // id
ps.setObject(2, LocalDateTime.now()); // timestamp
ps.addBatch(); // parameters will be write into buffered stream immediately in binary format
...
ps.executeBatch(); // stream everything on-hand into ClickHouse
}
一个性能表现优异的选项:
try (PreparedStatement ps = conn.prepareStatement(
"insert into mytable select col1, col2 from input('col1 String, col2 DateTime64(3), col3 Int32')")) {
// The column definition will be parsed so the driver knows there are 3 parameters: col1, col2 and col3
ps.setString(1, "test"); // col1
ps.setObject(2, LocalDateTime.now()); // col2, setTimestamp is slow and not recommended
ps.setInt(3, 123); // col3
ps.addBatch(); // parameters will be write into buffered stream immediately in binary format
...
ps.executeBatch(); // stream everything on-hand into ClickHouse
}
使用占位符插入数据
此选项仅建议用于小批量插入,因为它需要较长的 SQL 表达式(该表达式将在客户端解析并消耗 CPU & 内存):
try (PreparedStatement ps = conn.prepareStatement("insert into mytable values(trim(?),?,?)")) {
ps.setString(1, "test"); // id
ps.setObject(2, LocalDateTime.now()); // timestamp
ps.setString(3, null); // description
ps.addBatch(); // append parameters to the query
...
ps.executeBatch(); // issue the composed query: insert into mytable values(...)(...)...(...)
}
处理 DateTime 和时区
请使用 java.time.LocalDateTime 或 java.time.OffsetDateTime 代替 java.sql.Timestamp,并使用 java.time.LocalDate 代替 java.sql.Date。
try (PreparedStatement ps = conn.prepareStatement("select date_time from mytable where date_time > ?")) {
ps.setObject(2, LocalDateTime.now());
ResultSet rs = ps.executeQuery();
while(rs.next()) {
LocalDateTime dateTime = (LocalDateTime) rs.getObject(1);
}
...
}
处理 AggregateFunction
// batch insert using input function
try (ClickHouseConnection conn = newConnection(props);
Statement s = conn.createStatement();
PreparedStatement stmt = conn.prepareStatement(
"insert into test_batch_input select id, name, value from input('id Int32, name Nullable(String), desc Nullable(String), value AggregateFunction(groupBitmap, UInt32)')")) {
s.execute("drop table if exists test_batch_input;"
+ "create table test_batch_input(id Int32, name Nullable(String), value AggregateFunction(groupBitmap, UInt32))engine=Memory");
Object[][] objs = new Object[][] {
new Object[] { 1, "a", "aaaaa", ClickHouseBitmap.wrap(1, 2, 3, 4, 5) },
new Object[] { 2, "b", null, ClickHouseBitmap.wrap(6, 7, 8, 9, 10) },
new Object[] { 3, null, "33333", ClickHouseBitmap.wrap(11, 12, 13) }
};
for (Object[] v : objs) {
stmt.setInt(1, (int) v[0]);
stmt.setString(2, (String) v[1]);
stmt.setString(3, (String) v[2]);
stmt.setObject(4, v[3]);
stmt.addBatch();
}
int[] results = stmt.executeBatch();
...
}
// use bitmap as query parameter
try (PreparedStatement stmt = conn.prepareStatement(
"SELECT bitmapContains(my_bitmap, toUInt32(1)) as v1, bitmapContains(my_bitmap, toUInt32(2)) as v2 from {tt 'ext_table'}")) {
stmt.setObject(1, ClickHouseExternalTable.builder().name("ext_table")
.columns("my_bitmap AggregateFunction(groupBitmap,UInt32)").format(ClickHouseFormat.RowBinary)
.content(new ByteArrayInputStream(ClickHouseBitmap.wrap(1, 3, 5).toBytes()))
.asTempTable()
.build());
ResultSet rs = stmt.executeQuery();
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getInt(1), 1);
Assert.assertEquals(rs.getInt(2), 0);
Assert.assertFalse(rs.next());
}
配置 HTTP 库
ClickHouse JDBC 连接器支持三个 HTTP 库:HttpClient、HttpURLConnection 和 Apache HttpClient。
注意
HttpClient 仅支持 JDK 11 及以上版本。
JDBC 驱动程序默认使用 HttpClient。您可以通过设置以下属性来更改 ClickHouse JDBC 连接器所使用的 HTTP 库:
properties.setProperty("http_connection_provider", "APACHE_HTTP_CLIENT");
以下是对应值的完整列表:
| 属性 | HTTP 库 |
|---|
| HTTP_CLIENT | HttpClient |
| HTTP_URL_CONNECTION | HttpURLConnection |
| APACHE_HTTP_CLIENT | Apache HttpClient |
通过 SSL 连接到 ClickHouse
要使用 SSL 建立到 ClickHouse 的安全 JDBC 连接,需要配置 JDBC 属性以包含 SSL 参数。这通常需要在 JDBC URL 或 Properties 对象中指定 SSL 属性,如 sslmode 和 sslrootcert。
SSL 属性
| 名称 | 默认值 | 可选值 | 说明 |
|---|
ssl | false | true, false | 是否为该连接启用 SSL/TLS |
sslmode | strict | strict, none | 是否校验 SSL/TLS 证书 |
sslrootcert | | | SSL/TLS 根证书路径 |
sslcert | | | SSL/TLS 证书路径 |
sslkey | | | PKCS#8 格式的 RSA 私钥 |
key_store_type | | JKS, PKCS12 | 指定 KeyStore/TrustStore 文件的类型或格式 |
trust_store | | | TrustStore 文件路径 |
key_store_password | | | 用于访问 KeyStore 配置中指定的 KeyStore 文件的密码 |
这些属性可确保您的 Java 应用程序通过加密连接与 ClickHouse 服务器进行通信,从而增强数据传输过程中的安全性。
String url = "jdbc:ch://your-server:8443/system";
Properties properties = new Properties();
properties.setProperty("ssl", "true");
properties.setProperty("sslmode", "strict"); // NONE to trust all servers; STRICT for trusted only
properties.setProperty("sslrootcert", "/mine.crt");
try (Connection con = DriverManager
.getConnection(url, properties)) {
try (PreparedStatement stmt = con.prepareStatement(
// place your code here
}
}
解决大批量插入时的 JDBC 超时问题
在 ClickHouse 中执行大批量插入操作且执行时间较长时,可能会遇到 JDBC 超时错误,如下所示:
Caused by: java.sql.SQLException: Read timed out, server myHostname [uri=https://hostname.aws.clickhouse.cloud:8443]
这些错误可能会中断数据插入过程并影响系统稳定性。要解决此问题,需要调整客户端操作系统中的几个超时设置。
Mac OS
在 Mac OS 上,可以调整以下设置以解决此问题:
net.inet.tcp.keepidle: 60000
net.inet.tcp.keepintvl: 45000
net.inet.tcp.keepinit: 45000
net.inet.tcp.keepcnt: 8
net.inet.tcp.always_keepalive: 1
Linux
在 Linux 上,仅配置等效设置可能无法解决问题。由于 Linux 处理套接字 keep-alive 设置的方式不同,需要执行额外的步骤。请按以下步骤操作:
- 在
/etc/sysctl.conf 或其他相关配置文件中调整以下 Linux 内核参数:
net.inet.tcp.keepidle: 60000
net.inet.tcp.keepintvl: 45000
net.inet.tcp.keepinit: 45000
net.inet.tcp.keepcnt: 8
net.inet.tcp.always_keepalive: 1
net.ipv4.tcp_keepalive_intvl: 75
net.ipv4.tcp_keepalive_probes: 9
net.ipv4.tcp_keepalive_time: 60(可考虑将此值从默认的 300 秒下调)
- 修改内核参数后,运行以下命令使更改生效:
设置这些配置后,您需要确保客户端在套接字上启用 Keep Alive 选项:
properties.setProperty("socket_keepalive", "true");
注意
目前,在设置套接字保活(keep-alive)时,您必须使用 Apache HTTP Client 库,因为 clickhouse-java 支持的其他两个 HTTP 客户端库不允许设置套接字选项。详细指南请参阅配置 HTTP 库。
或者,您可以在 JDBC URL 中添加等效参数。
JDBC 驱动程序的默认套接字和连接超时时间为 30 秒。可以增加超时时间以支持大规模数据插入操作。使用 ClickHouseClient 的 options 方法,结合 ClickHouseClientOption 中定义的 SOCKET_TIMEOUT 和 CONNECTION_TIMEOUT 选项:
final int MS_12H = 12 * 60 * 60 * 1000; // 12 h in ms
final String sql = "insert into table_a (c1, c2, c3) select c1, c2, c3 from table_b;";
try (ClickHouseClient client = ClickHouseClient.newInstance(ClickHouseProtocol.HTTP)) {
client.read(servers).write()
.option(ClickHouseClientOption.SOCKET_TIMEOUT, MS_12H)
.option(ClickHouseClientOption.CONNECTION_TIMEOUT, MS_12H)
.query(sql)
.executeAndWait();
}