您可以通过开源的 ClickHouse JDBC 驱动程序连接到 ByteHouse 企业版,连接后进行库表操作、数据写入查询等。本文为您介绍 ClickHouse JDBC 驱动连接 ByteHouse 企业版的主要操作流程和操作要点。
支持 Java 11 或更高版本。
推荐下载 ClickHouse JDBC v0.6.5。
单击驱动下载链接,在 Assets 中下载 clickhouse-jdbc-0.6.5-all.jar 文件,下载 ClickHouse JDBC 驱动。
如果您需要使用 ByteHouse BitMap64 数据类型,需要安装 ByteHouse JDBC 专用补丁。
在使用官方 JDBC 0.3.2-patch11 连接 ByteHouse 企业版 2.1.0 执行查询时,可能会产生报错:java.sql.SQLException: Unknown data type: BitMap64,此时需要安装驱动补丁来保障程序正常运行:
下载 ByteHouse JDBC 补丁程序:
将文件拷贝到工程的 src/main/resources/lib 文件夹下。
以 Maven 构建,在项目的 pom.xml 文件里添加下面依赖项。
<dependencies> <dependency> <groupId>0.3.3-SNAPSHOT-ce-patch-v1</groupId> <artifactId>0.3.3-SNAPSHOT-ce-patch-v1</artifactId> <version>0.3.3-SNAPSHOT-ce-patch-v1</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/clickhouse-jdbc-0.3.3-SNAPSHOT-ce-patch-v1-all.jar</systemPath> <classifier>all</classifier> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <!-- 0.3.3-SNAPSHOT-ce-patch-v1 依赖 --> <groupId>org.roaringbitmap</groupId> <artifactId>RoaringBitmap</artifactId> <version>0.9.36</version> </dependency> </dependencies>
注意
<systemPath> 填写绝对路径,可以通过 ${project.basedir} 函数辅助获得部分路径。
使用 ClickHouse JDBC Driver 连接 ByteHouse 前,您需要获取 ByteHouse 连接信息。ByteHouse 企业版当前支持应用通过 IAM 子用户或数据库用户连接至 ByteHouse,常用连接信息获取方式说明如下,更多连接信息说明请参见获取集群连接信息。
参数 | 使用 IAM 子用户连接 | 使用数据库用户连接 |
|---|---|---|
Host | 您可以通过集群管理 > 集群列表 > 集群名称 > 基本信息路径,在网络信息模块,查看到当前集群的私网或公网连接地址(即 Host 地址)。 | |
port | 使用 HTTP 端口号固定为 8123。 | |
username | 在 ByteHouse 企业版控制台上,单击右上角 ByteHouse 企业版个人中心,单击账号管理,查看并复制集群连接账号名。 | 在 ByteHouse 企业版控制台上,通过权限管理 > 用户 > 用户列表路径,在列表中查看并复制数据库用户名称。 |
password | ByteHouse 企业版集群连接密码。在 ByteHouse 企业版控制台上,单击右上角 ByteHouse 企业版个人中心,单击账号管理,查看并复制集群连接密码。 | |
ByteHouse 企业版 IAM 子用户和数据库用户的差别说明如下:
子用户 | 数据库用户 | |
|---|---|---|
定义 | 由火山引擎主用户使用访问控制服务(IAM)创建的用户。 | ByteHouse 中创建的数据库级别的用户。 |
作用范围 | 集群或引擎级别 | 数据库级别 |
登录方式 |
| API、连接驱动、命令行、生态工具连接 |
适用场景 | 适用于需控制台操作、跨产品统一管控等场景。 | 适用于需要精细管理数据库内数据访问和操作权限的场景。 |
更多 IAM 用户和数据库用户的介绍请参见:权限模型、子用户管理、数据库用户管理。
创建一个 HelloClickHouse.java 文件,将以下代码贴入,即可连接 ByteHouse 并进行一个简单查询。
使用时注意替换连接语句中的 <host>、<ip>、<username>、<password> 等连接信息字段,获取方式请参见获取 ByteHouse 连接信息。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; /** */ public class SimpleQuery { public static void main(String[] args) throws Exception { Class.forName("com.clickhouse.jdbc.ClickHouseDriver"); Connection connection = DriverManager.getConnection("jdbc:clickhouse://<host>:<ip>", <username>, <password>); //<host>:<ip> 替换为 ByteHouse 节点的服务的地址和端口, <username>替换为用户名,<password>替换为密码 Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT (number % 3 + 1) as n, sum(number) FROM numbers(10000000) GROUP BY n"); while (rs.next()) { System.out.println(rs.getInt(1) + "\t" + rs.getLong(2)); } } }
package examples; import java.sql.*; /** * BatchQuery */ public class BatchQuery { public static void main(String[] args) throws Exception { try (Connection connection = DriverManager.getConnection("jdbc:clickhouse://<host>:<port>", username, password)) { //<host>:<ip> 替换为 ByteHouse 节点的服务的地址和端口, usernmae 替换为用户名, password 替换为密码 try (Statement stmt = connection.createStatement()) { try (ResultSet rs = stmt.executeQuery("drop table if exists test_jdbc_example")) { System.out.println(rs.next()); } try (ResultSet rs = stmt.executeQuery("create table test_jdbc_example(day Date, name String, age UInt8) Engine=Log")) { System.out.println(rs.next()); } try (PreparedStatement pstmt = connection.prepareStatement("INSERT INTO test_jdbc_example VALUES(?, ?, ?)")) { for (int i = 1; i <= 200; i++) { pstmt.setDate(1, new Date(System.currentTimeMillis())); if (i % 2 == 0) pstmt.setString(2, "Zhang San" + i); else pstmt.setString(2, "Zhang San"); pstmt.setByte(3, (byte) ((i % 4) * 15)); System.out.println(pstmt); pstmt.addBatch(); } pstmt.executeBatch(); } try (PreparedStatement pstmt = connection.prepareStatement("select count(*) from test_jdbc_example where age>? and age<=?")) { pstmt.setByte(1, (byte) 10); pstmt.setByte(2, (byte) 30); printCount(pstmt); } try (PreparedStatement pstmt = connection.prepareStatement("select count(*) from test_jdbc_example where name=?")) { pstmt.setString(1, "Zhang San"); printCount(pstmt); } try (ResultSet rs = stmt.executeQuery("drop table test_jdbc_example")) { System.out.println(rs.next()); } } } } public static void printCount(PreparedStatement pstmt) throws SQLException { try (ResultSet rs = pstmt.executeQuery()) { System.out.println(pstmt); if (rs.next()) System.out.println(rs.getInt(1)); } } }
未找到驱动程序类 'org. roaringbitmap. longlong. Roaring64NavigableMap'