Database Reference
In-Depth Information
cqlsh:weblog> CREATE TABLE comments (id timeuuid, post_id
timeuuid, title text, content text, posted_on timestamp,
commenter varchar, PRIMARY KEY(post_id, id));
cqlsh:weblog> CREATE TABLE post_votes(post_id timeuuid
PRIMARY KEY, upvotes counter, downvotes counter);
cqlsh:weblog> CREATE TABLE comment_votes(comment_id
timeuuid PRIMARY KEY, upvotes counter, downvotes counter);
Note
Universally unique identifiers:
uuid
and
timeuuid
In the preceding CQL statements, there are two interesting data types—
uuid
and
timeuuid
.
uuid
stands for universally unique identifier. There are five types of them.
One of these
uuid
types is
timeuuid
, which is essentially
uuid
type 1 that takes
timestamp
as its first component. This means it can be used to sort things by time. This
is what we wanted to do in this example: sort posts by the time they were published.
On the other hand,
uuid
accepts any of these five types of
uuid
as long as the format
follows the standard
uuid
format.
In Cassandra, if you have chosen the
uuid
type for a column, you will need to pass
uuid
while inserting the data. With
timeuuid
, just passing
timestamp
is enough.
The first statement requests Cassandra to create a keyspace named
weblog
with replica-
tion factor 1 because we are running a single node Cassandra on a local machine. Here are
a couple of things to notice:
• The
column
tags in the
posts
table is a set of strings.
• The primary key for
posts
,
categories
, and
comments
has more than one
component. The first of these components is a partition key. Data with the same
primary key in a table resides on the same machine. This means, all the posts' re-
cords that belong to one blog stays on one machine (not really; if the replication
factor is more than one, the records get replicated to as many machines). This is
true for all the tables with composite keys.