apache cassandra jdbc连接错误

62lalag4  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(447)

我想通过jdbc连接到远程服务器上的Cassandra1.2.4。

package cassandraclient;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.logging.Level;
 import java.util.logging.Logger;

 public class CassClient {
 public static void main(String[] args) {
     try {

         Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");

         Connection con =  DriverManager.getConnection("jdbc:cassandra://<domain>:7199");

        String query = "CREATE KEYSPACE CassandraClientTest WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };";
        Statement statement = con.createStatement();

        boolean success = statement.execute(query);
        System.out.println("created keyspace " + success);
        statement.close();

        con.close();

    } 
    catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    catch (SQLException ex) {
        ex.printStackTrace();
    }
 }
 }

当我尝试建立一个连接时,我得到以下异常。

java.sql.SQLNonTransientConnectionException: org.apache.thrift.transport.TTransportException
        at org.apache.cassandra.cql.jdbc.CassandraConnection.<init>(CassandraConnection.java:156)
        at org.apache.cassandra.cql.jdbc.CassandraDriver.connect(CassandraDriver.java:92)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at cassandraclient.CassClient.main(CassClient.java:17)
    Caused by: org.apache.thrift.transport.TTransportException
        at org.apache.thrift.transport.TIOStreamTransport.read(TIOStreamTransport.java:132)
        at org.apache.thrift.transport.TTransport.readAll(TTransport.java:84)
        at org.apache.thrift.transport.TFramedTransport.readFrame(TFramedTransport.java:129)
        at org.apache.thrift.transport.TFramedTransport.read(TFramedTransport.java:101)
        at org.apache.thrift.transport.TTransport.readAll(TTransport.java:84)
        at org.apache.thrift.protocol.TBinaryProtocol.readAll(TBinaryProtocol.java:378)
        at org.apache.thrift.protocol.TBinaryProtocol.readI32(TBinaryProtocol.java:297)
        at org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinaryProtocol.java:204)
        at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:69)
        at org.apache.cassandra.thrift.Cassandra$Client.recv_describe_cluster_name(Cassandra.java:1101)
        at org.apache.cassandra.thrift.Cassandra$Client.describe_cluster_name(Cassandra.java:1089)
        at org.apache.cassandra.cql.jdbc.CassandraConnection.<init>(CassandraConnection.java:125)
        ... 4 more

我还注意到了其他一些事情。cassandra的标准端口是9160,我没有指定其他端口。但当我再次尝试启动Cassandra它给了我以下例外。

Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 7199; nested exception is:
            java.net.BindException: Address already in use

所以我用了7199端口。当我在url中使用端口9160(和其他端口,如9161..2)时,我得到了这个异常。

java.sql.SQLNonTransientConnectionException: org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused: connect
        at org.apache.cassandra.cql.jdbc.CassandraConnection.<init>(CassandraConnection.java:156)
        at org.apache.cassandra.cql.jdbc.CassandraDriver.connect(CassandraDriver.java:92)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at cassandraclient.CassClient.main(CassClient.java:28)
    Caused by: org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused: connect
        at org.apache.thrift.transport.TSocket.open(TSocket.java:183)
        at org.apache.cassandra.cql.jdbc.CassandraConnection.<init>(CassandraConnection.java:123)
        ... 4 more
    Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at org.apache.thrift.transport.TSocket.open(TSocket.java:178)
        ... 5 more

我会感激你的帮助。

pzfprimi

pzfprimi1#

这里发生了几件事。
最容易解决的问题:
java.rmi.server.exportexception:端口已在使用:7199;java.net.bindexception:地址已在使用中
尽管c使用端口9160进行节俭,但它也使用端口7199进行jmx。这个例外基本上意味着有别的东西阻止c使用这个端口,我的钱在另一个cassandra服务器上。
使用activity monitor/tast管理器确保您已经停止了所有cassandra示例,然后尝试重新启动服务器,如果在osx/linux上使用-f标志在前面的回合中启动服务器,这将使停止更容易!

Windows> cassandra.bat
OSX/Linux$ ./cassandra -f

下一个问题(我不确定这是一个问题还是您刚刚决定混淆连接字符串)

Connection con =  DriverManager.getConnection("jdbc:cassandra://<domain>:7199");

这是不正确的,您需要在 <domain> 部分。例如:

Connection con =  DriverManager.getConnection("jdbc:cassandra://127.0.0.1:9160");
Connection con =  DriverManager.getConnection("jdbc:cassandra://mycassndrahost:9160");

此外,您不能使用端口7199,它是jmx所需要的,并且必须是免费的,除非您更改cassandra-env.sh文件中的配置。
告诉我你进展如何。

相关问题