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
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Lithuania

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

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 : 9781783989201
Vendor :
Apache
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Lithuania

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 115.97
Mastering Apache Cassandra - Second Edition
€41.99
Learning Real-time Analytics with Storm and Cassandra
€36.99
Learning Apache Cassandra
€36.99
Total 115.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela