clickhouse-java icon indicating copy to clipboard operation
clickhouse-java copied to clipboard

ClickHouseNodes.of(String endpoints) Unable to connect localhost Clickhouse client

Open Sigfied opened this issue 1 year ago • 2 comments

    @Test
    public void test() throws SQLException, ClickHouseException {
        String url = "jdbc:clickhouse://localhost:8123/clickhouse_test"; // use http protocol and port 8123 by default

        ClickHouseNodes servers = ClickHouseNodes.of(url);

        try (ClickHouseClient client = ClickHouseClient.newInstance(ClickHouseProtocol.HTTP);
             ClickHouseResponse response = client.read(servers)
                     .format(ClickHouseFormat.RowBinaryWithNamesAndTypes)
                     .query("select * from new_table")
                     .executeAndWait()) {
            ClickHouseResponseSummary summary = response.getSummary();
            long totalRows = summary.getTotalRowsToRead();
            System.out.println("totalRows: " + totalRows);

        }
    }
com.clickhouse.client.ClickHouseException: Connect to http://localhost:0 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused), server ClickHouseNode [uri=http://localhost:0/default]@575295931

	at com.clickhouse.client.ClickHouseException.of(ClickHouseException.java:166)
	at com.clickhouse.client.AbstractClient.lambda$execute$0(AbstractClient.java:275)
	at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:750)
Caused by: org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:0 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:607)
	at org.apache.hc.client5.http.socket.PlainConnectionSocketFactory.lambda$connectSocket$0(PlainConnectionSocketFactory.java:91)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.apache.hc.client5.http.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:90)
	at org.apache.hc.client5.http.socket.ConnectionSocketFactory.connectSocket(ConnectionSocketFactory.java:123)
	at org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:184)
	at org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:447)
	at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:162)
	at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:172)
	at org.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:142)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:192)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.HttpRequestRetryExec.execute(HttpRequestRetryExec.java:96)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.RedirectExec.execute(RedirectExec.java:115)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:170)
	at org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:123)
	at com.clickhouse.client.http.ApacheHttpConnectionImpl.post(ApacheHttpConnectionImpl.java:241)
	at com.clickhouse.client.http.ClickHouseHttpClient.send(ClickHouseHttpClient.java:118)
	at com.clickhouse.client.AbstractClient.sendAsync(AbstractClient.java:161)
	at com.clickhouse.client.AbstractClient.lambda$execute$0(AbstractClient.java:273)
	... 4 more

The local clickhouse client and server can connect screenshot-20240103-202810

Sigfied avatar Jan 03 '24 12:01 Sigfied

Hi, Can you change in to http://localhost:8123/clickhouse_test and test

mzitnik avatar Jan 07 '24 11:01 mzitnik

Just to note, I had the same problem after trying to get setup using the docs today.

Changing to use the format http://localhost:8123/clickhouse_test rather than jdbc:ch:http://localhost:8123/clickhouse_test as the docs state sorted it out for me.

Working example:

    private static final ClickHouseNodes servers = ClickHouseNodes.of(
            "http://localhost:8123/clickhouse_test");

Also had to add the LZ4 library too, based off of this issue: https://github.com/ClickHouse/clickhouse-java/issues/1317 which I think was also missing from the docs.

HarveyEllis avatar Mar 02 '24 17:03 HarveyEllis

你好, 你可以换成http://localhost:8123/clickhouse_test 并测试吗

Thank you, the problem has been solved

Sigfied avatar Mar 22 '24 06:03 Sigfied