🚀 Cassandra CQL Tutorial for Beginners and Intermediates

beginner
25 min

🚀 Cassandra CQL Tutorial for Beginners and Intermediates

Welcome to the Cassandra CQL Tutorial! 🎯 In this comprehensive guide, we'll learn about Apache Cassandra, a powerful, open-source, and distributed database management system. We'll focus on its Query Language (CQL) and explore its features with practical examples. Let's dive in!

🔑 What is Apache Cassandra?

Apache Cassandra is a NoSQL database designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure.

📝 Why Use Cassandra?

  1. Scalability: Cassandra can scale out across multiple servers and data centers, making it ideal for handling big data.
  2. Fault Tolerance: Data is replicated across multiple nodes, ensuring data availability even in the event of hardware failures.
  3. NoSQL Advantages: Cassandra provides the flexibility of a NoSQL database, with JSON-like data models, flexible schema design, and efficient data retrieval.

💡 Installing Apache Cassandra

Follow the official Cassandra installation guide to set up your local environment.

🔑 Understanding CQL

CQL (Cassandra Query Language) is the SQL-like query language for Cassandra. While similar to SQL, there are some key differences to be aware of.

📝 Basic CQL Commands

Creating Keyspaces and Tables

CREATE KEYSPACE mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}; USE mykeyspace; CREATE TABLE users ( id UUID PRIMARY KEY, name text, age int );

Inserting Data

INSERT INTO users (id, name, age) VALUES (uuid(), 'John Doe', 30);

Retrieving Data

SELECT * FROM users WHERE id = uuid('123e4567-e89b-12d3-a456-426614174000');

Deleting Data

DELETE FROM users WHERE id = uuid('123e4567-e89b-12d3-a456-426614174000');

🔑 Advanced CQL Concepts

CQL Data Types

  • UUID
  • text (varchar)
  • int
  • bigint
  • float
  • double
  • timestamp
  • boolean
  • inet
  • blob
  • counter

Indexing

Cassandra uses secondary indexes called Materialized Views to optimize data retrieval.

Clustering Columns and Partitioning

Data in Cassandra is partitioned by a partition key. Clustering columns define the order of data within a partition.

CQL Functions and Operators

  • Mathematical Functions (SUM, AVG, MIN, MAX)
  • String Functions (UPPER, LOWER, TRIM)
  • Date and Time Functions (CURRENT_TIMESTAMP, DATEDIFF)
  • Aggregate Functions (COUNT, MIN, MAX, AVG, SUM)

📝 Quiz Time!

Quick Quiz
Question 1 of 1

What is the primary key data type used in Cassandra?

📝 Summary

In this tutorial, we've learned about Apache Cassandra and its Query Language (CQL). We've explored basic and advanced concepts, including creating keyspaces and tables, inserting, retrieving, and deleting data, as well as understanding key data types, indexing, and CQL functions.

Happy coding! 💡