DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • SQL Data Manipulation Language (DML) Operations: Insert, Update, Delete
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]
  • Schema Change Management Tools: A Practical Overview
  • Navigating the Divide: Distinctions Between Time Series Data and Relational Data

Trending

  • The Future of Java: Virtual Threads in JDK 21 and Their Impact
  • Optimizing Generative AI With Retrieval-Augmented Generation: Architecture, Algorithms, and Applications Overview
  • Unlocking the Power of Streaming: Effortlessly Upload Gigabytes to AWS S3 With Node.js
  • Performance of ULID and UUID in Postgres Database
  1. DZone
  2. Data Engineering
  3. Databases
  4. Incrementing Data With Cassandra Counters

Incrementing Data With Cassandra Counters

Cassandra's counters store integers that you can expect to change in regular increments. This overview covers setting them up and using CRUD operations on them.

Piyush Rana user avatar by
Piyush Rana
·
Jan. 06, 17 · Tutorial
Like (2)
Save
Tweet
Share
37.0K Views

Join the DZone community and get the full member experience.

Join For Free

A counter is a special column used to store an integer that is changed in increments. Counters are useful for many data models, such as:

  • Keeping track of the number of web page views received on a company website

  • Keeping track of the number of games played online or the number of players who have joined an online game.

So, let's start exploring counters. First,  table creation:

CREATE TABLE WebLogs (
    page_id uuid,
    page_name Text,
    insertion_time timestamp,
    page_count counter,
    PRIMARY KEY (page_id, insertion_time)
);


Now, if you hit the create command, you will get the following error:

ERROR: – InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot mix counter and non counter columns in the same table"

Counter columns do not exist with non-counter columns, so every other column in the table should be a primary key/clustering keys. So, let's make our page_name column as a composite primary key with the page_id .

cqlsh:keyspace_test> CREATE TABLE WebLogs (
…     page_id uuid,
…     page_name Text,
…     insertion_time timestamp,
…     page_count counter,
…     PRIMARY KEY ((page_id,page_name), insertion_time)
… );


Now, let's insert some data into our table:

cqlsh:keyspace_test> insert into weblogs (page_id , page_name , insertion_time , page_count ) 
values(uuid(),’test.com’,dateof(now()),0) ;


And you might be astonished upon receiving another error:

ERROR: -InvalidRequest: Error from server: code=2200 [Invalid query] message=”INSERT statements are not allowed on counter tables, use UPDATE instead”

Well all right. Let's use some update statements: 

cqlsh:keyspace_test> update weblogs set page_count = page_count + 1 
where page_id =uuid() and page_name =’test.com’ and insertion_time =dateof(now());

cqlsh:keyspace_test> select * from weblogs;

page_id                              | page_name | insertion_time           | page_count
————————————–+———–+———————8372cee6-1d04-41f7-a70d-98fdd9036448 |  test.com | 2017-01-05 05:19:31+0000 |          1


Voila! You have successfully created your counter table and performed the insert operation.

More CRUD

Let's do some more CURD operations, like updating and deleting on the counter table.

Updating the Data

Say one more visitor visits the test.com webpage, so we need to increment the counter column.

cqlsh:keyspace_test> update weblogs set page_count = page_count + 1 
where page_id =8372cee6-1d04-41f7-a70d-98fdd9036448 and page_name =’test.com’ 
and insertion_time =’2017-01-05 05:19:31+0000′;


All the data in where clause should be the same.

cqlsh:keyspace_test> select * from weblogs ;

page_id                              | page_name | insertion_time           | page_count
————————————–+———–+——————-
8372cee6-1d04-41f7-a70d-98fdd9036448 |  test.com | 2017-01-05 05:19:31+0000 |          2


Removing the Data

This is pretty simple, as it uses Cassandra's delete command.

cqlsh:keyspace_test> delete from weblogs where page_id =8372cee6-1d04-41f7-a70d-98fdd9036448 
and page_name =’test.com’ and insertion_time =’2017-01-05 05:19:31+0000′;

cqlsh:keyspace_test> select * from weblogs;

page_id | page_name | insertion_time | page_count


Some Important Notes

  • A counter column cannot be indexed or deleted.

  • To load data into a counter column, or to increase or decrease the value of the counter, use the UPDATE command. Cassandra rejects USING TIMESTAMP or USING TTL when updating a counter column.

  • To create a table having one or more counter columns, use CREATE TABLE to define the counter and non-counter columns. Use all non-counter columns as part of the PRIMARY KEY definition.

Data (computing) Database Relational database

Published at DZone with permission of Piyush Rana, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • SQL Data Manipulation Language (DML) Operations: Insert, Update, Delete
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]
  • Schema Change Management Tools: A Practical Overview
  • Navigating the Divide: Distinctions Between Time Series Data and Relational Data

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: