无法连接到本地主机上的 Cassandra 集群 运行

Unable to connect to Cassandra cluster running on local host

我设置了 datastax cassandra 服务并创建了一个键空间,我的数据库 运行 正常。

以下是 nodetool status 命令的输出:

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

此外,创建的数据具有键空间。

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

greetings

cqlsh:axiaglobal> select * from greetings;

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

现在,当我尝试通过 Java 连接到 cassandra 时,出现以下异常:

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)

调用Cassandra的一段代码如下:

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

     }
    }

我无法理解哪里出了问题以及为什么我的连接无法连接到 cassandra 有人可以帮我解决吗?

我已尝试设置以下内容:

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

您是否检查过您的 Cassandra 版本是否具有正确的 java 驱动程序? 您使用的是什么版本的驱动程序和 Cassandra?

点这里 https://docs.datastax.com/en/developer/driver-matrix/doc/javaDrivers.html