2
votes

I am having some difficulty creating a column family (table) in cassandra via the cassandra-jdbc driver.

The cql command works correctly in cqlsh, but doesn't when using cassandra jdbc. I suspect this is something to do with the way I have defined my connection string. Any help would be greatly helpful.

Let me try and explain what I have done.

I have created a keyspace using cqlsh with the following command

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

This is as per the documentation at: http://www.datastax.com/docs/1.2/cql_cli/cql/CREATE_KEYSPACE#cql-create-keyspace

I am able to create a table (column family) in cqlsh using

 CREATE TABLE authdb.users(
    user_name varchar PRIMARY KEY,
    password varchar,
    gender varchar,
    session_token varchar,
    birth_year bigint
    );

This works correctly.

My problems start when I try to create the table using cassandra-jdbc-1.2.1.jar

The code I use is:

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();
    }

When using cassandra-jdbc-1.2.1.jar I get the following error:

    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)

Note: the cluster and key space are not correct

When using cassandra-jdbc-1.1.2.jar I get the following error:

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

Note: in this instance the cluster and keyspace appear to be correct.

1

1 Answers

3
votes

The error when using the 1.2.1 jar is because you have an old version of the cassandra-thrift jar. You need to keep that in sync with the cassandra-jdbc version. The cassandra-thrift jar is in the lib directory of the binary download.