gpt4 book ai didi

java - 使用 DataStax Java 驱动程序 1.0.4 通过 CQL 连接到 Cassandra 时出现异常

转载 作者:搜寻专家 更新时间:2023-11-01 01:10:21 24 4
gpt4 key购买 nike

我的笔记本电脑上运行着 Cassandra 1.2.11。我可以使用 nodetoolcqlsh 连接到它,但是当我尝试使用 DataStax 1.0.4 Java API 使用 CQL 3.0 进行连接时,出现以下错误:

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1 ([localhost/127.0.0.1] Unexpected error during transport initialization (com.datastax.driver.core.TransportException: [localhost/127.0.0.1] Channel has been closed)))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:186)

我正在使用以下代码进行连接,该代码取自 DataStax 文档。我已经尝试了几个端口号,包括离开 withPort() 调用,但似乎没有任何效果。

Cluster cluster = new Cluster.Builder()
.addContactPoints("localhost")
.withPort(9160)
.build();

使用 telnet 我可以验证 Cassandra 服务器确实正在监听我指定的每个端口。我还验证了所有必需的库 jar 文件都在我的类路径中,如文档中所述。

最佳答案

原来我错过了 documentation 中的一个部分.

我使用的是早期版本的 Cassandra 中的旧 cassandra.yaml 配置文件,它没有启用 Native Transport 二进制协议(protocol)。以下代码段显示了我需要更改的设置:

# Whether to start the native transport server.
start_native_transport: true
# port for the CQL native transport to listen for clients on
native_transport_port: 9042

重新启动 Cassandra 后,客户端能够成功连接并运行 CQL 3.0 命令。请注意,创建连接的代码必须更改为使用文件中指定的端口,如下所示:

Cluster cluster = new Cluster.Builder()
.addContactPoints("localhost")
.withPort(9042)
.build();

另外,请注意来自 Cassandra 1.2.5及以上版本,默认情况下启用 native 传输。

关于java - 使用 DataStax Java 驱动程序 1.0.4 通过 CQL 连接到 Cassandra 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20409470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com