尝试打开与ClickhouseDB的连接时程序挂起?

时间:2019-08-14 14:04:05

标签: c# docker docker-compose ado.net clickhouse

我正在尝试从我的应用程序连接到本地ClickhouseDB以执行一些原始查询。当我尝试打开连接时出现问题。它永远不会从方法调用中返回。

ClickhouseDB在Docker容器中启动。

using (ClickHouseConnection conn = connFactory.CreateConnection(new ClickHouseConnectionSettings(connectionString)))
            {
                var cmd = conn.CreateCommand();

                cmd.CommandText = "select * from default.table_name";

                conn.Open(); // it never goes past this line
                using (var reader = cmd.ExecuteReader())
                ...

我想在conn.Open()之后打开一个连接,并将程序继续到执行读取器的下一行。

注意: 如果我尝试从python控制台连接,绝对没有问题。

from clickhouse_driver import Client
client = Client(host='localhost')
client.execute('select * from default.table_name')
# [(4, 'four'), (5, 'five'), (1, 'one'), (2, 'two'), (3, 'three')]

更新: ConnectionTimeout之后的Stacktrace:

System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at InhouseNamespace.ClickHouse.Impl.ProtocolFormatter.ReadBytes(Int32 i)
   ...

docker-compose部署Clickhouse。我不是Docker专家,所以我对DB满意时感到满意,并且可以通过Tabix Web UI连接到它:

version: "3.7"
services:
  clickhouse:
    image: yandex/clickhouse-server
    ports:
    - "8123:8123"
    - "9000:9000"
    - "9009:9009"

    ulimits:
     nproc: 65535
     nofile:
      soft: 262144
      hard: 262144
 client:
    image: yandex/clickhouse-client
    command: ['--host', 'server']

更新: 连接到非本地CassandraDB后,问题消失了,这意味着连接到我的本地CassandraDb(在Docker容器中)存在问题。

2 个答案:

答案 0 :(得分:0)

您所使用的连接字符串似乎不正确。

取出“端口”键,然后将端口号附加到服务器(现在已将其命名为“主机”)键,如下所示:

“ Server = 127.0.0.1,8123; User = default; Password =; Database = default; Compress = True; CheckCompressedHash = False; SocketTimeout = 60000000; Compressor = lz4”

谢谢, 加里

答案 1 :(得分:0)

端口8123用作HTTP接口的默认端口。但是在使用时:

conn.Open();
using (var reader = cmd.ExecuteReader())

通信是通过本地Clickhouse客户端完成的,因此必须使用端口9000。

要解决此问题,请更改连接字符串中的端口。完整的连接字符串如下所示:connString = "Host=127.0.0.1;Port=9000;User=default;Password=;Database=default;Compress=True;CheckCompressedHash=False;SocketTimeout=60000000;Compressor=lz4"