简体   繁体   English

理解在Cassandra键空间中插入列族的代码?

[英]understanding the code for inserting column family in Cassandra keyspace?

I am reading Cassandra- The definitive guide by E.Hewitt . 我正在阅读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 这是在column Family HotelByCity column Family插入行rowkeys的方法

    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? Mutation对象等的目的是什么? 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. PS:我是Java开发人员。 so I am familiar what Maps etc. But I just don't follow why a Map is stuffed inside another Map and other details 所以我很熟悉Maps等。但是我只是不理解为什么将Map填充到另一个Map中以及其他详细信息

Thanks 谢谢

The book describes Thrift interface to Cassandra. 这本书描述了与Cassandra的Thrift接口。 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. 一次很棒,因为它允许通过将Thrift API编译成您选择的语言来支持许多客户。 So a single server API written in Thrift allowed N clients out of the box. 因此,使用Thrift编写的单个服务器API允许开箱即用的N个客户端。

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. Thrift也是旧版API ,不应用于新的应用程序开发。 The new binary protocol was developed and integrated into the later versions of Cassandra. 开发了新的二进制协议,并将其集成到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. 它是机器生成的界面,在此时此刻可能没有什么意义,因此请不要打扰,而要看一下Java驱动程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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