Unable to connect to Cassandra cluster running on local host

6.7k views Asked by At

I have setup datastax cassandra service and have created a keyspace and my db is running fine.

Below are output from the nodetool status command:

C:\Users\xxx>cd C:\Program Files\DataStax Community\apache-cassandra\bin

C:\Program Files\DataStax Community\apache-cassandra\bin>nodetool status
Datacenter: datacenter1
========================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens       Owns (effective)  Host ID                               Rack
UN  127.0.0.1  229 KB     256          100.0%            d5229669-f8f2-4b06-a887-4ab91a883a74  rack1

Also , the data is created having a keyspace.

cqlsh:axiaglobal> use axiaglobal;
cqlsh:axiaglobal> describe tables;

greetings

cqlsh:axiaglobal> select * from greetings;

 user | id | creation_date | greet
------+----+---------------+-------

Now when I try to connect to cassandra via Java I get the following exception:

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (null))
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:196)
    at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
    at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1104)
    at com.datastax.driver.core.Cluster.init(Cluster.java:121)
    at com.datastax.driver.core.Cluster.connect(Cluster.java:198)
    at com.datastax.driver.core.Cluster.connect(Cluster.java:226)
    at com.axia.global.dao.cassandra.service.CassandraApp.main(CassandraApp.java:29)

Piece of code which makes a call to Cassandra is listed below:

package com.axia.global.dao.cassandra.service;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;

import com.axia.global.model.cassandra.Person;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;

public class CassandraApp {

     private static final Logger LOG = LoggerFactory.getLogger(CassandraApp.class);

     private static Cluster cluster;
     private static Session session;

     public static void main(String[] args) {

      try {
       cluster = Cluster.builder().addContactPoints("127.0.0.1").build();

       session = cluster.connect("axiaglobal");

       CassandraOperations cassandraOps = new CassandraTemplate(session);

       Select s = QueryBuilder.select().from("greetings");
       

      } catch (Exception e) {
       e.printStackTrace();
      }

     }
    }

I am unable to understand where I am going wrong and why my connection is failing to connect to cassandra can some one help me out with it ?

I have tried setting up the following:

rpc_address: 0.0.0.0 broadcast_rpc_address: 1.2.3.4 and even it did not work.
1

There are 1 answers

0
J KVS On BEST ANSWER

Did you check you have the correct java Driver for your Cassandra version? what version of driver and Cassandra are you using?

Check here https://docs.datastax.com/en/developer/driver-matrix/doc/javaDrivers.html