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:
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.
To interact with Cassandra, we'll use its built-in query language, CQL. Here's how to connect to a Cassandra cluster:
cqlsh tool (Cassandra Query Language Shell).cqlsh tool and connect to your Cassandra cluster:$ 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.Now that we're connected, let's explore some essential CQL commands:
A keyspace acts as a container for tables. Here's how to create a keyspace and a table:
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.
To insert data into our users table, we can use the INSERT command:
INSERT INTO users (id, name, email) VALUES (uuid(), 'John Doe', 'john.doe@example.com');We can retrieve data from our table using the SELECT command:
SELECT * FROM users WHERE id = uuid('your-uuid-here');📝 Note: Replace 'your-uuid-here' with the UUID of the user you want to query.
To delete data, we can use the DELETE command:
DELETE FROM users WHERE id = uuid('your-uuid-here');Update a user's email using the UPDATE command:
UPDATE users SET email = 'new_email@example.com' WHERE id = uuid('your-uuid-here');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! 🎉