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

grk*_*vlt 4 java cassandra cql3 datastax-java-driver

我在我的笔记本电脑上运行了Cassandra 1.2.11.我可以使用nodetool和连接它,cqlsh但当我尝试使用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)
Run Code Online (Sandbox Code Playgroud)

我使用以下代码进行连接,取自DataStax文档.我已经尝试了几个端口号,包括离开withPort()呼叫,但似乎没有任何工作.

Cluster cluster = new Cluster.Builder()
        .addContactPoints("localhost")
        .withPort(9160)
        .build();
Run Code Online (Sandbox Code Playgroud)

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

grk*_*vlt 11

事实证明我错过了文档中的一个部分.

我使用的cassandra.yaml是早期版本的Cassandra中的旧配置文件,但它没有启用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 
Run Code Online (Sandbox Code Playgroud)

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

Cluster cluster = new Cluster.Builder()
        .addContactPoints("localhost")
        .withPort(9042)
        .build();
Run Code Online (Sandbox Code Playgroud)

另请注意,从Cassandra 1.2.5及更高版本开始,默认情况下启用本机传输.