Cassandra 2.1.0 - Batch inserts into multiple tables

1,260 views
Skip to first unread message

Dave Decicco

unread,
Feb 18, 2015, 5:07:34 PM2/18/15
to csharp-dr...@lists.datastax.com
Good afternoon,

I'm trying to figure out the best way to provide transactional like insertions into multiple tables in Cassandra.
I have a scenario where I need 5 inserts to occur into 5 different tables, with different partition keys, and would like all statements to be inserted or none of the statements inserted.

I have read that the batch statement can be used but the examples I've seen insert to the same table with the same partition key for all the insertions.
Can I use a batch statement to perform 5 inserts into 5 different tables even if the partition keys of those tables differ?  I have also seen some examples online where the session.ExecuteAsync() is used but I don't believe I can use that as I'm using .NET Framework 4.0.


Is it safe to use the batch statement in this scenario?  I realize there will be a performance hit if the partition keys differ but I'd like to make sure that all 5 statements are executed.
I'm looking for the some best practices recommendations when the partition keys of the tables differ.

Thank you for your time,

Dave Decicco

Jorge Bay Gondra

unread,
Feb 19, 2015, 3:12:02 AM2/19/15
to csharp-dr...@lists.datastax.com
Hi Dave,
For the scenario you describe, you should use batch statements.
Executing a logged batch is an atomic operation: no risk of partially applied batches. Batches can be used for different partitions and column families.


//Prepare the statements involved in a profile update once
var profileStmt = session.Prepare("UPDATE user_profiles SET email=? WHERE key=?");
var userTrackStmt = session.Prepare("INSERT INTO user_track (key, text, date) VALUES (?, ?, ?)");
//...you should reuse the prepared statements
//Bind the parameters and add the statement to the batch batch
var batch = new BatchStatement()
  .Add(profileStmt.Bind(emailAddress, "hendrix"))
  .Add(userTrackStmt.Bind("hendrix", "You changed your email", DateTime.Now));
//Execute the batch
session.Execute(batch);

Jorge



To unsubscribe from this group and stop receiving emails from it, send an email to csharp-driver-u...@lists.datastax.com.

Dave Decicco

unread,
Feb 19, 2015, 8:54:30 AM2/19/15
to csharp-dr...@lists.datastax.com
Thank you for the quick response Jorge.
I will take a look.  I have read that there is a 30% performance hit when logged batches are performed.
Is this correct?  or does it depend on the number of different partitions in the batch statements?
Reply all
Reply to author
Forward
0 new messages