使用cassandra-jdbc-1.2.1 jar创建表时出错

使用cassandra-jdbc-1.2.1 jar创建表时出错,jdbc,cassandra,cassandra-jdbc,Jdbc,Cassandra,Cassandra Jdbc,我在通过cassandra jdbc驱动程序在cassandra中创建列族(表)时遇到了一些困难 cql命令在cqlsh中正常工作,但在使用cassandrajdbc时不能正常工作。我怀疑这与我定义连接字符串的方式有关。任何帮助都会大有帮助 让我试着解释一下我做了什么 我已经通过以下命令使用cqlsh创建了一个键空间 CREATE KEYSPACE authdb WITH REPLICATION = { 'class' : 'Simpl

我在通过cassandra jdbc驱动程序在cassandra中创建列族(表)时遇到了一些困难

cql命令在cqlsh中正常工作,但在使用cassandrajdbc时不能正常工作。我怀疑这与我定义连接字符串的方式有关。任何帮助都会大有帮助

让我试着解释一下我做了什么

我已经通过以下命令使用cqlsh创建了一个键空间

CREATE KEYSPACE authdb WITH 
       REPLICATION = { 
                     'class' : 'SimpleStrategy', 
                     'replication_factor' : 1 
                     };
这是根据以下文件:

我可以使用

 CREATE TABLE authdb.users(
    user_name varchar PRIMARY KEY,
    password varchar,
    gender varchar,
    session_token varchar,
    birth_year bigint
    );
这是正确的

当我尝试使用cassandra-jdbc-1.2.1.jar创建表时,问题就开始了

我使用的代码是:

public static void createColumnFamily()  {
    try {
        Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
        Connection con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/authdb?version=3.0.0");

        String qry = "CREATE TABLE authdb.users(" +
            "user_name varchar PRIMARY KEY," +
            "password varchar," +
            "gender varchar," +
            "session_token varchar," +
            "birth_year bigint" + 
            ")";

            Statement smt = con.createStatement();
            smt.executeUpdate(qry);
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
    }
使用cassandra-jdbc-1.2.1.jar时,我得到以下错误:

    main DEBUG jdbc.CassandraDriver - Final Properties to Connection: {cqlVersion=3.0.0, portNumber=9160, databaseName=authdb, serverName=localhost}
    main DEBUG jdbc.CassandraConnection - Connected to localhost:9160 in Cluster 'authdb' using Keyspace 'Test Cluster' and CQL version '3.0.0'
    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)
    main DEBUG jdbc.CassandraDriver - Final Properties to Connection: {cqlVersion=3.0.0, portNumber=9160, databaseName=authdb, serverName=localhost}
    main INFO  jdbc.CassandraConnection - Connected to localhost:9160 using Keyspace authdb and CQL version 3.0.0
    java.sql.SQLSyntaxErrorException: Cannot execute/prepare CQL2 statement since the CQL has been set to CQL3(This might mean your client hasn't been upgraded correctly to use the new CQL3 methods introduced in Cassandra 1.2+).
注意:群集和密钥空间不正确

使用cassandra-jdbc-1.1.2.jar时,我得到以下错误:

    main DEBUG jdbc.CassandraDriver - Final Properties to Connection: {cqlVersion=3.0.0, portNumber=9160, databaseName=authdb, serverName=localhost}
    main DEBUG jdbc.CassandraConnection - Connected to localhost:9160 in Cluster 'authdb' using Keyspace 'Test Cluster' and CQL version '3.0.0'
    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)
    main DEBUG jdbc.CassandraDriver - Final Properties to Connection: {cqlVersion=3.0.0, portNumber=9160, databaseName=authdb, serverName=localhost}
    main INFO  jdbc.CassandraConnection - Connected to localhost:9160 using Keyspace authdb and CQL version 3.0.0
    java.sql.SQLSyntaxErrorException: Cannot execute/prepare CQL2 statement since the CQL has been set to CQL3(This might mean your client hasn't been upgraded correctly to use the new CQL3 methods introduced in Cassandra 1.2+).

注意:在本例中,集群和键空间似乎是正确的。

使用1.2.1 jar时出现错误是因为您使用的是旧版本的cassandra thrift jar。您需要将其与cassandra jdbc版本保持同步。cassandra thrift jar位于二进制下载的lib目录中。

Brilliant-谢谢。我使用位于的maven存储库添加了cassandra thrift jar。我使用的是Cassandra 1.2.2,Cassandra节约罐版本是1.2.2。我在声明maven依赖项时使用了最新的版本号。org.apache.cassandra cassandra节俭1.2.2