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(); }
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"); }
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With