cassnadra multi/single partition batch explanation

Viewed 110

I red the cassandra docs about Good use of BATCH statement - single partition batch example I want to understand about multi/single partition batch.

According to the docs this is a single partition batch.

CREATE TABLE cycling.cyclist_expenses ( 
  cyclist_name text, 
  balance float STATIC, 
  expense_id int, 
  amount float, 
  description text, 
  paid boolean, 
  PRIMARY KEY (cyclist_name, expense_id) 
);

BEGIN BATCH

  INSERT INTO cycling.cyclist_expenses (cyclist_name, expense_id, amount, description, paid) VALUES ('Vera ADRIAN', 2, 13.44, 'Lunch', true);

  INSERT INTO cycling.cyclist_expenses (cyclist_name, expense_id, amount, description, paid) VALUES ('Vera ADRIAN', 3, 25.00, 'Dinner', false);

...
APPLY BATCH;

First partition is - 'Vera ADRIAN', 2 Second partition - 'Vera ADRIAN', 3

Could u explain pls why is it single partition batch?

In another docs I found the example of multi partition batch:

Create table shopping_chart 
(cart_id UUID,item_id UUID,price Decimal, total Decimal static,
primary key ((cart_id),item_id));

insert into shopping_chart(cart_id,item_id,price,total) 
values (ABC12345,ABCITEM12345,0.01,0.01);

Begin Batch
insert into shopping_chart(cart_id,item_id,price) values ( ABC12345,ABCITEM123451,1.00);

insert into shopping_chart(cart_id,item_id,price) values ( ABC12345,ABCITEM1234512,2.00);

Update …. cart_id=ABC12345 IF total =0.01;
Apply Batch;

And I can’t understand why it's a multi partition batch? Could u pls explain ? There is working only with one partition = ABC12345

2 Answers

First partition is - 'Vera ADRIAN', 2 Second partition - 'Vera ADRIAN', 3

Could u explain pls why is it single partition batch?

Sure. Because the expense_id is not part of the partition key. Therefore, Vera ADRIAN is the same partition key value used in both INSERTs.

For the 2nd part of your question, you're right in that the 2nd example does not appear to be a multi-partition query as the cart_ids are the same. Following your link above, I quickly found a bad use of BATCH (multi-partition): https://docs.datastax.com/en/dse/6.8/cql/cql/cql_using/useBatchBadExample.html

The single-partition batch is when your queries are targeting the same partitions - in this case, Cassandra packs all queries into a single operation (also called "mutation").

The description of second example is incorrect - it's still single-partition batch.

Related