Henry Henrinson
Henry Henrinson

Reputation: 5412

Prepared statements with set of values in Cassandra

I'm using the datastax Cassandra 2.0 driver and I'm playing around with prepared and bound statements. Let's say I want to query something like this:

SELECT * FROM foo WHERE mykey IN (UUID1, UUID2, UUID3);

where UUID1, UUID2, UUID3 are UUID values. What's the programatic way of doing this using bound statements. Currently I am trying something along the lines of:

preparedStatement = session.prepare("SELECT * FROM foo WHERE vref IN ?")
boundStatement = new BoundStatement(preparedStatement)

val uuids: java.util.ArrayList[java.util.UUID] = //assume we're getting the values from somewhere
session.execute(boundStatement.bind(uuids))

This is currently returning the wrong results. Any suggestions how to properly format the query?

Upvotes: 2

Views: 4025

Answers (1)

markc
markc

Reputation: 2158

Here's an example using a simple table and text values. Using Centos 6.5 and DSE4.5.1

My Table:

DESCRIBE TABLE deleteme1 ;

CREATE TABLE deleteme1 (
  col1 text,
  col2 text,
  PRIMARY KEY ((col1))
) WITH
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.100000 AND
  gc_grace_seconds=10000 AND
  index_interval=128 AND
  read_repair_chance=0.000000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  default_time_to_live=0 AND
  speculative_retry='99.0PERCENTILE' AND
  memtable_flush_period_in_ms=0 AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'LZ4Compressor'};

My data:

cqlsh:results> select * from deleteme1 ;

 col1  | col2
-------+-------
 three | three
   one |   one
   two |   two
  four |  four

(4 rows)

My Sample test class

import java.util.ArrayList;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;


public class PrepStatement {
    Cluster cluster;
    Session session;

    public static void main(String args []){
        PrepStatement myTest = new PrepStatement();
        String node = "192.168.56.22";
        myTest.runTest(node);
        myTest.close();

    }

    private void runTest(String node){
        ArrayList<String> vals = new ArrayList<String>(2);
        vals.add("one");
        vals.add("three");

        try {
            cluster = Cluster.builder()
                    .addContactPoint(node)
                    //.withCredentials("cassandra", "cassandra") // only if you need a login
                    .build();
            session = cluster.connect();
            PreparedStatement preparedStatement = session.prepare("SELECT * FROM results.deleteme1 WHERE col1 IN ?");
            BoundStatement boundStatement = new BoundStatement(preparedStatement);
            ResultSet results = session.execute(boundStatement.bind(vals));
            for (Object result : results){
                System.out.println(result.toString());
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * cleans up the session
     */
    private void close() {  
        session.close();
        cluster.close();
    }
}

Finally.. here's the output:

Row[one, one]
Row[three, three]

Note you can only use the IN query on columns that are primary key columns as outlined on the CQL3.0 docs here: http://www.datastax.com/documentation/cql/3.0/cql/cql_reference/select_r.html

Upvotes: 1

Related Questions