Apache Cassandra using the CQL

Category : Cassandra | Sub Category : Cassandra | By Prasad Bonam Last updated: 2023-08-19 13:44:30 Viewed : 326


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.

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

    cql
    USE my_keyspace;
  3. Creating a Table:

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

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

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

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

    To update data, you can use the UPDATE statement.

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

    You can delete data using the DELETE statement.

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

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

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

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

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

Search
Sub-Categories
Related Articles

Leave a Comment: