[英]Exception when connecting to Cassandra with CQL using DataStax Java driver 1.0.4
I have Cassandra 1.2.11 running on my laptop. 我在我的笔记本电脑上运行了Cassandra 1.2.11。 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: 我可以使用
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)
I am using the following code to connect, taken from the DataStax documentation. 我使用以下代码进行连接,取自DataStax文档。 I have tried several port number, including leaving the
withPort()
call out but nothing seems to work. 我尝试了几个端口号,包括离开
withPort()
调用,但似乎没有任何工作。
Cluster cluster = new Cluster.Builder()
.addContactPoints("localhost")
.withPort(9160)
.build();
Using telnet
I can verify that the Cassandra server is definitely listening at each of the ports I have specified. 使用
telnet
我可以验证Cassandra服务器肯定在我指定的每个端口上监听。 I have also verified that all the required library jar files are on my classpath as described in the documentation. 我还验证了所有必需的库jar文件都在我的类路径中,如文档中所述。
It turns out I missed a section in the documentation . 事实证明我错过了文档中的一个部分。
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. 我使用的是早期版本的Cassandra中的旧
cassandra.yaml
配置文件,它没有启用Native Transport二进制协议。 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
After restarting Cassandra the client was able to connect successfully and run CQL 3.0 commands. 重新启动Cassandra后,客户端能够成功连接并运行CQL 3.0命令。 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();
Also, note that from Cassandra 1.2.5 and above, the native transport is enabled by default. 另请注意,从Cassandra 1.2.5及更高版本开始,默认情况下启用本机传输。
尝试使用旧版本的java驱动程序和较新的cassandra版本时,也会出现此错误。
I have the same issue in Ubuntu. 我在Ubuntu中遇到了同样的问题。 I installed recently with the below command which was the datastax guideline
我最近安装了以下命令,这是datastax指南
apt-get install cassandra=2.0.11 dsc20=2.0.11-1
apt-get install cassandra = 2.0.11 dsc20 = 2.0.11-1
before that I used just 在此之前,我只使用了
apt-get install cassandra
apt-get install cassandra
then it makes the same issue for me. 那对我来说也是同样的问题。 I just removed then installed it with the first command (datastax guideline).
我刚删除然后用第一个命令(datastax指南)安装它。
PS For remove the cassandra use below command PS用于删除cassandra使用以下命令
apt-get purge cassandra
apt-get purge cassandra
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.