使用DataStax Java驱动程序1.0.4使用CQL连接到Cassandra时出现异常 [英] Exception when connecting to Cassandra with CQL using DataStax Java driver 1.0.4

查看:178
本文介绍了使用DataStax Java驱动程序1.0.4使用CQL连接到Cassandra时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have Cassandra 1.2.11 running on my laptop. I can connect to it using nodetool and cqlsh but when I try and use the DataStax 1.0.4 Java API to connect using CQL 3.0 I get the following error:

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()调用但似乎没有任何工作。

I am using the following code to connect, taken from the DataStax documentation. I have tried several port number, including leaving the withPort() call out but nothing seems to work.

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

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

Using telnet I can verify that the Cassandra server is definitely listening at each of the ports I have specified. I have also verified that all the required library jar files are on my classpath as described in the documentation.

推荐答案

事实证明我错过了文档中的部分。

It turns out I missed a section in the documentation.

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

I was using an old cassandra.yaml configuration file from an earlier version of Cassandra and it didn't enable the Native Transport binary protocol. The following snippet shows the settings that I needed to change:

# 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命令。请注意,必须更改创建连接的代码以使用文件中指定的端口,如下所示:

After restarting Cassandra the client was able to connect successfully and run CQL 3.0 commands. Note that the code to create the connection must be changed to use the port specified in the file, like this:

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

此外,请注意来自Cassandra 1.2.5 及更高版本,默认情况下启用本机传输。

Also, note that from Cassandra 1.2.5 and above, the native transport is enabled by default.

这篇关于使用DataStax Java驱动程序1.0.4使用CQL连接到Cassandra时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆