我已经设置了datastax cassandra服务并创建了一个密钥空间,我的数据库运行正常。
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();
}
}
}
我无法理解我哪里出错了,为什么我的连接无法连接到cassandra 有人可以帮助我吗?
我尝试过设置以下内容: rpc_address:0.0.0.0 broadcast_rpc_address:1.2.3.4甚至它都不起作用。
答案 0 :(得分:2)
您是否检查过您的Cassandra版本是否有正确的Java驱动程序? 您使用的是什么版本的驱动程序和Cassandra?
点击这里 https://docs.datastax.com/en/developer/driver-matrix/doc/javaDrivers.html