Jagveer Singh
Jagveer Singh

Reputation: 584

"[NoHostAvailableException: All host(s) tried for query failed" exception occurs in connecting with cassandra cluster

var cluster: Cluster = null
var session: Session = null
cluster = Cluster.builder().addContactPoints("192.168.1.3","192.168.1.2").build()
val metadata = cluster.getMetadata()
printf("Connected to cluster: %s\n",
metadata.getClusterName())
metadata.getAllHosts() map {
case host =>
  printf("Datatacenter: %s; Host: %s; Rack: %s\n",
    host.getDatacenter(), host.getAddress(), host.getRack())

}

i am not able to connect to cassandra cluster using this code . It is giving me error that-

[NoHostAvailableException: All host(s) tried for query failed (tried: /192.168.1.3 ([/192.168.1.3] Cannot connect), /192.168.1.2 ([/192.168.1.2] Cannot connect))]

What is my mistake in above code.

Upvotes: 2

Views: 4708

Answers (3)

Harender Bhardwaj
Harender Bhardwaj

Reputation: 16

you can use port 9042 and try to connect with ip local host or other machine as follows:

public String serverIP = "127.0.0.1"; //change ip with yours
//public String serverIP = "52.10.160.197"; //for prod
public String keyspace = "database name";  //for prod
//public String keyspace = "dbname_test";  //for staging
Cluster cluster = Cluster.builder().addContactPoint(serverIP).build();
Session session = cluster.connect(keyspace);

Upvotes: 0

Harender Bhardwaj
Harender Bhardwaj

Reputation: 16

Remote access to Cassandra is via its thrift port (although note that the JMX port can be used to perform some limited operations).

The thrift port is defined in cassandra.yaml by the rpc_port parameter, which defaults to 9160. Your cassandra node should be bound to the IP address of your server's network card - it shouldn't be 127.0.0.1 or localhost which is the loopback interface's IP, binding to this will prevent direct remote access. You configure the bound address with the rpc_address parameter in cassandra.yaml. Setting this to 0.0.0.0 says "listen on all network interfaces" which may or may not be suitable for you.

To make a connection you can use:

The cassandra-cli in the cassandra distribution's bin directory provides simple get / set / list operations and depends on Java
The cqlsh shell which provides CQL access to cassandra, this depends on Python
A higher level interface such as Apollo

Upvotes: 0

lorcan
lorcan

Reputation: 3300

Your code looks ok on first blush. The error suggests that Cassandra is not actually running on port 9042 (the default) on IPs "192.168.1.3","192.168.1.2"

If Cassandra is running on those IPs but it's another port you will need to use

int port = 19042; // Put the correct port here
cluster = Cluster.builder().addContactPoints("192.168.1.3","192.168.1.2").withPort(port).build()

Upvotes: 3

Related Questions