使用cassandra-jdbc驱动程序从列系列中检索数据时出错

时间:2013-09-03 09:22:31

标签: java database apache cassandra

当我使用Cassandra-JDBC驱动程序从列族中检索数据时。我收到了错误

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cassandra.thrift.Cassandra$Client.execute_cql3_query(Ljava/nio/ByteBuffer;Lorg/apache/cassandra/thrift/Compression;Lorg/apache/cassandra/thrift/ConsistencyLevel;)Lorg/apache/cassandra/thrift/CqlResult;

at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:447)
at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:472)
at org.apache.cassandra.cql.jdbc.CassandraStatement.doExecute(CassandraStatement.java:161)
at org.apache.cassandra.cql.jdbc.CassandraStatement.executeQuery(CassandraStatement.java:226)
at CassandraJDBCTest.main(CassandraJDBCTest.java:19)

代码是

public static void main (String args[]) throws SQLException{

    try {
        Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
    Connection con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/TestExample");
        Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT Name,Age FROM Users WHERE keyname='001';");

     rs.next();


        System.out.println(rs.getString("Name"));


       System.out.println(rs.getInt(2));
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

1 个答案:

答案 0 :(得分:0)

您缺少依赖项:
线程“main”中的异常java.lang。 NoSuchMethodError

我感觉类路径没有正确配置(为什么L's?):

Ljava/nio/ByteBuffer;
Lorg/apache/cassandra/thrift/Compression;
Lorg/apache/cassandra/thrift/ConsistencyLevel;
...

如果你想保存dependency hell尝试使用maven,那么Datastax java驱动程序有maven central repo,你只需要包含依赖项:

<dependency>
   <groupId>com.datastax.cassandra</groupId>
   <artifactId>cassandra-driver-core</artifactId>
   <version>1.0.2</version>
</dependency>

修改 对不起,没有意识到你正在使用JDBC。 cassandra jdbc驱动程序还有got a maven repository

<dependency>
<groupId>org.apache-extras.cassandra-jdbc</groupId>
<artifactId>cassandra-jdbc</artifactId>
<version>1.2.5</version>
</dependency>