Wednesday, August 4, 2010

Reading column name value pairs from a Supercolumn for the BloggyAppy

In the last blog post we look at reading from normal column families, in this part we are going to look at reading from supercolumns. We are still folowing the BloggyAppy design from Arin Sarkissian. In this design we have a keyspace called columns. Each entry has a key (that is the title slug from before). Each supercoulmn uses a TIMEUID as the key/ name and the details of the comment are stored as columns underneath that. Here’s the structure:

Comments: {
 Blog-Slug:{
  Time_UUID_1:{
   Coment: A Comment,
   Email: andy@abc.com
},
Time_UUID_2:{
   Coment: A Comment,
   Email: andy@abc.com
}
}
}


The first job is to get the comment keys for a particular title slug. First of all remember that we are going to get all the Time_UUID’s which will be the super columns. The process is very similar to getting columns in a normal column family. There are only one major difference, instead of getting a RangeSlice from the keyspace we will use the getSuperRangeSlices method from the keyspace. The KeyRange and slice predicate work in exactly the same way as before. So to get the map of supercolmns we use:


Keyspace ks = client.getKeyspace("BloggyAppy");
ColumnParent columnParent = new ColumnParent("Comments");
          
SlicePredicate slicePredicate = new SlicePredicate();
SliceRange supercolumnRange = new SliceRange();
             
supercolumnRange.setStart(new byte[0]);  
supercolumnRange.setFinish(new byte[0]); supercolumnRange.setReversed(true); 
supercolumnRange.setCount(1000);
slicePredicate.setSlice_range(supercolumnRange);
KeyRange titlesRange = new KeyRange(200); 
titlesRange.setStart_key("First-Blog");
titlesRange.setEnd_key("First-Blog");
              
Map<String, List<SuperColumn>> supermap =ks.getSuperRangeSlices(columnParent, slicePredicate, titlesRange);

Now we have the map we can step through the keys (in our example there will be only one) and then for each get the list of supercolumns underneath it. The only thing to remember here is to convert the column name to type UUId:

for (String key : supermap.keySet()) {
 List<SuperColumn> columns = supermap.get(key);

 System.out.println("Key "+key);
 for (SuperColumn column : columns) {
  //print columns with values
  java.util.UUID Name=toUUID(column.getName()) ;
  System.out.println("Name "+Name);
  }
}


Finally we want to get the names and values of the columns inside the supercolumn. The trick here is to get the actual SuperColumn from the keyspace. Here’s one way to do it (note that column.getName will return a timeUUID in our case):

ColumnPath cp = new ColumnPath("Comments");
cp.setSuper_column(column.getName());
SuperColumn sc = ks.getSuperColumn(key, cp);

Now we have the Supercolumn we can just get a list of columns it contains and iterate through them:

List<Column> cols=sc.getColumns();
Iterator<Column>itr = cols.iterator(); 
while(itr.hasNext()) {
 Column col = itr.next(); 
 System.out.println("\t\t"+string(col.getName()) + "\t ==\t" + string(col.getValue()));
}


We should now have all we need to read columns and supercolumsn from the keyspaces. The next step will be to encapsulate all this into models for our web application to use. We’ll start that in our next post.

No comments:

Post a Comment