Apache Cassandra using the CQL

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):

  1. Creating a Keyspace:

    A keyspace is the top-level container for data in Cassandra. It defines the replication strategy and other options.

    CREATE KEYSPACE my_keyspace WITH replication = {`class`: `SimpleStrategy`, `replication_factor`: 1};
  2. Using a Keyspace:

    Before you can perform operations on tables, you need to specify the keyspace you want to use.

    USE my_keyspace;
  3. Creating a Table:

    Tables in Cassandra are created with predefined columns and data types.

    CREATE TABLE users ( user_id UUID PRIMARY KEY, first_name TEXT, last_name TEXT, email TEXT );
  4. Inserting Data:

    You can insert data into a table using the INSERT statement.

    INSERT INTO users (user_id, first_name, last_name, email) VALUES (uuid(), `John`, `Doe`, `john@example.com`);
  5. Querying Data:

    You can query data using the SELECT statement.

    SELECT * FROM users WHERE user_id = <some_uuid>;
  6. Updating Data:

    To update data, you can use the UPDATE statement.

    UPDATE users SET email = `new_email@example.com` WHERE user_id = <some_uuid>;
  7. Deleting Data:

    You can delete data using the DELETE statement.

    DELETE FROM users WHERE user_id = <some_uuid>;
  8. Secondary Indexes:

    Creating a secondary index allows you to query based on non-primary key columns.

    CREATE INDEX ON users (email);
  9. Batch Insertion:

    You can perform multiple insertions in a batch to improve efficiency.

    BEGIN 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;
  10. Aggregations:

    Cassandra supports limited aggregations like COUNT, SUM, AVG, etc.

    SELECT 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.

Related Articles

Leave a Comment: