JAVA 应用可以通过 ClickHouse JDBC Driver 与 ClickHouse 进行交互。
注意
BitMap64
数据类型时,需要安装ByteHouse JDBC专用补丁,详见后文。创建一个 HelloClickHouse.java
,将以下代码贴入,即可连接 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)); } }
以下示例则拼接了一系列复杂 SQL:
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)); } } }
在使用官方 JDBC 0.3.2-patch11 连接 ByteHouse 企业版 2.1.0 执行查询时,可能会产生报错:java.sql.SQLException: Unknown data type: BitMap64
,此时需要安装驱动补丁来保障程序正常运行:
src/main/resources/lib
文件夹下。<dependencies> <dependency> <groupId>0.3.2-patch11-ce-patch-v1</groupId> <artifactId>0.3.2-patch11-ce-patch-v1</artifactId> <version>0.3.2-patch11-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}
函数辅助获得部分路径。