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

时间:2013-12-05 20:00:26

标签: java cassandra cql3 datastax-java-driver

我的笔记本电脑上运行了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 个答案:

答案 0 :(得分:11)

事实证明我错过了documentation中的一个部分。

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

# 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及更高版本开始,默认情况下会启用本机传输。

答案 1 :(得分:3)

在尝试使用旧版本的cassandra版本的旧版java驱动程序时,也会出现此错误。

答案 2 :(得分:0)

我在Ubuntu中遇到同样的问题。我最近安装了以下命令,这是数据存储准则

  

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

之前我只用

  

apt-get install cassandra

那么它对我来说也是同样的问题。我刚删除然后用第一个命令(datastax指南)安装它。

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

  

apt-get purge cassandra