Book Image

Learning Apache Cassandra

By : Matthew Brown
4 (1)
Book Image

Learning Apache Cassandra

4 (1)
By: Matthew Brown

Overview of this book

Table of Contents (19 chapters)
Learning Apache Cassandra
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 a bit different from the earlier ones in this chapter, since we're now specifying a pair of types rather than just one. Maps do not require their keys and values to be of the same type, so each is specified individually. The first type given...