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

Saurabh Deshpande picture Saurabh Deshpande · May 28, 2013 · Viewed 49.2k times · Source

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

Answer

Bharadwaj picture Bharadwaj · Jun 25, 2013

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