Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while connecting to Cassandra using Java Driver for Apache Cassandra 1.0 from com.example.cassandra

While connecting to Cassandra client using java driver for Cannsandra by DataStax, it is throwing following error..

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

Please suggest...

Thanks!

My java code is like this:

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(); } 
like image 921
Saurabh Deshpande Avatar asked May 28 '13 04:05

Saurabh Deshpande


2 Answers

In my case, I ran into this issue as I used the default RPC port of 9160 during connection. One can find a different port for CQL in cassandra.yaml -

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

Once I changed the code to use port 9042 the connection attempt succeeded -

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"); } 
like image 164
Bharadwaj Avatar answered Sep 20 '22 17:09

Bharadwaj


I had this issue and it was sorted by setting the ReadTimeout in SocketOptions:

Cluster cluster = Cluster.builder().addContactPoint("localhost").build(); cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(HIGHER_TIMEOUT); 
like image 22
Aneesh Vijendran Avatar answered Sep 23 '22 17:09

Aneesh Vijendran