Cassandra DB Query Example

29.01.2025

Introduction

Cassandra is a distributed NoSQL database known for its scalability and high availability. In this article, we will explore some Cassandra DB query examples that demonstrate how to interact with the database using CQL (Cassandra Query Language).

Overview of User Defined Type (UDT) in Cassandra - GeeksforGeeks

Connecting to Cassandra

Before running any queries, you need to establish a connection to the Cassandra cluster. You can use the DataStax PHP Driver or DataStax Node.js Driver to connect to Cassandra from your PHP or JavaScript application.

Massively Scalable Time Series with Apache Cassandra – YouTube
Feb 13, 2024 … Apache Cassandra is widely known for being great for time series. To get the most out of it, you’ll want to employ a handful of best …

Creating a Keyspace

A keyspace in Cassandra is similar to a database in traditional relational databases. You can create a keyspace using the following CQL query:

CREATE KEYSPACE IF NOT EXISTS my_keyspace   WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};

Creating a Table

Tables in Cassandra hold your data. You can create a table using the following CQL query:

CREATE TABLE IF NOT EXISTS my_keyspace.my_table (   id UUID PRIMARY KEY,   name TEXT,   age INT );

Inserting Data

To insert data into a Cassandra table, you can use the following CQL query:

INSERT INTO my_keyspace.my_table (id, name, age)   VALUES (uuid(), 'Alice', 30);

Querying Data

Retrieving data from a Cassandra table involves running queries. Here are some examples of querying data:

  • SELECT * FROM my_keyspace.my_table; – Retrieves all data from the table.
  • SELECT * FROM my_keyspace.my_table WHERE name = 'Alice'; – Retrieves data based on a specific condition.

Updating Data

You can update existing data in a Cassandra table using the following CQL query:

UPDATE my_keyspace.my_table   SET age = 31   WHERE name = 'Alice';

Deleting Data

To remove data from a Cassandra table, you can use the following CQL query:

DELETE FROM my_keyspace.my_table   WHERE name = 'Alice';

Conclusion

These Cassandra DB query examples demonstrate the basic operations you can perform when interacting with a Cassandra database. By understanding how to create keyspaces, tables, insert data, query data, update data, and delete data, you can effectively work with Cassandra in your PHP or JavaScript applications.

Yan Hadzhyisky

fullstack PHP+JS+REACT developer