1、Clickhouse连接端口默认8123,但是Clickhouse默认情况下不允许其他设备进行http连接,所以需要更改clickhouse的默认配置:
1 [root@master local]# vim /etc/clickhouse-server/config.xml
新增一行,默认是注释的,让本地服务可以远程连接远程部署的Clickhouse服务,如下所示:
此设置是允许任何设备进行连接,配置好了之后就可以API连接。创建一个maven项目,然后引入Clickhouse提供的依赖坐标即可,如下所示:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 4 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>com.bie</groupId> 7 <artifactId>clickhouse</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 10 <dependencies> 11 <dependency> 12 <groupId>ru.yandex.clickhouse</groupId> 13 <artifactId>clickhouse-jdbc</artifactId> 14 <version>0.2.6</version> 15 </dependency> 16 </dependencies> 17 </project>
然后搞一个测试类,和连mysql其实一样,将驱动包,url改成Clickhouse即可,如下所示:
1 package com.bie.utils; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.ResultSetMetaData; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 10 public class ClickhouseUtils { 11 12 private static Connection connection = null; 13 14 static { 15 try { 16 Class.forName("ru.yandex.clickhouse.ClickHouseDriver");// 驱动包 17 String url = "jdbc:clickhouse://192.168.110.133:8123/system";// url路径 18 String user = "default";// 账号 19 String password = "";// 密码 20 connection = DriverManager.getConnection(url, user, password); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 } 25 26 public static void main(String[] args) throws SQLException { 27 Statement statement = connection.createStatement(); 28 ResultSet resultSet = statement.executeQuery("select * from system.functions"); 29 ResultSetMetaData metaData = resultSet.getMetaData(); 30 int columnCount = metaData.getColumnCount(); 31 while (resultSet.next()) { 32 for (int i = 1; i <= columnCount; i++) { 33 System.out.println(metaData.getColumnName(i) + ":" + resultSet.getString(i)); 34 } 35 } 36 } 37 38 }
直接运行,发现没有啥子问题,就说明配置对了,maven构建项目,开发工具eclipse。