日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Hbase 之KeyValue結構詳解

作者:南風知我意丿 更新時間: 2022-09-05 編程語言

文章目錄

  • 源碼
  • KeyValue結構
    • KeyValue的實現
  • 操作KeyValue

源碼

在這里插入圖片描述

  • 源碼注釋部分翻譯
HBase 鍵值。這是基本的 HBase 類型。

KeyValue 包裝一個字節數組,并將偏移量和長度放入傳遞的數組中,從哪里開始將內容解釋為 KeyValue。
字節數組中的 KeyValue 格式為: <keylength> <valuelength> <key> <value> 
Key 進一步分解為: <rowlength> <row> <columnfamilylength> <columnfamily> <columnqualifier> <timestamp> <keytype> 
行長最大值為 Short.MAX_SIZE,
列族長度最大值為 Byte.MAX_SIZE,
列限定符 + 鍵長度必須 < Integer.MAX_SIZE。
該列不包含 familyqualifier 分隔符 COLUMN_FAMILY_DELIMITER

KeyValue結構

在這里插入圖片描述

  • 在KeyValue中,其中的KeyLength為4B:
  • ValueLength標識Value在字節數組中所占的長度,為4B:
  • Row Length:存儲rowkey的長度,為2B
  • TimeStamp是Long型,肯定就是占8B:
  • KeyType為1B:

從ColumnQualifier開始內容前面不在帶有長度了。TimeStamp和KeyType因為所占的長度是固定的,所以不用包含長度信息。而Qualifier的長度信息,則用以下的方式可以得出:

new Ordering[(ImmutableBytesWritable, KeyValue)] {
      override def compare(x: (ImmutableBytesWritable, KeyValue), y: (ImmutableBytesWritable, KeyValue)): Int = {
        x._2.getFamilyLength()
        x._2.getQualifierLength
      }
    }

KeyValue的實現

在Hbase中,所有數據都是以Byte的形式存在的。在KeyValue中,使用的是byte數組來存儲實際內容。

 // KeyValue core instance fields.
  private byte [] bytes = null;  // an immutable byte array that contains the KV
  private int offset = 0;  // offset into bytes buffer KV starts at
  private int length = 0;  // length of the KV starting from offset.

其中,bytes數組用來存儲KeyValue中的實際內容,offset表示KeyValue在數組bytes中的起始位置length則是KeyValue在數組bytes中自起始位置offset后的長度

KeyValue提供了一系列的Offset方法在數組中定位各個字段的的起始位置,如getValueOffset,getRowOffset等。也提供了一系列的length方法來獲取KeyValue中各個字段的大小。


操作KeyValue

把keyValue 數據取出放到Map里

 Map<String, Object> stringMap = new HashMap<>();
 stringMap.put("row", Bytes.toStringBinary(getRowArray(), getRowOffset(), getRowLength()));
 stringMap.put("family",Bytes.toStringBinary(getFamilyArray(), getFamilyOffset(), getFamilyLength()));
 stringMap.put("qualifier",Bytes.toStringBinary(getQualifierArray(), getQualifierOffset(), getQualifierLength()));
 stringMap.put("timestamp", getTimestamp());
 stringMap.put("vlen", getValueLength());
 Iterator<Tag> tags = getTags();
 if (tags != null) {
  List<String> tagsString = new ArrayList<String>();
  while (tags.hasNext()) {
   tagsString.add(tags.next().toString());
  }
  stringMap.put("tag", tagsString);
 }

原文鏈接:https://blog.csdn.net/Lzx116/article/details/126609986

欄目分類
最近更新