Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a primary key in Cassandra contain a collection column?

Tags:

cassandra

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)
);
like image 977
Chris Dutrow Avatar asked May 09 '13 20:05

Chris Dutrow


People also ask

Can primary key be a collection of columns?

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.

What is the primary key in Cassandra?

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.

Do primary keys have to be unique Cassandra?

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.

How do you set a primary key in Cassandra?

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.


2 Answers

I'd say no: because the collection is mutable, and you can't have a primary key that keeps changing in time.

like image 65
Óscar López Avatar answered Oct 04 '22 09:10

Óscar López


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.

like image 39
nicholasamorim Avatar answered Oct 04 '22 09:10

nicholasamorim