DataStax Academy FAQ

DataStax Academy migrated to a new learning management system (LMS) in July 2020. We are also moving to a new Cassandra Certification process so there are changes to exam bookings, voucher system and issuing of certificates.

Check out the Academy FAQ pages for answers to your questions:


question

chandrasekar.b03_190734 avatar image
chandrasekar.b03_190734 asked ·

What is the difference between "BatchStatement" and "BatchQuery" in C*?

I have already asked a question about creating Batch Statements in C* python. https://community.datastax.com/questions/7313/how-to-use-cassandra-batch-statements-in-python.html?childToView=7338#comment-7338. I got an answer to use BatchQuery. I found very difficult to code using that.

I have then searched datastax docs for an alternate command and I found BatchStatement in C* package. So what is the difference between BatchQuery and BatchStatement?

Which one is good for querying CRUD op during denormalization? Will that be useful when deploying applications like SpaceCraft application on week 3 exercise?

If not, which approach will be useful for denormalization when deploying application ?

python driverbatch
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

bettina.swynnerton avatar image
bettina.swynnerton answered ·

Hi @chandrasekar.b03_190734,

the difference between BatchStatement and BatchQuery applies to the Python driver. They both write C* batch statements, logged and unlogged.

cqlengine is the Cassandra CQL 3 Object Mapper packaged with the later versions of the Datastax Python driver.

cqlengine supports batch queries using the BatchQuery class.

In order to use this BatchQuery class, you will need to implement the Object Mapper.

See here how to get started with that:

https://docs.datastax.com/en/developer/python-driver/3.24/object_mapper/#getting-started


However, you can also create batches by using the BatchStatement, imported from cassandra.query. This does not need the Object Mapper.

This is the method we are using the in the Python Getting Started with Astra backend, by the way, see here for an example:

https://github.com/DataStax-Academy/cassandra-workshop-series/blob/master/week4-AppDev-api/getting-started-with-astra-python/dao/spacecraft_temperature_dao.py

For the API reference, see here:

https://docs.datastax.com/en/developer/python-driver/3.24/api/cassandra/query/#cassandra.query.BatchStatement

The batch type is by default BatchType.LOGGED.


To understand the difference between logged and unlogged batches, please also refer to the cql documentation re batches, and please note the performance caveats. While batches seem convenient, they can be detrimental for performance when multiple partitions are involved.

https://docs.datastax.com/en/cql-oss/3.x/cql/cql_reference/cqlBatch.html

Also have a look at some other questions re batch statements here, for example:

https://community.datastax.com/questions/5002/batch-operation-in-cassandra.html


I hope this helps!

1 comment Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Yes, now I totally understood what are batches and how they may affect the performance during multiple partition.

Thanks bettina for this help.

0 Likes 0 · ·