3
votes

What would be the right way to build a insert statement with QueryBuilder from Datastax Java Driver for Cassandra

I am using Cassandra 2.x with Java Driver 2.0.0-rc1

I know i could use a prepared statement to achieve the same but i am looking forward to using the QueryBuilder

@Test
public void testTableInsert() {
    Insert insert = QueryBuilder
            .insertInto(KEYSPACE_NAME, TABLE_NAME)
            .value("username", "jdoe")
            .value("first", "John")
            .value("last", "Doe");
    System.out.println(insert.toString());
    ResultSet result = session.execute(insert.toString());
    System.out.println(result);

}

I can confirm the query string is valid because it succeeds when attempting manually on cqlsh

INSERT INTO test.user(username,first,last) VALUES ('jdoe','John','Doe');

The error reported by the driver is

    com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured columnfamily user
    at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35)
    at com.datastax.driver.core.ResultSetFuture.extractCauseFromExecutionException(ResultSetFuture.java:271)
    at       com.datastax.driver.core.ResultSetFuture.getUninterruptibly(ResultSetFuture.java:187)
    at com.datastax.driver.core.Session.execute(Session.java:126)
    at com.datastax.driver.core.Session.execute(Session.java:77)
    at SimpleClientTest.testTableInsert(SimpleClientTest.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured columnfamily user
    at com.datastax.driver.core.Responses$Error.asException(Responses.java:96)
    at com.datastax.driver.core.ResultSetFuture$ResponseCallback.onSet(ResultSetFuture.java:122)
    at com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:224)
    at com.datastax.driver.core.RequestHandler.onSet(RequestHandler.java:359)
    at com.datastax.driver.core.Connection$Dispatcher.messageReceived(Connection.java:510)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
    at org.jboss.netty.handler.codec.oneone.OneToOneDecoder.handleUpstream(OneToOneDecoder.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
    at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462)
    at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443)
    at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:109)
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:90)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

I couldn't find any examples on the official documentation page at here

3
Just ran your code and it works fine. Check that you haven't actually dropped the "user" CF and make sure that KEYSPACE_NAME is what you expect it to be (i.e. "test")Lyuben Todorov
@LyubenTodorov thank you for trying this out. You can see my answer as to why i was seeing this error. It boils down to the Session object not refreshing its TableMetaData which is obtained when the Session object is created.Adil F
That sounds odd, can you show me the code you are using (maybe in a gist)Lyuben Todorov
@LyubenTodorov You can just issue the create table query before the query without recreating a session. modified code at gist.github.com/adilfulara/8944508Adil F
You aren't creating the keyspace in that query, you are only creating a table. Where is the keyspace created? Does it already exist?Lyuben Todorov

3 Answers

3
votes

You just forgot to create the keyspace.

Full code used to create a KS, create a CF and use the querybuilder to insert a partition.

public static void main(String[] Args)
{
    Cluster cluster = null;
    cluster = Cluster.builder()
                     .addContactPoint("127.0.0.1")
                     .build();
    Session session = cluster.connect();

    try
    {
        String createKS = "CREATE KEYSPACE test WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': '2' }";
        session.execute(createKS);
        String query = "create table test.user (username text primary key,first text, last text);";
        session.execute(query);

        System.out.println("insert executed");
        Insert insert = QueryBuilder.insertInto("test", "user")
                                    .value("username", "jdoe")
                                    .value("first", "John")
                                    .value("last", "Doe");
        System.out.println(insert.toString());
        ResultSet result = session.execute(insert.toString());
        System.out.println(result);
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }

    System.exit(0);
}
4
votes

There is no problem in the code. The error is due to the fact that the CF being queried was created in the same session. In such a case, one needs to create a new Session to the cluster for the Session to get the TableMetaData about the CF. I just assumed that since a connection was established, the Session would update its TableMetaData before a Insert statement. Unfortunately not the case.

0
votes

I just had the same problem but arrived at a different solution. In my case I had an integration test, running multiple tests against a Cassandra instance; four tests (not using batches) succeeded flawlessly, a fifth (using batches) failed.

For some reason the CQL parser converts all names to lower-case so if you have a keyspace or column family name with mixed case letters you have to surround the name of the keyspace or column family with quotes, i.e.

QueryBuilder.insertInto(String.format("\"%s\"", COLUMN_FAMILY)).…