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

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)

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();

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.

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

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();

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值