Cassandra CQL Reference

beginner
16 min

Cassandra CQL Reference

Welcome to our comprehensive guide on Cassandra CQL! In this tutorial, we'll dive deep into the world of NoSQL databases, focusing on Apache Cassandra and its query language, CQL (Cassandra Query Language). By the end of this lesson, you'll be well-equipped to work with Cassandra in your projects.

🎯 Key Takeaways:

  • Understand the basics of NoSQL databases and why Cassandra is a popular choice
  • Learn CQL syntax and essential commands for data manipulation
  • Gain hands-on experience with practical examples and real-world applications

What is Cassandra?

Apache Cassandra is an open-source, distributed, NoSQL database management system designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure.

Why Cassandra?

  • Scalability: Cassandra can horizontally scale to hundreds (or thousands) of nodes, making it perfect for handling big data.
  • Performance: Cassandra boasts high throughput and low latency, making it suitable for real-time data processing.
  • Fault Tolerance: Cassandra's replication and partitioning strategies ensure data consistency even in case of hardware failures.

Getting Started with CQL

To interact with Cassandra, we'll use its built-in query language, CQL. Here's how to connect to a Cassandra cluster:

  1. Install the cqlsh tool (Cassandra Query Language Shell).
  2. Start the cqlsh tool and connect to your Cassandra cluster:
bash
$ cqlsh Connected to Cassandra cluster: TestCluster at localhost:9042 [cqlsh 6.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Node/Version Local: 616439404e2179346451106e5d497f87/3.11.6] Use HELP for help.

Essential CQL Commands

Now that we're connected, let's explore some essential CQL commands:

Creating Keyspaces and Tables

A keyspace acts as a container for tables. Here's how to create a keyspace and a table:

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

💡 Pro Tip: Use UUID for primary keys for easy distribution across nodes.

Inserting Data

To insert data into our users table, we can use the INSERT command:

cql
INSERT INTO users (id, name, email) VALUES (uuid(), 'John Doe', 'john.doe@example.com');

Querying Data

We can retrieve data from our table using the SELECT command:

cql
SELECT * FROM users WHERE id = uuid('your-uuid-here');

📝 Note: Replace 'your-uuid-here' with the UUID of the user you want to query.

Deleting Data

To delete data, we can use the DELETE command:

cql
DELETE FROM users WHERE id = uuid('your-uuid-here');

Updating Data

Update a user's email using the UPDATE command:

cql
UPDATE users SET email = 'new_email@example.com' WHERE id = uuid('your-uuid-here');

Quiz

Quick Quiz
Question 1 of 1

Which command is used to create a keyspace in Cassandra?

With these essential CQL commands under your belt, you're well on your way to mastering Apache Cassandra. Stay tuned for more advanced topics and real-world examples in our upcoming lessons! 🚀

Happy coding! 🎉