Using maps to store key-value pairs

The third and final collection type offered by Cassandra is the map, which stores key-value pairs. Keys are unique and unordered, like the elements of a set.

Suppose we'd like to keep track of our users' identities on other social networks. We could create a column for each network, but that would require changing the schema each time we discovered a new network that we want to track, and would also potentially result in a large number of columns in the users table just to keep track of a given user's identities.

Instead, let's create a map column that maps the name of a social network to the numeric ID of the user on that network:

ALTER TABLE "users"
ADD social_identities MAP<text,bigint>;

This definition looks ...

Get Learning Apache Cassandra now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.