Book Image

Learning Apache Cassandra - Second Edition

Book Image

Learning Apache Cassandra - Second Edition

Overview of this book

Cassandra is a distributed database that stands out thanks to its robust feature set and intuitive interface, while providing high availability and scalability of a distributed data store. This book will introduce you to the rich feature set offered by Cassandra, and empower you to create and manage a highly scalable, performant and fault-tolerant database layer. The book starts by explaining the new features implemented in Cassandra 3.x and get you set up with Cassandra. Then you’ll walk through data modeling in Cassandra and the rich feature set available to design a flexible schema. Next you’ll learn to create tables with composite partition keys, collections and user-defined types and get to know different methods to avoid denormalization of data. You will then proceed to create user-defined functions and aggregates in Cassandra. Then, you will set up a multi node cluster and see how the dynamics of Cassandra change with it. Finally, you will implement some application-level optimizations using a Java client. By the end of this book, you'll be fully equipped to build powerful, scalable Cassandra database layers for your applications.
Table of Contents (14 chapters)

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 identity.

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 is...