2013-03-12 33 views
2

我在通過cassandra-jdbc驅動程序在cassandra中創建列族(表)時遇到了一些困難。使用cassandra-jdbc-1.2.1 jar創建表時的錯誤

cql命令在cqlsh中正常工作,但在使用cassandra jdbc時不會正常工作。我懷疑這與我定義連接字符串的方式有關。任何幫助將是非常有幫助的。

讓我試着解釋我所做的。

我創建使用cqlsh用下面的命令密鑰空間

CREATE KEYSPACE authdb WITH 
     REPLICATION = { 
        'class' : 'SimpleStrategy', 
        'replication_factor' : 1 
        }; 

這是因爲每文檔:http://www.datastax.com/docs/1.2/cql_cli/cql/CREATE_KEYSPACE#cql-create-keyspace

我能夠創建一個使用

在cqlsh表(列族)
CREATE TABLE authdb.users(
    user_name varchar PRIMARY KEY, 
    password varchar, 
    gender varchar, 
    session_token varchar, 
    birth_year bigint 
    ); 

這個工作正常。當我嘗試使用卡桑德拉-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(); 
    } 

當使用卡桑德拉-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) 

注:集羣和密鑰空間是不正確的

當使用卡桑德拉-JDBC-1.1.2.jar我得到以下錯誤:

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+). 

注:在這種情況下,集羣和密鑰空間似乎是正確的。

回答

3

使用1.2.1 jar的錯誤是因爲你有cassandra-thrift jar的老版本。您需要保持與cassandra-jdbc版本同步。 cassandra-thrift jar在二進制下載的lib目錄中。

+0

輝煌 - 謝謝。 我使用http://mvnrepository.com/artifact/org.apache.cassandra/cassandra-thrift/上的maven倉庫添加了cassandra-thrift jar。 我使用Cassandra 1.2.2和cassandra-thrift jar版本是1.2.2。我在聲明maven依賴時使用了最新的版本號。 \t org.apache.cassandra \t 卡桑德拉-節儉 \t 1.2.2 Mahesh 2013-03-13 09:07:06