【问题标题】:How to connect to Apache Cassandra with JDBC?如何使用 JDBC 连接到 Apache Cassandra?
【发布时间】:2014-12-20 07:02:43
【问题描述】:

我正在尝试使用 JDBC 连接从 Java 代码连接到 Cassandra。这是我正在使用的罐子

现在这是我在 Stackoverflow 中找到的代码:

String serverIP = "localhost";
String keyspace = "mykeyspace";

Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();

Session session = cluster.connect(keyspace);

String cqlStatement = "SELECT * FROM users";




for (Row row : session.execute(cqlStatement)) {
      System.out.println(row.toString());
    }

但不幸的是它抛出了以下异常:

log4j:WARN No appenders could be found for logger (com.datastax.driver.core.Cluster).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NoSuchMethodError: org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder.<init>(IIIIIZ)V
    at com.datastax.driver.core.Frame$Decoder.<init>(Frame.java:130)
    at com.datastax.driver.core.Connection$PipelineFactory.getPipeline(Connection.java:795)
    at org.jboss.netty.bootstrap.ClientBootstrap.connect(ClientBootstrap.java:212)
    at org.jboss.netty.bootstrap.ClientBootstrap.connect(ClientBootstrap.java:188)
    at com.datastax.driver.core.Connection.<init>(Connection.java:93)
    at com.datastax.driver.core.Connection$Factory.open(Connection.java:432)
    at com.datastax.driver.core.ControlConnection.tryConnect(ControlConnection.java:216)
    at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:171)
    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.mabsisa.resources.Demo.main(Demo.java:28)

我在互联网上搜索这种异常情况。但我查到的资料不多。请帮我解决这个问题,因为我需要尽早解决这个问题...

【问题讨论】:

    标签: cassandra jboss netty cqlsh nosql


    【解决方案1】:

    我认为问题出在您使用的 netty 版本上。您使用的是 netty 2.3.0 版本,并且在该版本中使用的是类

    org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder

    没有 cassandra 驱动程序需要的构造函数。在 maven 仓库中,cassandra 驱动核心依赖于 netty 的 3.9.0.FINAL 版本:

    http://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core/2.0.2

    所以,请尝试更新您的 netty 版本。

    【讨论】:

      【解决方案2】:

      确保您的最终构建中没有两个版本的 netty。 我有同样的问题,我有两个版本的 netty 3.2.2 和 3.9.0 ,最新的 datastax 驱动程序需要 3.9.0 。

      【讨论】:

        猜你喜欢
        • 2014-05-04
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 2016-07-10
        • 2018-12-18
        • 2015-06-15
        • 2021-05-16
        • 1970-01-01
        相关资源
        最近更新 更多