Search code examples
cassandradatastax-java-driver

Cassandra Statement set KeySpace


Using Cassandra 2.2.8 with 3.0 Connector. I am trying to create a Statement with QueryBuilder. When I execute Statement it complains no keyspace defined. The only way I know to set keyspace is as below (There is no setKeyspace method in Statement). When I do a getKeySpace - I actually get null

 Statement s = QueryBuilder.select().all()
            .from("test.tests")
System.out.println("getKeyspace:"+ s.getKeyspace()); >> null

Am I doing something wrong, Is there any other (more reliable) way to setKeyspace? Thanks


Solution

  • from(String) expects a table name. While what you are doing is technically valid and cassandra will interpret it correctly, the driver is not able to derive the keyspace name in this way.

    Instead you could use from(String, String) which takes the first parameter as the keyspace.

        Statement s = QueryBuilder.select().all()
                .from("test", "tests");
        System.out.println("getKeyspace:" + s.getKeyspace()); // >> test