Database Reference
In-Depth Information
Chapter 8. Collections, Tuples, and
User-defined Types
In the preceding chapters, we focused largely on the high-level structure of Cassandra
tables, and particularly on the forms and uses of primary keys. We paid scant attention to
data columns, generally defining our table schemas with one or two text columns at most.
While this type of scalar data column is the workhorse of Cassandra schemas, they're not
the only arrow in Cassandra's quiver.
Cassandra offers three different structures that store multiple values in a single column.
Collection columns can store an arbitrary number of values, all of the same type. Tuples
and user-defined types store a fixed number of values but these values can be of different
types. While the fields in a tuple are specified by position, user-defined types give a name
to each field; when reading data from a user-defined type, it's possible to retrieve a subset
of the fields within the column.
Collections come in three flavors: lists, sets, and maps. Set columns store unordered,
unique collections of values. List columns store ordered collections of values, and allow
duplicates. Map columns store dictionaries of key-value pairs.
The true power of collection columns comes from the ability to make discrete modifica-
tions to their contents, without reading their value first. It is possible, for instance, to push a
value on to the end of a list, without reading the list's current contents first. We will see
many more such examples in this chapter.
Collection columns are particularly useful in scenarios with multiple processes concur-
rently writing data to the same target. However, collection columns have their limitations,
and are not appropriate for every scenario in which they could conceivably be used. We'll
discuss the upsides and downsides of collection columns in this chapter.
Tuples and user-defined types are useful when we want to store a single piece of data that
has a complex structure. They are especially powerful when used in combination with other
CQL features, particularly secondary indexes. We'll see how tuples and user-defined types
can be used to work around one of the major limitations of secondary indexes in Cas-
sandra: the restriction of indexes to a single column.
By the end of this chapter, you'll know:
Search WWH ::




Custom Search