Can a primary key in Cassandra contain a collection column?
Example:
CREATE TABLE person (
first_name text,
emails set<text>,
description text
PRIMARY KEY (first_name, emails)
);
A primary index is automatically created for the primary key and ensures that the primary key is unique. You can use the primary index to retrieve and access objects from the database. The unique index is a column, or an ordered collection of columns, for which each value identifies a unique row.
A primary key in Cassandra consists of one or more partition keys and zero or more clustering key components. The order of these components always puts the partition key first and then the clustering key.
Yes the primary key has to be unique. Otherwise there would be no way to know which row to return when you query with a duplicate key. In your case you can have 2 rows with the same name or with the same surname but not both.
There is no way to change a primary key, as it defines how your data is physically stored. You can create a new table with the new primary key, copy data from the old one, and then drop the old table.
I'd say no: because the collection is mutable, and you can't have a primary key that keeps changing in time.
It's been a while but as this is on first page in Google when you look for using a map in the primary key, I thought it was worth to update it.
Cassandra now allows (I think since 2.1) to use a collection in a Primary Key provided it is frozen.
A frozen value serializes multiple components into a single value. Non-frozen types allow updates to individual fields. Cassandra treats the value of a frozen type as a blob. The entire value must be overwritten.
With frozen, the collection becomes essentially immutable not allowing for modifications in-place, therefore, becoming suitable for a Primary Key.
Here is an example using a frozen list.
CREATE TABLE test_frozen (
app_id varchar,
kind varchar,
properties frozen <list<text>>,
PRIMARY KEY ((app_id, kind), properties));
Since 2.1, Cassandra also allows to create an index on columns of type map, set or list - though that not's necessarily a good idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With