简体   繁体   中英

Connecting to remote Cassandra server shows NoHostAvailableException

There are many questions regarding Cassandra NoHostAvailableException . Even after following many of them yet I am unable to fix this issue.

From my local machine I can not access cassandra DB using java.

java code is: ( In remote server )

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

Error is

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 hosted in AWS ec2 instance.

my cassandra.yaml file has

rpc_address: 0.0.0.0 # I have changed it from 172.15.81.249 to 0.0.0.0

broadcast_rpc_address: 172.15.81.249

native_transport_port: 9042

rpc_port: 9160

My AWS instance was not allowing my IP so I added some inbound rules on Cassandra AWS instance to allow ip where source code is present. And that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM