Cassandra Counters Column

Cassandra Counters Column

Instructor-svgAl-Mamun Sarkar
Sep 26 , 2020

Cassandra Counters Column only allows increment or decrement operation. In this lesson, I will show the use of the counter.

Create Table With Counter Column:

CREATE TABLE sales_by_counter (
    id uuid PRIMARY KEY, 
    sales counter 
);

Insert Data:

UPDATE sales_by_counter SET sales = sales + 1 WHERE id = uuid();
UPDATE sales_by_counter SET sales = sales + 1 WHERE id = uuid();
UPDATE sales_by_counter SET sales = sales + 1 WHERE id = uuid();

Select Data:

SELECT * FROM sales_by_counter;

Increase Counter:

UPDATE sales_by_counter 
SET sales = sales + 1 
WHERE id = f434f8cd-0e17-4d4d-9923-4603dc78233e;

Decrease Counter:

UPDATE sales_by_counter 
SET sales = sales - 1 
WHERE id = f434f8cd-0e17-4d4d-9923-4603dc78233e;

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram