简体   繁体   中英

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

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

Go to your Apache Cassandra conf directory and enable the binary protocol

Cassandra binary protocol The Java driver uses the binary protocol that was introduced in Cassandra 1.2. It only works with a version of Cassandra greater than or equal to 1.2. Furthermore, the binary protocol server is not started with the default configuration file in Cassandr a 1.2. You must edit the cassandra.yaml file for each node:

start_native_transport: true

Then restart the node.

I was also having same problem. I have installed Cassandra in a separate Linux pc and tried to connect via Window pc. I was not allowed to create the connection.

But when we edit cassandra.yaml, set my linux pc ip address to rpc_address and restart, it allows me to connect successfully,

# The address or interface to bind the Thrift RPC service and native transport
# server to.
#
# Set rpc_address OR rpc_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
#
# Leaving rpc_address blank has the same effect as on listen_address
# (i.e. it will be based on the configured hostname of the node).
#
# Note that unlike listen_address, you can specify 0.0.0.0, but you must also
# set broadcast_rpc_address to a value other than 0.0.0.0.
#rpc_address: localhost
rpc_address: 192.168.0.10

Just posting this for people who might have the same problem as I did, when I got that error message. Turned out my complex dependency tree brought about an old version of com.google.collections, which broke the CQL driver. Removing this dependency and relying entirely on guava solved my problem.

I was having the same issue testing a new cluster with one node.

After removing this from the Cluster builder I was able to connect:

.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("US_EAST"))

It was able to connect.

In my case this was a port issue, which I forgot to update

Old RPC port is 9160
New binary port is 9042

I too encountered this problem, and it was caused by a simple error in the statement that was being submitted.

session.prepare(null);

Obviously, the error message is misleading.

Edit

/etc/cassandra/cassandra.yaml 

and change

rpc_address to 0.0.0.0 ,broadcast_rpc_address and listen_address to ip address of the cluster.

Check below points:

i) check server ip

ii) check listening port

iii) data-stack client dependency must match the server version.

About the yaml file, latest versions has below properties enabled:

    start_native_transport: true
    native_transport_port: 9042

Assuming you have default configurations in place, check the driver version compatibility. Not all driver versions are compatible with all versions of Cassandra, though they claim backward compatibility. Please see the below link.

http://docs.datastax.com/en/developer/java-driver/3.1/manual/native_protocol/

I ran into a similar issue & changing the driver version solved my problem.

Note: Hopefully, you are using Maven (or something similar) to resolve dependencies. Otherwise, you may have to download a lot of dependencies for higher versions of the driver.

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