Category : Cassandra | Sub Category : Cassandra | By Prasad Bonam Last updated: 2023-08-19 19:14:30 Viewed : 47
Here are a few examples of common operations you can perform with Apache Cassandra using the CQL (Cassandra Query Language):
Creating a Keyspace:
A keyspace is the top-level container for data in Cassandra. It defines the replication strategy and other options.
cqlCREATE KEYSPACE my_keyspace WITH replication = {`class`: `SimpleStrategy`, `replication_factor`: 1};
Using a Keyspace:
Before you can perform operations on tables, you need to specify the keyspace you want to use.
cqlUSE my_keyspace;
Creating a Table:
Tables in Cassandra are created with predefined columns and data types.
cqlCREATE TABLE users ( user_id UUID PRIMARY KEY, first_name TEXT, last_name TEXT, email TEXT );
Inserting Data:
You can insert data into a table using the INSERT
statement.
cqlINSERT INTO users (user_id, first_name, last_name, email) VALUES (uuid(), `John`, `Doe`, `john@example.com`);
Querying Data:
You can query data using the SELECT
statement.
cqlSELECT * FROM users WHERE user_id = <some_uuid>;
Updating Data:
To update data, you can use the UPDATE
statement.
cqlUPDATE users SET email = `new_email@example.com` WHERE user_id = <some_uuid>;
Deleting Data:
You can delete data using the DELETE
statement.
cqlDELETE FROM users WHERE user_id = <some_uuid>;
Secondary Indexes:
Creating a secondary index allows you to query based on non-primary key columns.
cqlCREATE INDEX ON users (email);
Batch Insertion:
You can perform multiple insertions in a batch to improve efficiency.
cqlBEGIN BATCH INSERT INTO users (user_id, first_name, last_name) VALUES (uuid(), `Alice`, `Smith`; INSERT INTO users (user_id, first_name, last_name) VALUES (uuid(), `Bob`, `Johnson`); APPLY BATCH;
Aggregations:
Cassandra supports limited aggregations like COUNT, SUM, AVG, etc.
cqlSELECT COUNT(*) FROM users;
Remember that Cassandras data modeling is heavily influenced by your applications query patterns, so designing your schema to match your query requirements is crucial for optimal performance. Additionally, these are just basic examples, and Cassandra offers more advanced features for dealing with complex data models, clustering, replication strategies, and more. Always refer to the official documentation for detailed information and best practices.