Cassandra-unit:java.io.IOException:连接被对等方重置

Cassandra-unit : java.io.IOException: Connection reset by peer

我正在尝试使用 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();
          }         
     }

}

版本:

任何修复将不胜感激!

将端口更改为默认的 cassandra 本机传输端口 9142。

Cluster.builder().addContactPoint("127.0.0.1").withClusterName(EmbeddedCassandraServerHelper.getClusterName()).withPort(9142);

还要在创建 table 查询中指定 PRIMARY KEY

前段时间我遇到了同样的问题,发现由于 maven 中的冲突,工件版本不正确且不兼容。

我使用的是以下版本并且对我来说工作正常:

Cassandra-all:2.1.14 卡桑德拉单元:2.1.9.2 Cassandra 驱动核心:3.3.2

还有一种很酷的方法可以使用@EmbeddedCassandra Annotation 来启动嵌入式 Cassandra,它可以为您完成基础工作。

希望这对你也有用。