使用DataStax Java驱动程序1.0.4连接Cassandra时出现CQL异常。

4

我在我的笔记本电脑上运行着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文件已按照文档说明添加到我的类路径中。

3个回答

11

事实证明我错过了文档中的一节。

我使用了一个旧的cassandra.yaml配置文件,它来自于一个早期版本的Cassandra,并且没有启用本地传输二进制协议。以下代码片段展示了我需要更改的设置:

# 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及以上版本开始,本地传输将默认启用。


3

如果您尝试使用旧版Java驱动程序与较新版本的Cassandra一起使用,则也会遇到此错误。


1
我正在运行Cassandra Community 2.0.6,我需要使用哪个驱动程序版本?我尝试了2.0.1,但仍然出现错误。我可以通过telnet连接到端口和IP地址,所以我知道它们正在运行... - Nicholas Terry

0

我在Ubuntu中遇到了同样的问题。我最近使用以下命令安装了数据线指南:

apt-get install cassandra=2.0.11 dsc20=2.0.11-1

在此之前,我只使用了以下命令:

apt-get install cassandra

然后它对我产生了相同的问题。我只是用第一个命令(数据线指南)将其删除,然后重新安装。

P.S. 要删除cassandra,请使用以下命令

apt-get purge cassandra


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接