连接到远程Cassandra服务器显示NoHostAvailableException

时间:2018-01-25 08:46:03

标签: java cassandra datastax-java-driver

关于Cassandra NoHostAvailableException有很多问题。即使在跟随其中许多但我无法解决这个问题。

从我的本地计算机上我无法使用java访问cassandra数据库。

java代码是:在远程服务器中

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;

public class Read_Data {

    public static void main(String args[])throws Exception{

        //queries
        String query = "select title,hotel_id,comment from agoda_kuchikomi limit 1";

        //Creating Cluster object
        Cluster cluster = Cluster.builder().addContactPoint("172.15.81.249").build();

        //Creating Session object
        Session session = cluster.connect("kuchikomi");

        //Getting the ResultSet
        ResultSet result = session.execute(query);

        System.out.println(result.all());
    }
}

错误

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /172.15.81.249:9042 (com.datastax.driver.core.exceptions.TransportException: [/172.15.81.249:9042] Cannot connect))
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:232)
    at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
    at com.datastax.driver.core.Cluster$Manager.negotiateProtocolVersionAndConnect(Cluster.java:1600)
    at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1518)
    at com.datastax.driver.core.Cluster.init(Cluster.java:159)
    at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:330)
    at com.datastax.driver.core.Cluster.connect(Cluster.java:280)
    at Read_Data.main(Read_Data.java:17)

Cassandra在AWS ec2实例中托管。

我的cassandra.yaml文件有

rpc_address: 0.0.0.0#我已将它从172.15.81.249更改为0.0.0.0

broadcast_rpc_address: 172.15.81.249

native_transport_port: 9042

rpc_port: 9160

1 个答案:

答案 0 :(得分:0)

我的AWS实例不允许使用我的IP,因此我在Cassandra AWS实例上添加了一些入站规则,以允许ip存在源代码。就是这样。