简体   繁体   中英

understanding the code for inserting column family in Cassandra keyspace?

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.

PS: 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

The book describes Thrift interface to Cassandra. It was great at a time, as it allowed to support many clients through a compilation of thrift API into a language of your choice. So a single server API written in Thrift allowed N clients out of the box.

However, thrift is painful to understand and is much slower compared to binary native protocol. Thrift is also a legacy API and should not be used for new application development. The new binary protocol was developed and integrated into the later versions of Cassandra.

It's not only you who has difficulty understanding it. It is machine-generated interface that is probably pointless to learn at this moment of time, so don't bother and have a look at java driver instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM