首页 文章

使用DataStax Java驱动程序1.0.4使用CQL连接到Cassandra时出现异常

提问于
浏览
4

我在我的笔记本电脑上运行了Cassandra 1.2.11 . 我可以使用 nodetoolcqlsh 连接到它但是当我尝试使用DataStax 1.0.4 Java API使用CQL 3.0进行连接时,我收到以下错误:

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1 ([localhost/127.0.0.1] Unexpected error during transport initialization (com.datastax.driver.core.TransportException: [localhost/127.0.0.1] Channel has been closed)))
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:186)

我使用以下代码进行连接,取自DataStax文档 . 我已经尝试了几个端口号,包括留下 withPort() 呼叫,但似乎没有任何工作 .

Cluster cluster = new Cluster.Builder()
        .addContactPoints("localhost")
        .withPort(9160)
        .build();

使用 telnet 我可以验证Cassandra服务器肯定正在侦听我指定的每个端口 . 我还验证了所有必需的库jar文件都在我的类路径中,如文档中所述 .

3 回答

  • 3

    事实证明我错过了documentation中的一个部分 .

    我使用的是早期版本的Cassandra中的旧 cassandra.yaml 配置文件,但它没有启用Native Transport二进制协议 . 以下代码段显示了我需要更改的设置:

    # Whether to start the native transport server.
    start_native_transport: true
    # port for the CQL native transport to listen for clients on
    native_transport_port: 9042
    

    重新启动Cassandra后,客户端能够成功连接并运行CQL 3.0命令 . 请注意,必须更改创建连接的代码以使用文件中指定的端口,如下所示:

    Cluster cluster = new Cluster.Builder()
            .addContactPoints("localhost")
            .withPort(9042)
            .build();
    

    另请注意,从Cassandra 1.2.5及更高版本,默认情况下启用本机传输 .

  • 0

    尝试使用旧版本的java驱动程序和较新的cassandra版本时,也会出现此错误 .

  • 11

    我在Ubuntu中遇到了同样的问题 . 我最近安装了以下命令,这是datastax指南

    apt-get install cassandra = 2.0.11 dsc20 = 2.0.11-1

    在此之前,我只使用了

    apt-get install cassandra

    那对我来说也是同样的问题 . 我刚删除然后用第一个命令(datastax指南)安装它 .

    附:要删除cassandra,请使用以下命令

    apt-get purge cassandra

相关问题