CASSANDRA CQL and Data Modeling 1 — Questions and Answers
Question 1: What CQL statement is used to create a new keyspace in Cassandra?
- CREATE DATABASE
- CREATE SCHEMA
- CREATE KEYSPACE (Correct answer)
- CREATE NAMESPACE
Correct answer: CREATE KEYSPACE
CREATE KEYSPACE is the CQL command to define a new keyspace, specifying the replication strategy and factor.
Question 2: What is the difference between a partition key and a clustering key in a Cassandra primary key?
- There is no difference; they are synonyms
- The partition key determines data distribution; clustering keys determine sort order within a partition (Correct answer)
- The clustering key determines which node stores data; the partition key sorts rows
- The partition key is optional; the clustering key is mandatory
Correct answer: The partition key determines data distribution; clustering keys determine sort order within a partition
The partition key routes data to a node via consistent hashing, while clustering columns control the physical sort order of rows stored within that partition.
Question 3: Which CQL clause restricts the result set of a SELECT query to a specific partition?
- GROUP BY
- LIMIT
- WHERE partition_key = value (Correct answer)
- ORDER BY
Correct answer: WHERE partition_key = value
A WHERE clause filtering on the full partition key allows Cassandra to route the query directly to the correct node without a full cluster scan.
Question 4: What does the TTL (Time To Live) option do when inserting data in Cassandra?
- Sets a timeout for the write operation
- Automatically deletes the data after the specified number of seconds (Correct answer)
- Limits the number of reads before expiry
- Reserves space in the MemTable
Correct answer: Automatically deletes the data after the specified number of seconds
TTL marks data with an expiration time; Cassandra automatically creates a tombstone for that data once the TTL expires.
Question 5: What is a 'materialized view' in Cassandra?
- A read-only cached copy of a base table with a different primary key for alternate access patterns (Correct answer)
- A writable denormalized copy of a table managed by the user
- A view stored in memory only, not persisted to disk
- A cross-keyspace join of two tables
Correct answer: A read-only cached copy of a base table with a different primary key for alternate access patterns
A materialized view is automatically maintained by Cassandra as a separate table built from a base table, allowing efficient queries on non-primary-key columns.
Question 6: Which CQL data type should be used to store a universally unique identifier in Cassandra?
- TEXT
- BIGINT
- UUID or TIMEUUID (Correct answer)
- BLOB
Correct answer: UUID or TIMEUUID
UUID stores a random version-4 UUID while TIMEUUID stores a version-1 time-based UUID, both natively supported as primary key types in Cassandra.
What CQL statement is used to create a new keyspace in Cassandra?