Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch insertion using object mapper in Cassandra

Using cassandra object mapper api wanted to do a batch persist.

for single object it's working fine.

Mapper<MyObj> mapper = new MappingManager(getSession()).mapper(MyObj.class);
mapper.save(myObj);

For batch update I tried in this way, but ideally Cassandra is thinking I am persisting a list so it's giving exception like @Table annotation was not found in List, which is the expected behavior

Mapper<List<MyObj>> mapper = new MappingManager(getSession()).mapper(List.class);
myObjList.add(myObj1)
myObjList.addd(myObj2)
mapper.save(myObjList);

Is there a way to achieve the same in cassandra using batch?

NOTE: Alternatively I can do this by repeating the first step using for loop, but I am looking for a Batch insertions @ObjectMapper.

like image 844
lambodar Avatar asked Dec 10 '22 17:12

lambodar


1 Answers

Here is the snippet that I wanted to summarize form @adutra discussion in the above thread.

You can take the advantage of BatchStatement which is coming with cassandra-core driver and it's very much compatible with cassandra-mapper.

Mapper<MyObj> mapper = new MappingManager(getSession()).mapper(MyObj.class);
BatchStatement batch = new BatchStatement();
batch.add(mapper.saveQuery(myObj1));
batch.add(mapper.saveQuery(myObj2));
session.execute(batch);
like image 126
lambodar Avatar answered Dec 30 '22 18:12

lambodar