cql - cassandra java binding a collection of maps to a prepared statement -
i using datastax' cassandra java driver. cassandra supports cql 2 only.
i have prepared insert statement needs accept list of maps.
this schema:
create type test.snap_guid ( l bigint, h bigint ); create table test.t ( id bigint, snap list<frozen<snap_guid>>, primary key ((id)) )
this prepared statement:
preparedstatement mysttmt = <client>.prepare("insert test.t (id, snap) values (?, ?)");
i know in order bind collections prepared statement need create collection, , bind it. cannot put collection brackets in prepared query. instance (for list of text):
arraylist<string> snaps = new arraylist<>(); snaps.add("aef34gf665"); // insert test.t (id, snap) values (?, ?) boundstatement bs = mysttmt.bind(12, snaps);
my question is: how bind list of maps? how can create query next one?
insert test.t (id, snap) values (12, [{l:10,h:50}]) // know impossible create following prepared statement: insert test.t (id, snap) values (?, [{l:?,h:?}]) // list of maps has single bound variable... how??? insert test.t (id, snap) values (?, ?)
create tables:
create type my_type ( user_id text, name text, age bigint ); create table my_table_with_set_of_udt ( my_key text primary key, my_types set<frozen <my_type>> ); create index on my_table_with_set_of_udt(my_types);
create mapping classes , beans
@udt(keyspace = "my_ks", name = "my_type") public class mytype { @field(name = "user_id") private string userid; @field(name = "name") private string name; @field(name = "age") private int age; // getters, setters & constructors // equals & hashcode required set } @table(keyspace = "my_ks", name = "my_table_with_set_of_udt") public class mytablewithsetofudt { @partitionkey @column(name = "my_key") private string mykey; @column(name = "my_types") @frozenvalue // because of --> set<frozen <my_type>> private set<mytype> mytypes; // getters, setters & constructors // equals & hashcode required set } @bean public mapper<mytablewithsetofudt> mytablewithsetofudtmapper() { return new mappingmanager(cassandraclient.getsession()).mapper(mytablewithsetofudt.class); } @bean public udtmapper<mytype> mytypemapper() { return new mappingmanager(cassandraclient.getsession()).udtmapper(mytype.class); }
create prepared statements, example: addupdatestatement = cassandraclient.getsession().prepare("update my_table_with_set_of_udt set my_types = my_types + ? my_key=?;"); deleteupdatestatement = cassandraclient.getsession().prepare("update my_table_with_set_of_udt set my_types = my_types - ? my_key=?;");
bind parameters:
mytype mytype = mytype(userid, name, age); udtvalue value = mytypemapper.toudt(mytype); cassandraclient.getsession().execute(addupdatestatement.bind(sets.newhashset(value), mykey));
or
cassandraclient.getsession().execute(deleteupdatestatement.bind(sets.newhashset(value), mykey));
insert record table value 'my_key' (you can directly in cql)
- run code, can add/delete elements set updating table our udt.
Comments
Post a Comment