我在通过cassandra-jdbc驱动程序在cassandra中创建列族(表)时遇到了一些困难。
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
);这可以正常工作。
当我尝试使用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)注意:集群和密钥空间不正确
当使用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 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+).注意:在这种情况下,cluster和keyspace似乎是正确的。
发布于 2013-03-13 15:39:03
使用1.2.1JAR时出现错误是因为您使用的是cassandra-thrift jar的旧版本。您需要使其与cassandra-jdbc版本保持同步。cassandra-thrift jar位于二进制文件下载的lib目录中。
https://stackoverflow.com/questions/15373462
复制相似问题