Database Reference
In-Depth Information
Creating a tuple column
Let's say we'd like to allow users to add some information about their education to their
profile. In particular, users should be able to enter where they went to university, and what
year they graduated.
There are two distinct pieces of information here, namely school name and graduation year.
However, the two are really two components of a single piece of information. It would
make no sense, for instance, to store a graduation year but not a school name.
For this reason, education is a good candidate for using a tuple type. The syntax for creat-
ing a tuple column is similar to that for creating a collection column: we specify both the
fact that we would like to use a tuple type, and the individual types of each component of
the tuple:
ALTER TABLE "users"
ADD "education" frozen <tuple<text, int>>;
This statement introduces two new CQL keywords, frozen and tuple . Working from
the inside out, the tuple keyword takes one or more type arguments, separated by com-
mas; each type is applied to a component of the tuple. Order matters here: our educa-
tion tuple has two components, the first of which contains text , and the second of
which contains an int .
The frozen keyword means that the education column will be stored as a single, indi-
visible unit. Practically, this means that we cannot insert, change, or remove data in an indi-
vidual component of the tuple; whenever we write to this column, we write all components
together. This stands in contrast to the discrete element modifications we can perform on
collection columns.
Note
In Cassandra 2.1, tuple columns must be declared frozen . Support for nonfrozen tuple
columns that would allow discrete modification of individual tuple components is planned
for Cassandra 3.0.
A tuple type declaration, like frozen <tuple<text, int>> , can be used anywhere
a primitive type like ascii or uuid is valid. You can create collections of tuples, nest
tuples within tuples, or even use tuples as a primary key column.
Search WWH ::




Custom Search