我正在使用cassandra-unit尝试嵌入式Cassandra,并遇到以下异常,
com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1 (com.datastax.driver.core.ConnectionException: [/127.0.0.1] Unexpected error during transport initialization (com.datastax.driver.core.TransportException: [/127.0.0.1] Unexpected exception triggered (java.io.IOException: Connection reset by peer))))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:195)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:78)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1029)
at com.datastax.driver.core.Cluster.init(Cluster.java:120)
at com.datastax.driver.core.SessionManager.init(SessionManager.java:61)
at com.datastax.driver.core.SessionManager.execute(SessionManager.java:416)
at com.datastax.driver.core.SessionManager.executeQuery(SessionManager.java:453)
at com.datastax.driver.core.SessionManager.executeAsync(SessionManager.java:103)
at com.datastax.driver.core.SessionManager.execute(SessionManager.java:91)
at com.datastax.driver.core.SessionManager.execute(SessionManager.java:83)
at com.me.cc.bma.cassandra.test.main(test.java:30)
对于以下代码,
import com.datastax.driver.core.Cluster;
import org.cassandraunit.CQLDataLoader;
import org.cassandraunit.dataset.CQLDataSet;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.apache.cassandra.config.DatabaseDescriptor;
import com.datastax.driver.core.ResultSet;
import static com.datastax.driver.core.Cluster.builder;
import com.datastax.driver.core.Session;
public class Test{
static Session session;
public static void main(String args[]) throws Exception {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(500000L);
Cluster.Builder clusterBuilder = builder().addContactPoint("127.0.0.1").withClusterName(EmbeddedCassandraServerHelper.getClusterName()).withPort(7010);
Cluster cluster = clusterBuilder.build();
session = cluster.newSession();
try {
session.execute("CREATE KEYSPACE ci WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
session = cluster.connect("ci");
session.execute("CREATE TABLE login (l_id int)");
session.execute("insert into login(l_id) values(1)");
ResultSet res = session.execute("select * from login");
System.out.println(res.all());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
版本:
任何修复将不胜感激!
答案 0 :(得分:2)
一段时间以前,我遇到了同样的问题,发现工件版本由于Maven中的冲突而不正确且不兼容。
我正在使用以下版本,并且对我来说效果很好:
Cassandra-all:2.1.14 卡桑德拉单位:2.1.9.2 Cassandra驱动程序核心:3.3.2
还有一种很酷的方法,可以使用@EmbeddedCassandra Annotation启动嵌入式Cassandra,它可以为您完成基础工作。
希望这对您也有用。
答案 1 :(得分:1)
将默认的cassndra本机传输端口更改为9142。
Cluster.builder().addContactPoint("127.0.0.1").withClusterName(EmbeddedCassandraServerHelper.getClusterName()).withPort(9142);
还在创建表查询中指定PRIMARY KEY