一、JDBC 驅動
clickhouse 有兩種 JDBC 驅動實現。
官方驅動:
<dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.2.6</version> </dependency>
--注意如遇到
ClickHouse exception, code: 1002 可通過升級版本解決,0.2.6以下版本會出現響應失敗情況,升級版本即可。
三方提供的驅動:
<dependency> <groupId>com.github.housepower</groupId> <artifactId>clickhouse-native-jdbc</artifactId> <version>1.6-stable</version> </dependency>
兩者間的主要區別如下:
驅動類加載路徑不同,分別為 ru.yandex.clickhouse.ClickHouseDriver 和 com.github.housepower.jdbc.ClickHouseDriver
默認連接端口不同,分別為 8123 和 9000
連接協議不同,官方驅動使用 HTTP 協議,而三方驅動使用 TCP 協議
需要注意的是,兩種驅動不可共用,同個項目中只能選擇其中一種驅動。
二、代碼示例
本例使用三方提供的驅動,示例代碼如下所示:
2.1 create table
Class.forName("com.github.housepower.jdbc.ClickHouseDriver"); Connection connection = DriverManager.getConnection("jdbc:clickhouse://192.168.60.131:9000"); Statement statement = connection.createStatement(); statement.executeQuery("create table test.jdbc_example(day Date, name String, age UInt8) Engine=Log");
通過 clickhouse-client 命令行界面查看表情況:
ck-master :) show tables; SHOW TABLES ┌─name─────────┐ │ hits │ │ jdbc_example │ └──────────────┘
發現表 jdbc_example 成功創建。
2.2 batch insert
Class.forName("com.github.housepower.jdbc.ClickHouseDriver"); Connection connection = DriverManager.getConnection("jdbc:clickhouse://192.168.60.131:9000"); PreparedStatement pstmt = connection.prepareStatement("insert into test.jdbc_example values(?, ?, ?)"); // insert 10 records for (int i = 0; i < 10; i++) { pstmt.setDate(1, new Date(System.currentTimeMillis())); pstmt.setString(2, "panda_" + (i + 1)); pstmt.setInt(3, 18); pstmt.addBatch(); } pstmt.executeBatch();
通過命令行查詢,發現新增結果如下:
ck-master :) select * from jdbc_example; SELECT * FROM jdbc_example ┌────────day─┬─name─────┬─age─┐ │ 2019-04-25 │ panda_1 │ 18 │ │ 2019-04-25 │ panda_2 │ 18 │ │ 2019-04-25 │ panda_3 │ 18 │ │ 2019-04-25 │ panda_4 │ 18 │ │ 2019-04-25 │ panda_5 │ 18 │ │ 2019-04-25 │ panda_6 │ 18 │ │ 2019-04-25 │ panda_7 │ 18 │ │ 2019-04-25 │ panda_8 │ 18 │ │ 2019-04-25 │ panda_9 │ 18 │ │ 2019-04-25 │ panda_10 │ 18 │ └────────────┴──────────┴─────┘
2.3 select query
Class.forName("com.github.housepower.jdbc.ClickHouseDriver"); Connection connection = DriverManager.getConnection("jdbc:clickhouse://192.168.60.131:9000"); Statement statement = connection.createStatement(); String sql = "select * from test.jdbc_example"; ResultSet rs = statement.executeQuery(sql); while (rs.next()) { // ResultSet 的下標值從 1 開始,不可使用 0,否則越界,報 ArrayIndexOutOfBoundsException 異常 System.out.println(rs.getDate(1) + ", " + rs.getString(2) + ", " + rs.getInt(3)); } 運行代碼后,控制台輸出如下結果: 2019-04-25, panda_1, 18 2019-04-25, panda_2, 18 2019-04-25, panda_3, 18 2019-04-25, panda_4, 18 2019-04-25, panda_5, 18 2019-04-25, panda_6, 18 2019-04-25, panda_7, 18 2019-04-25, panda_8, 18 2019-04-25, panda_9, 18 2019-04-25, panda_10, 18
顯示結果與我們上面在命令行中查詢的結果相同。
2.4 drop table
Class.forName("com.github.housepower.jdbc.ClickHouseDriver"); Connection connection = DriverManager.getConnection("jdbc:clickhouse://192.168.60.131:9000"); Statement statement = connection.createStatement(); statement.executeQuery("drop table test.jdbc_example");
再次通過命令行確認:
ck-master :) show tables; SHOW TABLES ┌─name─┐ │ hits │ └──────┘
發現表 jdbc_example 已被刪除。
三、解決 Connection refuse 的問題
默認配置下,如果我們連接遠程服務器上的 clickhouse,會出現 Connection refuse 異常。
一開始以為是防火牆導致的,結果關閉防火牆后發現問題仍未解決,此時就開始懷疑是 clickhouse 本身的配置問題。
果然,在 clickhouse 的配置文件 /etc/clickhouse-server/config.xml 中發現了這么一段描述:
<!-- Default values - try listen localhost on ipv4 and ipv6: --> <!-- <listen_host>::1</listen_host> <listen_host>127.0.0.1</listen_host> -->
原來,默認情況下,clickhouse 只監聽本地的請求,因此我們進行遠程訪問時,會拋出 Connection refuse 異常。
那么,要如何解決這個問題呢?
在上述描述的前面,我們發現了這么一句話:
<!-- Listen specified host. use :: (wildcard IPv6 address), if you want to accept connections both with IPv4 and IPv6 from everywhere. --> <!-- <listen_host>::</listen_host> -->
意思就是,如果我們想監聽來自任意主機的請求,可以增加如下配置:
<listen_host>::</listen_host>
按照此方法修改保存,並重啟 clickhouse。再次進行遠程訪問時,發現不會再有 Connection refuse 的錯誤了。