Saturday, July 31, 2010

Creating the TaggedPost Column Family

Now it’s time to deal with the TaggedPosts Column family. I like to think of this as the indexing mechanism for our application, it’s this Columnfamily that allows us to get all posts or posts from a particular tag. Because the Column names are TimeUUIDType, Arin (who’s design we are working from remember WTF is a SuperColumn? An Intro to the Cassandra Data Model) points out that getting the latest 10 entries is going to be very efficient.

So our entries for this Column family are going to look like:

Tag:{
TimeofPost: TitleofPost,
TimeofPost:TitleofPost,
}

Also remember that Arin’s design has denormalised the tags in the Blog entry so they look like tag1,Tag2,Tag3. In our test code we’ll use an array of tags for a our test entry.

First up we are going to need a ColumnPath for this Column family:

ColumnPath tagsColumnPath = new ColumnPath("TaggedPosts");

So here’s the code:

String Tags[]={"Daily","Ramblings","_No-Tag_"};
columnName = "tags";
value = "";
for (int i=0;i<Tags.length; i++){
      value=value+Tags[i]+",";
      String tagKey=Tags[i];
     tagsColumnPath.setColumn(asByteArray(timeUUID));
     ks.insert(tagKey, tagsColumnPath, slugValue.getBytes());
}


The only point to note here is that the slugValue has been stored earlier in the code and is essentially the title of the post

Now , there is one major point to note, that’s the timeUUID. There are some problems creating this value which is essentially the time of the post, for details on the problems see:

http://wiki.apache.org/cassandra/FAQ#working_with_timeuuid_in_java

Essentially to create this UUID we are going to use Johann Burkard’s UUID library available from http://johannburkard.de/software/uuid/ and some of the code detailed in the Apache Cassandra FAC. So our timeUUID is generated as:

java.util.UUID timeUUID=getTimeUUID();

Where getTimeUUID() is taken form the Cassandra FAC:

public static java.util.UUID getTimeUUID()
{
     return java.util.UUID.fromString(new com.eaio.uuid.UUID().toString());
}

And that’s all we need to create he TaggedPosts CollumnFamily.

No comments:

Post a Comment