使用Apache Cassandra 1.0的Java驱动程序连接Cassandra时出现错误,来自com.example.cassandra。

27

使用DataStax的Cassandra Java驱动程序连接Cassandra客户端时,会引发以下错误:

Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: [/127.0.0.1])

请提供建议...

谢谢!

我的Java代码如下:

package com.example.cassandra;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;

public class SimpleClient {

private Cluster cluster;

public void connect(String node){

    cluster = Cluster.builder().addContactPoint(node).build();
    Metadata metadata = cluster.getMetadata();
    System.out.println(metadata.getClusterName());
}   


public void close()
{
cluster.shutdown();
}

public static void main(String args[]) {

SimpleClient client = new SimpleClient();
client.connect("127.0.0.1");
client.close();
}

1
只是为了更加清晰明确地表达:我确定已经连接到cassandra了。但仍然显示这个错误。 - Saurabh Deshpande
11个回答

13

在我的情况下,我使用了默认的RPC端口9160进行连接,所以遇到了这个问题。可以在cassandra.yaml中找到不同的CQL端口 -

start_native_transport: true
# port for the CQL native transport to listen for clients on
native_transport_port: 9042

当我修改代码使用9042端口后,连接尝试成功 -

public BinaryDriverTest(String cassandraHost, int cassandraPort, String keyspaceName) {
    m_cassandraHost = cassandraHost;
    m_cassandraPort = cassandraPort;
    m_keyspaceName = keyspaceName;

    LOG.info("Connecting to {}:{}...", cassandraHost, cassandraPort);
    cluster = Cluster.builder().withPort(m_cassandraPort).addContactPoint(cassandraHost).build();
    session = cluster.connect(m_keyspaceName);
    LOG.info("Connected.");
}

public static void main(String[] args) {
    BinaryDriverTest bdt = new BinaryDriverTest("127.0.0.1", 9042, "Tutorial");
}

5
我曾遇到过这个问题,通过在SocketOptions中设置ReadTimeout来解决:
Cluster cluster = Cluster.builder().addContactPoint("localhost").build();
cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(HIGHER_TIMEOUT);

4
请前往您的Apache Cassandra配置目录并启用二进制协议。Cassandra二进制协议是在Cassandra 1.2中引入的。仅适用于大于或等于1.2版本的Cassandra。此外,在Cassandra 1.2中,二进制协议服务器不使用默认配置文件启动。您必须为每个节点编辑cassandra.yaml文件。
start_native_transport: true

然后重新启动节点。


4

我也遇到了同样的问题。我在一台独立的Linux电脑上安装了Cassandra,并尝试通过Windows电脑进行连接。但是我没有被允许创建连接。

但是当我们编辑cassandra.yaml文件时,将我的Linux电脑IP地址设置为rpc_address并重新启动,就可以成功连接了。

# The address or interface to bind the Thrift RPC service and native transport
# server to.
#
# Set rpc_address OR rpc_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
#
# Leaving rpc_address blank has the same effect as on listen_address
# (i.e. it will be based on the configured hostname of the node).
#
# Note that unlike listen_address, you can specify 0.0.0.0, but you must also
# set broadcast_rpc_address to a value other than 0.0.0.0.
#rpc_address: localhost
rpc_address: 192.168.0.10

3

我在测试一个只有一个节点的新集群时遇到了同样的问题。

从集群构建器中删除这个后,我就可以连接了:

.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("US_EAST"))

连接成功。


3

我发帖是为了帮助可能遇到和我一样问题的人,当我看到那个错误信息时。结果证明,我的复杂依赖树引入了com.google.collections的旧版本,这破坏了CQL驱动程序。移除这个依赖并完全依赖guava解决了我的问题。


2
我也遇到了这个问题,原因是提交的语句中存在一个简单的错误。
session.prepare(null);

显然,这个错误信息是误导性的。

2
在我的情况下,这是一个端口问题,我忘记更新了。
旧的RPC端口是9160
新的二进制端口是9042

1

假设您已经使用默认配置,请检查驱动程序版本的兼容性。虽然它们声称向后兼容,但并不是所有驱动程序版本都与所有Cassandra版本兼容。请参见下面的链接。

http://docs.datastax.com/en/developer/java-driver/3.1/manual/native_protocol/

我遇到了类似的问题,更改驱动程序版本解决了我的问题。

注意:希望您正在使用Maven(或类似工具)来解决依赖关系。否则,您可能需要下载许多更高版本的驱动程序依赖项。


1
编辑
/etc/cassandra/cassandra.yaml 

rpc_address 更改为 0.0.0.0,将 broadcast_rpc_address 和 listen_address 更改为集群的 IP 地址。并更好地理解这些内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接