我的笔记本电脑上运行的是 Cassandra 1.2.11。我可以使用连接到它nodetool
and 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)
我使用以下代码进行连接,取自 DataStax 文档。我尝试了几个端口号,包括留下withPort()
大声喊叫,但似乎没有任何作用。
Cluster cluster = new Cluster.Builder()
.addContactPoints("localhost")
.withPort(9160)
.build();
Using telnet
我可以验证 Cassandra 服务器确实正在侦听我指定的每个端口。我还验证了所有必需的库 jar 文件都位于我的类路径中,如文档中所述。
原来我错过了其中的一个部分文档 http://www.datastax.com/documentation/developer/java-driver/1.0/webhelp/java-driver/reference/ConnectionRequirements_r.html.
我用的是旧的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
重新启动 Cassandra 后,客户端能够成功连接并运行 CQL 3.0 命令。请注意,必须更改创建连接的代码以使用文件中指定的端口,如下所示:
Cluster cluster = new Cluster.Builder()
.addContactPoints("localhost")
.withPort(9042)
.build();
另外,请注意,从卡桑德拉1.2.5 https://github.com/apache/cassandra/blob/cassandra-1.2.5/conf/cassandra.yaml#L335及以上,默认情况下启用本机传输。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)