---
title: "Apache Cassandra Learning Step by Step (4): Data Modeling"
author: "itstarting"
date: "2012-02-22T18:14:29Z"
tags: []
categories: ["互联网"]
slug: "apache-cassandra-learning-step-by-step-4-data-modeling"
draft: false
lastmod: "2012-02-25T17:57:33Z"
id: 1418328
url: "https://itstarting.iteye.com/blog/1418328"
---

22 Feb 2012, by Bright Zheng (IT进行时)

 

写在这章前面的几点牢骚或感慨：

1\. 我发现建模是比较别扭的一件事情，尤其是你的脑子里都是RDBMS的ERD的时候；

2\. 本人试图通过两者的建模过程体现思考要点，但感觉在NoSQL的建模上有点“那个”——如果不在大型项目上吃亏过或者直接受教于前辈，总感觉缺那么点味道；

3\. 这篇是我写的最郁闷的一篇，而且可能后面需要无数个补丁，但管不了了，有错误才有感悟

 

 

# 5. Data Modeling

Data Modeling is one of the most important things in experiencing Cassandra, especially to those who have lots of experiences with RDBMS data modeling.

 

By admiring [Twissandra](https://github.com/twissandra/twissandra) project, we name it as ***Jtwissandra*** as an example. If possible, I’ll try to create and implement it and share it in GitHub.

This is a simple example to showcase the NoSQL concepts by admiring the Twitter via Cassandra.

## 5.1.  Tranditional RDBMS Data Modeling

Following are the core Entities & Relationships if we’re modeling in RDBMS concepts.

![](http://dl.iteye.com/upload/attachment/0063/6084/d7122ab6-4938-3dd5-9abc-99fb0c6391c9.png)  
 

Here are some pseudo codes for demonstrating the business logic/requirements:

**1. Adding a new user:**

USER.insert(user\_id, user\_name, user\_password, create\_timestamp);

**2. Following a friend:**

FRIEND.insert(user\_id, followed\_id, create\_timestamp)

        as ($current\_user\_id, user\_id, create\_timestamp);

FOLLOWER.insert(user\_id, follower\_id, create\_timestamp)

        as (user\_id, $current\_user\_id, create\_timestamp);

**3. Tweetting:**

FRIEND.insert(user\_id, followed\_id, create\_timestamp)

        as ($current\_user\_id, user\_id, create\_timestamp);

FOLLOWER.insert(user\_id, follower\_id, create\_timestamp)

        as (user\_id, $current\_user\_id, create\_timestamp);

**4. Getting Tweets (that are twitted by self and friends):**

select * from TWEET t

where

   t.user\_id = $current\_user\_id

   or t.user\_id in (

       select followed\_id from FRIEND

       where user\_id = $current\_user\_id

   )

**Comment**:: What a bottleneck is here!! That’s also the most important reason why Twitter has to migrate to NoSQL solutions.

## 5.2. NoSQL Data Modeling

Before we go deeper of NoSQL data modeling with Cassandre, we must understand the key design points of it.

       1. Cassandra is a key-value based model

       2. Cassandra supports more complex modeling by importing the concept of Super Column

       3. The data can be stored in two ways: as column names or as values (it’s really confusing for the beginners sometimes, but you will be free if you understand more especially on the indexing)

       4. The Columns, normal Columns or Super ones, in the Column Family is sorted by Column Names, not values

 

So let’s get started.

We need to create the Keyspace first.

create keyspace JTWISSANDRA

with placement\_strategy = 'org.apache.cassandra.locator.SimpleStrategy'

and strategy\_options = \[{replication\_factor:1}];

 

Under this Keyspace, we’ll be working on the data modeling one by one.

## 5.2.1. User

The key points should be under consideration:

           - The key we can simply use Time UUID

           - The user\_name must be (secondary) indexed because we may use it for search

           - The create\_timestamp should be (secondary) indexed because we may use it for search or some kinds of partitioning

           - The password must be encoded as base64. No more CSDN story please.

 

So the sample data model will be as following:

**ColumnFamily: USER**

**Key**

**Columns**

 

550e8400-e29b-41d4-a716-446655440000

**name**

**value**

 

“user\_name”

“itstarting”

 

“password”

"\*\*\*\*\*\*"

 

“create\_timestamp”

1329836819890000

550e8400-e29b-41d4-a716-446655440001

**name**

**value**

 

“user\_name”

“test1”

 

“password”

"\*\*\*\*\*\*"

 

“create\_timestamp”

1329836819890001

Here is the create script:

create column family USER

with comparator = UTF8Type  

and key\_validation\_class = UTF8Type

and default\_validation\_class = UTF8Type

and column\_metadata = [

{column\_name: user\_name, validation\_class: UTF8Type,

               index\_name:user\_name\_idx, index\_type:KEYS }

{column\_name: user\_password, validation\_class: UTF8Type}

{column\_name: create\_timestamp, validation\_class: LongType,

               index\_name:create\_timestamp\_idx, index\_type:KEYS}

];

And the insert script/CLI for showcase only:

// insert user 550e8400-e29b-41d4-a716-446655440000

set USER\[‘550e8400-e29b-41d4-a716-446655440000’]\[‘user\_name’] = ‘itstarting’;

set USER\[‘550e8400-e29b-41d4-a716-446655440000’]\[‘password’] = ‘111222’;

set USER\[‘550e8400-e29b-41d4-a716-446655440000’]\[‘create\_timestamp’] = 1329836819890000;

 

// insert user 550e8400-e29b-41d4-a716-446655440001

set USER\[‘550e8400-e29b-41d4-a716-446655440001’]\[‘user\_name’] = ‘test1;

set USER\[‘550e8400-e29b-41d4-a716-446655440001’]\[‘password’] = ‘222111’;

set USER\[‘550e8400-e29b-41d4-a716-446655440001’]\[‘create\_timestamp’] = 1329836819890001;

 

## 5.2.2. Friend

The friends mean: who are the user X following?

The key points should be under consideration:

           - The key should be the uuid of the user X

           - The timestamp when the relationship is built is the column (for friend sorting) and the friend’s uuid is the value. Wow again here. Right?

 

Let’s say the two users we created are friends each other.

So the sample data model will be as following:

**ColumnFamily: FRIEND**

**Key**

**Columns**

 

550e8400-e29b-41d4-a716-446655440000

**name**

**value**

 

“1329836819859000”

“550e8400-e29b-41d4-a716-446655440001”

 

*If the guy has more friends, insert colums here*

550e8400-e29b-41d4-a716-446655440001

**name**

**value**

 

“1329836819781000”

“550e8400-e29b-41d4-a716-446655440000”

 

*If the guy has more friends, insert colums here*

The first record means the user X is 550e8400-e29b-41d4-a716-446655440000 and his/her friend is 550e8400-e29b-41d4-a716-446655440001 and the relationship is established at timestamp of 1329836819859000.

Here is the create script:

create column family FRIEND

with comparator = UTF8Type  LongType

and key\_validation\_class = UTF8Type

and default\_validation\_class = UTF8Type;

No more column name definitions here? Yes, Cassandra is a so-called schema-free data store. Wow!

 

And the insert script/CLI for showcase only:

set FRIEND\[‘550e8400-e29b-41d4-a716-446655440000’]\[‘1329836819859000’]

   = ‘550e8400-e29b-41d4-a716-446655440001;

 

set FRIEND\[‘550e8400-e29b-41d4-a716-446655440001’]\[‘1329836819781000’]

   = ‘550e8400-e29b-41d4-a716-446655440000;

## 5.2.3. Follower

The Follower is a reversed concept compared to Friend: Who are following user X?

The key points should be under consideration:

           - The key should be the uuid of the user X

           - The timestamp when the relationship is built is the column (for follower sorting) and the follower’s user uuid is the value.

 

Actually the logic should be within the same transaction of friend creation. So we’d like to follow the sample in Friend chapter.

So the sample data model will be as following:

**ColumnFamily: FOLLOWER**

**Key**

**Columns**

 

550e8400-e29b-41d4-a716-446655440001

**name**

**value**

 

“1329836819859000”

“550e8400-e29b-41d4-a716-446655440000”

 

*If the guy has more friends, insert colums here*

550e8400-e29b-41d4-a716-446655440000

**name**

**value**

 

“1329836819781000”

“550e8400-e29b-41d4-a716-446655440001”

 

*If the guy has more friends, insert colums here*

 

Here is the create script:

create column family FOLLOWER

with comparator =  UTF8Type  LongType   

and key\_validation\_class = UTF8Type

and default\_validation\_class = UTF8Type;

 

And the insert script/CLI for showcase only:

set FOLLOWER\[‘550e8400-e29b-41d4-a716-446655440001’]\[‘1329836819859000’’]

   = ‘550e8400-e29b-41d4-a716-446655440000;

 

set FOLLOWER\[‘550e8400-e29b-41d4-a716-446655440000’]\[‘329836819781000’]

   = ‘550e8400-e29b-41d4-a716-446655440001;

## 5.2.4. Tweet & Timeline

The tweets are the soul of Twitter.

The key points should be under consideration:

           - How to get my tweets?

           - How to get my friends’ tweets without join?

           - How to sort all tweets including mine and my friends’.

 

That’s why Twitter imported the concept of **Timeline**.

Let’s imagine something like this (please correct me if I’m wrong on following discussions):

![Timeline](http://user.services.openoffice.org/en/forum/download/file.php?id=8226&sid=7f8dbd205ea4482eaf02c389b1a465de&mode=view)&lt;!--

Copied from: [http://user.services.openoffice.org/en/forum/viewtopic.php?f=9&t=32185](http://user.services.openoffice.org/en/forum/viewtopic.php?f=9&t=32185)

 

All the events (tweets) are going along the time.

The Timeline means the line with the specified user’s all related events including

           - The events (tweets) I sent

           - The events (tweets) my friends sent

 

So the tweets are inserted to CF of Tweet but need to add one more CF: Timeline.

 

CAUTION: The following learning experiences/exercises might be not correct, please take your own risks if you still want to read on. But of course, any feedback is welcome.

## 5.2.4.1. Tweet

**ColumnFamily: TWEET**

**Key**

**Columns**

 

550e8400-e29b-41d4-a716-446655440011

**name**

**value**

 

“user\_uuid”

“550e8400-e29b-41d4-a716-446655440000”

 

“tweet\_content”

“Hello world: 11”

550e8400-e29b-41d4-a716-446655440012

**name**

**value**

 

“user\_uuid”

“550e8400-e29b-41d4-a716-446655440000”

 

“tweet\_content”

“Hello world: 12”

550e8400-e29b-41d4-a716-446655440021

**name**

**value**

 

“user\_uuid”

“550e8400-e29b-41d4-a716-446655440001”

 

“tweet\_content”

“Hello world: 21”

550e8400-e29b-41d4-a716-446655440022

name

value

 

“user\_uuid”

“550e8400-e29b-41d4-a716-446655440001”

 

“tweet\_content”

“Hello world: 22”

Here is the create script:

create column family TWEET

            with comparator = UTF8Type  

            and key\_validation\_class = UTF8Type

            and default\_validation\_class = UTF8Type

            and column\_metadata = [

                        {column\_name: user\_uuid, validation\_class: UTF8Type}

                        {column\_name: tweet\_content, validation\_class: UTF8Type}

            ];         

And the insert script/CLI for showcase only:

set TWEET\['550e8400-e29b-41d4-a716-446655440011']\['user\_uuid']

    = '550e8400-e29b-41d4-a716-446655440000';

set TWEET\['550e8400-e29b-41d4-a716-446655440011']\['tweet\_content'] = 'Hello world: 11';

 

set TWEET\['550e8400-e29b-41d4-a716-446655440012']\['user\_uuid']

    = '550e8400-e29b-41d4-a716-446655440000';

set TWEET\['550e8400-e29b-41d4-a716-446655440012']\['tweet\_content'] = 'Hello world: 12';

 

set TWEET\['550e8400-e29b-41d4-a716-446655440021']\['user\_uuid']

    = '550e8400-e29b-41d4-a716-446655440001';

set TWEET\['550e8400-e29b-41d4-a716-446655440021']\['tweet\_content'] = 'Hello world: 21';

 

set TWEET\['550e8400-e29b-41d4-a716-446655440022']\['user\_uuid']

    = '550e8400-e29b-41d4-a716-446655440001';

set TWEET\['550e8400-e29b-41d4-a716-446655440022']\['tweet\_content'] = 'Hello world: 22';

 

## 5.2.4.2. Timeline

**ColumnFamily: TIMELINE**

**Key**

**Columns**

 

550e8400-e29b-41d4-a716-446655440000

**name**

**value**

 

“1329883039824000”

“550e8400-e29b-41d4-a716-446655440011”

 

“1329883039825000”

“550e8400-e29b-41d4-a716-446655440021”

 

“1329883039934000”

“550e8400-e29b-41d4-a716-446655440012”

 

“1329883039935000”

“550e8400-e29b-41d4-a716-446655440022”

550e8400-e29b-41d4-a716-446655440001

**name**

**value**

 

“1329883039824000”

“550e8400-e29b-41d4-a716-446655440011”

 

“1329883039825000”

“550e8400-e29b-41d4-a716-446655440021”

 

“1329883039934000”

“550e8400-e29b-41d4-a716-446655440012”

 

“1329883039935000”

“550e8400-e29b-41d4-a716-446655440022”

 

Here is the create script:

create column family TIMELINE

with comparator =  UTF8Type  LongType   

and key\_validation\_class = UTF8Type

and default\_validation\_class = UTF8Type;

 

And the insert script/CLI for showcase only:

set TIMELINE\['550e8400-e29b-41d4-a716-446655440000']\['1329883039824000']

     = '550e8400-e29b-41d4-a716-446655440011';

set TIMELINE\['550e8400-e29b-41d4-a716-446655440000']\['1329883039825000']

     = '550e8400-e29b-41d4-a716-446655440021';

set TIMELINE\['550e8400-e29b-41d4-a716-446655440000']\['1329883039834000']

     = '550e8400-e29b-41d4-a716-446655440012';

set TIMELINE\['550e8400-e29b-41d4-a716-446655440000']\['1329883039835000']

     = '550e8400-e29b-41d4-a716-446655440022';

 

set TIMELINE\['550e8400-e29b-41d4-a716-446655440001']\['1329883039824000']

     = '550e8400-e29b-41d4-a716-446655440011';

set TIMELINE\['550e8400-e29b-41d4-a716-446655440001']\['1329883039825000']

     = '550e8400-e29b-41d4-a716-446655440021';

set TIMELINE\['550e8400-e29b-41d4-a716-446655440001']\['1329883039834000']

     = '550e8400-e29b-41d4-a716-446655440012';

set TIMELINE\['550e8400-e29b-41d4-a716-446655440001']\['1329883039835000']

     = '550e8400-e29b-41d4-a716-446655440022';