转载:CLickHouse出现端口异常问题
执行一段代码,然后报错如下Port 9000 is for clickhouse-client program
详细的报错如下
java.lang.RuntimeException: ru.yandex.clickhouse.except.ClickHouseUnknownException: ClickHouse exception, code: 1002, host: 10.3.1.59, port: 9000; Port 9000 is for clickhouse-client program. You must use port 8123 for HTTP.
at ru.yandex.clickhouse.ClickHouseConnectionImpl.initTimeZone(ClickHouseConnectionImpl.java:99)
at ru.yandex.clickhouse.ClickHouseConnectionImpl.<init>(ClickHouseConnectionImpl.java:80)
at ru.yandex.clickhouse.ClickHouseDriver.connect(ClickHouseDriver.java:55)
at ru.yandex.clickhouse.ClickHouseDriver.connect(ClickHouseDriver.java:47)
at ru.yandex.clickhouse.ClickHouseDriver.connect(ClickHouseDriver.java:29)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.crgt.gtdata.ClickHouseTest2.main(ClickHouseTest2.java:24)
Caused by: ru.yandex.clickhouse.except.ClickHouseUnknownException: ClickHouse exception, code: 1002, host: 10.3.1.59, port: 9000; Port 9000 is for clickhouse-client program. You must use port 8123 for HTTP.
at ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier.getException(ClickHouseExceptionSpecifier.java:91)
at ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier.specify(ClickHouseExceptionSpecifier.java:55)
at ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier.specify(ClickHouseExceptionSpecifier.java:28)
at ru.yandex.clickhouse.ClickHouseStatementImpl.checkForErrorAndThrow(ClickHouseStatementImpl.java:875)
at ru.yandex.clickhouse.ClickHouseStatementImpl.getInputStream(ClickHouseStatementImpl.java:616)
at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:117)
at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:100)
at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:95)
at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:90)
at ru.yandex.clickhouse.ClickHouseConnectionImpl.initTimeZone(ClickHouseConnectionImpl.java:94)
... 7 more
Caused by: java.lang.Throwable: Port 9000 is for clickhouse-client program. You must use port 8123 for HTTP.
at ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier.specify(ClickHouseExceptionSpecifier.java:53)
... 15 more
详细的代码如下
public void nativeClient3() throws Exception {
Class.forName("com.github.housepower.jdbc.ClickHouseDriver");
Connection connection = DriverManager.getConnection("jdbc:clickhouse://localhost:9000");
PreparedStatement pstmt = connection.prepareStatement("insert into lcc.t_city values(?, ?, ?,?)");
// insert 10 records
for (int i = 0; i < 10; i++) {
pstmt.setString(1, "panda_" + (i + 1));
pstmt.setString(2, "panda_" + (i + 1));
pstmt.setDate(2, new Date(System.currentTimeMillis()));
pstmt.setInt(3, 18);
pstmt.addBatch();
}
pstmt.executeBatch();
}
后来测试发现,同时引入了两个jar包,一个官方jar包,一个非官方jar包
<dependency>
<groupId>com.github.housepower</groupId>
<artifactId>clickhouse-native-jdbc</artifactId>
<version>1.7-stable</version>
</dependency>
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.2</version>
</dependency>
官方jar包默认是8123端口,而第三方jar包(clickhouse-native-jdbc)默认端口是9000,如果同时引入两个jar包,虽然自己指定
Class.forName("com.github.housepower.jdbc.ClickHouseDriver")
为第三方jar包,实际执行中,使用官方端口.
官方第三方jar包是继承了
public class ClickHouseDriver implements Driver
官方jar包是 直接继承 Driver类
public class ClickHouseDriver implements Driver {
static {
try {
DriverManager.registerDriver(new ClickHouseDriver());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
解决方案,只引入一个jar包,如果是官方的jar包就使用8123端口,如果是第三方是 9000端口
public class ClickHouseTest3 {
public static void main(String[] args) {
System.out.println("test click house");
try {
Class.forName("com.github.housepower.jdbc.ClickHouseDriver");//第三方jar包
// Class.forName("ru.yandex.clickhouse.ClickHouseDriver");//官方
//如果官方jar包引入,则下面不能执行,如果包官方jar包去掉,则可以执行
Connection connection = DriverManager.getConnection("jdbc:clickhouse://127.0.0.1:9000");//第三方
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select count(*) from test");
if (rs.next()) {
int count = rs.getInt(1);
System.out.println(count);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_21383435/article/details/121687960
内容来源于网络,如有侵权,请联系作者删除!