Database Reference
In-Depth Information
Thinner rows distributed across multiple nodes in a cluster work fine with added sec-
ondary index support. Still applications may require applying sorting order on fetched
rows. Implementing such sorting mechanisms at the client side with a massive amount
of data is clearly not desirable. Applications would rely on a database for such sup-
ports.
In version 1.1 onward, Cassandra provides support for sorted wide rows via com-
posite columns. Sorted wide rows mean more columns in place of skinny rows, which
allows more data to be stored in a colocated way.
Let's explore it with the same Twitter example, where the user and their tweets
should be stored locally and need to be sorted by
tweet_date
.
1.
First, let's create a keyspace
twitter
and column family
users
:
create keyspace twitter with replication =
{'class':'SimpleStrategy','replication_factor':1};
use twitter;
create table users(user_id text,followers
set<text>, tweet_date timestamp, tweet_body
text, first_name text, PRIMARY
KEY(user_id,tweet_date, first_name));
Here for
users
the table contains a composite primary key with
user_id
,
tweet_date
and
first_name
.
2.
Let's store a few columns in the
users
column family for user_id
'im-
vivek'
:
// insert records for 'imvivek'
insert into
users(user_id,tweet_date,tweet_body,first_name)
values('imvivek','2013-12-31','good bye
2013','vivek');
insert into
users(user_id,tweet_date,tweet_body,first_name)
values('imvivek','2014-01-01','welcome
2014','vivek');
insert into
users(user_id,tweet_date,tweet_body,first_name)