I am reading Cassandra- The definitive guide by E.Hewitt
. I am in the fourth chapter where the author describes the code for sample Hotel application. The image from the book is given here for reference.
Here is the method for inserting rowkeys
in the column Family
HotelByCity
private void insertByCityIndex(String rowKey, String hotelName) throws Exception {
Clock clock = new Clock(System.nanoTime());
Column nameCol = new Column(hotelName.getBytes(UTF8), new byte[0], clock);
ColumnOrSuperColumn nameCosc = new ColumnOrSuperColumn();
nameCosc.column = nameCol;
Mutation nameMut = new Mutation();
nameMut.column_or_supercolumn = nameCosc;
//set up the batch
Map<String, Map<String, List<Mutation>>> mutationMap =
new HashMap<String, Map<String, List<Mutation>>>();
Map<String, List<Mutation>> muts =
new HashMap<String, List<Mutation>>();
List<Mutation> cols = new ArrayList<Mutation>();
cols.add(nameMut);
String columnFamily = "HotelByCity";
muts.put(columnFamily, cols);
//outer map key is a row key
//inner map key is the column family name
mutationMap.put(rowKey, muts);
//create representation of the column
ColumnPath cp = new ColumnPath(columnFamily);
cp.setColumn(hotelName.getBytes(UTF8));
ColumnParent parent = new ColumnParent(columnFamily);
//here, the column name IS the value (there's no value)
Column col = new Column(hotelName.getBytes(UTF8), new byte[0], clock);
client.insert(rowKey.getBytes(), parent, col, CL);
LOG.debug("Inserted HotelByCity index for " + hotelName); } //end inserting ByCity index
I am having difficulty following the code. Especially why so many containers (Maps) are created.
what is the purpose of Mutation
object etc? How exactly is the rowkey inserted?
If you could explain, what is going on at each step of code, that would be great. The book does not explain and I am not able to get a picture of how this is done.
P.S: I am a Java developer. so I am familiar what Maps etc. But I just don't follow why a Map is stuffed inside another Map and other details
Thanks