cql - Cassandra BoundStatement with Multiple Parameters and Multi-Partition Query -


after reading "asynchronous queries java driver" article in datastax blog, trying implement solution similar 1 in section called - 'case study: multi-partition query, a.k.a. “client-side select...in“'.

i have code looks this:

public future<list<resultset>> executemultipleasync(final boundstatement statement, final object... partitionkeys) {     list<future<resultset>> futures = lists.newarraylistwithexpectedsize(partitionkeys.length);     (object partitionkey : partitionkeys) {       statement bs = statement.bind(partitionkey);       futures.add(executewithretry(bs));     }     return futures.successfulaslist(futures); } 

but, i'd improve on that. in cql query boundstatement holds, i'd have looks this:

select * <column_family_name> <param1> = :p1_name , param2 = :p2_name , <partiotion_key_name> = ?; 

i'd clients of method give me boundstatement bound parameters (two parameters in case) , list of partition keys. in case, need do, bind partition keys , execute queries. unfortunately, when bind key statement fail error - com.datastax.driver.core.exceptions.invalidtypeexception: invalid type value 0 of cql type varchar, expecting class java.lang.string class java.lang.long provided. problem is, try bind key first parameter , not last. string , not long.

i can solve either giving partition parameter name i'd have name via method parameters, or specifying it's index again require additional method parameter. either way, if use name or index have bind specific type. instance: bs.setlong("<key_name>", partitionkey);. reason, can't leave boundstatement interpret type of last parameter.

i'd avoid passing parameter name explicitly , bypass type problem. there can done?

thanks!

i've posted same question in 'datastax java driver apache cassandra user mailing list' , got answer saying functionality i'm missing may added in next version (2.2) of datastax java driver.

in java-721 (to introduced in 2.2) tentatively planning on adding following methods signature boundstatement:

public boundstatement setobject(int i, v v) public boundstatement setobject(string name, v v)

and

you can emulate setobject in 2.1:

void setobject(boundstatement bs, int position, object object,                protocolversion protocolversion) {      datatype type =  bs.preparedstatement().getvariables().gettype(position);      bytebuffer buffer = type.serialize(object, protocolversion);      bs.setbytesunsafe(position, buffer);  } 

to avoid passing parameter name, 1 thing position isn't bound yet:

int findunsetposition(boundstatement bs) {     int size = bs.preparedstatement().getvariables().size();     (int = 0; < size; i++)         if (!bs.isset(i))             return i;     throw new illegalargumentexception("found no unset position"); } 

i don't recommend though, because it's ugly , unpredictable if user forgot bind 1 of non-pk variables.

the way require user pass callback sets pk:

interface pkbinder<t> {     void bind(boundstatement bs, t pk); } public <t> future<list<resultset>> executemultipleasync(final boundstatement statement, pkbinder<t> pkbinder, final t... 

partitionkeys)

as bonus, work composite partition keys.


Comments