使用 DataStax Java 驱动程序 1.0.4 通过 CQL 连接到 Cassandra 时出现异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20409470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception when connecting to Cassandra with CQL using DataStax Java driver 1.0.4
提问by grkvlt
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:
我的笔记本电脑上运行着 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)
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.
我正在使用以下代码进行连接,取自 DataStax 文档。我尝试了几个端口号,包括退出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. I have also verified that all the required library jar files are on my classpath as described in the documentation.
使用telnet
我可以验证 Cassandra 服务器是否确实在我指定的每个端口上侦听。我还验证了所有必需的库 jar 文件都在我的类路径中,如文档中所述。
采纳答案by grkvlt
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 Transportbinary protocol. The following snippet shows the settings that I needed to change:
我使用的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
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:
重新启动 Cassandra 后,客户端能够成功连接并运行 CQL 3.0 命令。请注意,创建连接的代码必须更改为使用文件中指定的端口,如下所示:
Cluster cluster = new Cluster.Builder()
.addContactPoints("localhost")
.withPort(9042)
.build();
Also, note that from Cassandra 1.2.5and above, the native transport is enabled by default.
另请注意,从Cassandra 1.2.5及更高版本开始,默认情况下启用本机传输。
回答by Douglas Rapp
You also get this error when trying to use an old version of the java driver with a newer cassandra version.
尝试将旧版本的 java 驱动程序与较新的 cassandra 版本一起使用时,也会出现此错误。
回答by Esmaeil MIRZAEE
I have the same issue in Ubuntu. I installed recently with the below command which was the datastax guideline
我在 Ubuntu 中有同样的问题。我最近安装了以下命令,这是 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 安装 cassandra
then it makes the same issue for me. I just removed then installed it with the first command (datastax guideline).
那么它对我来说也是同样的问题。我刚刚删除然后使用第一个命令(datastax 指南)安装它。
P.S. For remove the cassandra use below command
PS 要删除 cassandra 使用下面的命令
apt-get purge cassandra
apt-get 清除 cassandra