Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learning Apache Cassandra
Learning Apache Cassandra

Learning Apache Cassandra: Build an efficient, scalable, fault-tolerant, and highly-available data layer into your application using Cassandra

eBook
$19.99 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning Apache Cassandra

Chapter 2. The First Table

For the next few chapters, we'll be building the persistence layer for an application called MyStatus, which allows its users to post status updates for their friends to see. In this chapter, we'll make the first table of that persistence layer, which will store user profiles.

In Chapter 1, Getting Up and Running with Cassandra, you set up a local Cassandra installation and created a keyspace, so we're prepared to hit the ground running. This chapter will provide a whirlwind tour of many core Cassandra concepts such as:

  • How Cassandra tables are structured
  • Cassandra's type system
  • How to create a table
  • How to insert rows into a table
  • How to retrieve rows by primary key
  • How to query for all rows in a table
  • How to page through large result sets

Creating the users table

Our first table will store basic user account information: username, email, and password. To create the table, fire up the CQL shell (don't forget to use the USE "my_status"; statement if you are starting a fresh session) and enter the following CQL statement:

CREATE TABLE "users" (
  "username" text PRIMARY KEY,
  "email" text,
  "encrypted_password" blob
);

In the above statement, we created a new table called users, which has three columns: username and email, which are text columns, and encrypted_password, which has the type blob. The username column acts as the primary key for the table.

Structuring of tables

Cassandra structures tables in rows and columns, just like a relational database. Also like a relational database, the columns available to a table are defined in advance. New columns cannot be added on-the-fly when inserting data, although it's possible to update an existing table's schema...

Inserting data

For our status sharing application, the first thing we'll want any user to do is to create an account. We'll ask them to choose a username, enter their email, and pick a password; our business logic will be responsible for ensuring that the entries are valid and for encrypting the password appropriately. At that point, we'll be ready to insert the account information as a new row in the users table:

INSERT INTO "users"
("username", "email", "encrypted_password")
VALUES (
  'alice',
  '[email protected]',
  0x8914977ed729792e403da53024c6069a9158b8c4
);

In the previous statement, which should be familiar to anyone who has used an SQL database, we provide the following information:

  • We want to add a row to the users table
  • We'll be adding data to three columns in that row: username, email, and encrypted_password
  • Finally, we provide the values to insert into those columns, in the same order that the column...

Selecting data

We now know how to retrieve data from the database, but that isn't much good unless we can get it back again. Let's say we now want to build an account settings page for MyStatus; we've got the user's username stored in a persistent session, but we will retrieve the other profile fields from the database to display in the settings form:

SELECT * FROM "users"
WHERE "username" = 'alice';

This query tells Cassandra we want to retrieve the rows where the value for username (the primary key) is alice. The * wildcard simply says we would like all the columns in that row, saving us from having to type them all out. You'll see the rows we requested nicely formatted in the CQL shell as follows:

Selecting data

In other scenarios, we don't need all the columns. When a user tries to log in to MyStatus, we want to retrieve their password and compare it to the one the user provided us with, but we don't care about the email. Avoiding unnecessary...

Developing a mental model for Cassandra

Any time you learn a new tool, you will naturally begin to develop a mental model for how that tool works. So far, our model for how Cassandra tables works is fairly simple; we will expand upon it throughout the book.

For now, we can think of a Cassandra table as a collection of keys, each of which points to a row. Each row contains data in some subset of its columns.

We also know that, at least in the users table, rows are stored non-contiguously; accessing each row requires Cassandra to seek a different place in storage. So we may imagine the current state of our users table to look something like this:

Developing a mental model for Cassandra

Essentially, we can think of our Cassandra table as a key-value store, where each value consists of one or more predefined columns containing data of a predefined type.

Summary

In this chapter, you're off to a running start with your MyStatus application, having created our first table, inserted data into it, and learned a few ways to retrieve that data. We've developed a model for how Cassandra tables are structured, and taken a deep dive into Cassandra's type system.

We now have experience with the INSERT and SELECT statements, two of the core operations in CQL. You've learned that primary keys, at least the ones we've seen so far, determine the order in which rows are returned, but that ordering is opaque to an application. We can only rely on it being consistent. We've seen some of the limitations of simple tables in Cassandra, and we know that the types of multi-row retrieval we know so far aren't the most efficient.

In Chapter 3, Organizing Related Data, we'll introduce a new way to structure a table that lets us overcome many of the limitations we encountered here.

Left arrow icon Right arrow icon

Description

If you're an application developer familiar with SQL databases such as MySQL or Postgres, and you want to explore distributed databases such as Cassandra, this is the perfect guide for you. Even if you've never worked with a distributed database before, Cassandra's intuitive programming interface coupled with the step-by-step examples in this book will have you building highly scalable persistence layers for your applications in no time.

What you will learn

  • Install Cassandra and create your first keyspace
  • Choose the right table structure for the task at hand in a variety of scenarios
  • Use range slice queries for efficient data access
  • Effortlessly handle concurrent updates with collection columns
  • Ensure data integrity with lightweight transactions and logged batches
  • Understand eventual consistency and use the right consistency level for your situation
  • Implement best practices for data modeling and access

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 25, 2015
Length: 246 pages
Edition : 1st
Language : English
ISBN-13 : 9781783989218
Vendor :
Apache
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 25, 2015
Length: 246 pages
Edition : 1st
Language : English
ISBN-13 : 9781783989218
Vendor :
Apache
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 152.97
Mastering Apache Cassandra - Second Edition
$54.99
Learning Real-time Analytics with Storm and Cassandra
$48.99
Learning Apache Cassandra
$48.99
Total $ 152.97 Stars icon
Banner background image

Table of Contents

13 Chapters
1. Getting Up and Running with Cassandra Chevron down icon Chevron up icon
2. The First Table Chevron down icon Chevron up icon
3. Organizing Related Data Chevron down icon Chevron up icon
4. Beyond Key-Value Lookup Chevron down icon Chevron up icon
5. Establishing Relationships Chevron down icon Chevron up icon
6. Denormalizing Data for Maximum Performance Chevron down icon Chevron up icon
7. Expanding Your Data Model Chevron down icon Chevron up icon
8. Collections, Tuples, and User-defined Types Chevron down icon Chevron up icon
9. Aggregating Time-Series Data Chevron down icon Chevron up icon
10. How Cassandra Distributes Data Chevron down icon Chevron up icon
A. Peeking Under the Hood Chevron down icon Chevron up icon
B. Authentication and Authorization Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(13 Ratings)
5 star 38.5%
4 star 61.5%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Amazon Customer Oct 07, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book covers Cassandra features and CQL very well. Book also touches on internal workings of Cassandra.Book doesn't touch any programming language API available for for communicating with Cassandra. It would have been good if one additional chapter has been dedicated to programming language API like Java.
Amazon Verified review Amazon
Jascha Casadio Dec 20, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Sooner or later, every professional working with noSQL databases will meet a beauty called Cassandra, a distributed database management system initially developed by Facebook and now a top-level project at the Apache Foundation. Widely acclaimed for its features, Cassandra is a beast difficult to tame. Next to tech giants such as Facebook, Twitter and Ubisoft are a myriad of start-ups selling data driven products relying on Cassandra. The high demand of professionals able to properly set it up and tune it results in a lot of documentation available, plus a very active community ready to improve it and support new comers. Among the many titles available to us at the bookstore is Learning Apache Cassandra by Mat Brown, released early this 2015, a title that covers the latest features and targets beginners, be them professionals or simply curious people interested in a quick taste of what Cassandra is and what is it capable of.Spanning through less than 300 pages, Learning Cassandra is short but very intense book. By the time the reader gets to the back cover, he will feel like he has been exposed to a lot of information and, mainly, that he was gradually been taken, step by step, through the very basics of this technology, without any abrut change of topic, without feeling lost. The book written by Mat is easy to follow and comprehensive. Through a learn by doing approach, he does introduce each and every concepts, starting with an overview of what Cassandra is and how it differs from other solutions, up to topics such as lightweight transactions and collections, passing through the different data types. The different topics are presented to the reader through a unique example, an application that closely resembles some basic feature of Facebook. The examples are explained step by step. Nothing is taken for granted.A couple of things deserve a praise: the first, is the choice of the author to present Cassandra through CQL, rather than through the bindings of a specific programming language, which would make it less useful and interesting to most of us; the second is the many concepts presented first in SQL, then in Cassandra. Mat here beautifully shows the differences between a noSQL database and an old school relational database. The author often highlights the importance of denormalizing the data and to state the questions we want to answer first and then build our queries around them.Being a book for beginners, we must say that this title does not present any real world example. Similarly, Mat does use a test cluster made of a single machine, which is far from what we find in production. This rules out all the worries about replication, consensus and so on.Overall, this is by far the most friendly introduction to Cassandra that I have read so far. While it does not provide anything related to administration and configuration, as well as nothing about real world scenarios, it is a perfect companion to anyone interested in learning what Cassandra is and how to get it to work.As usual, you can find more reviews on my personal blog: http://books.lostinmalloc.com. Feel free to pass by and share your thoughts!
Amazon Verified review Amazon
AJ Jun 22, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Thanks to Mat, this book is awesome. I read almost all the books on Cassandra available, and somehow I missed this one. This happens to be my latest reading.One question I have for Mat on Pp 170 of the book. While the diagram seems to be correct, the explanation between the two diagrams on this page, for me, looks incorrect. I could be mistaken.In my opinion the explanation should say Alice is on Node3, Bob is on Node1 and Ivan is on Node2Am I missing something. If any one could clarify I would appreciate it.Five stars for this book and the author.
Amazon Verified review Amazon
Amazon Customer Jan 21, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am completely new to Apache Cassandra. I just decided to learn it out of my own interest. I purchased the Kindle edition of this book 3 days back. Till now I have completed 5th chapter and I must say that after a long time I am reading such a good book.Contents are perfectly organized.Lots of real life examples and solutions.No unnecessary description or irrelevant data.Language and presentation style is so simple that beginners will love to read it.I am completely satisfied with this book.
Amazon Verified review Amazon
Nagender Dec 25, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.