ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

          We recently set up HBase and HBase-trx (from https://github.com/hbase-trx) to use multiple-column indexes with this code.  After you compile it, just copy the jar and the hbase-trx jar into your hbase’s lib folder and you should be good to to!

          When you create a composite index, you can see the metadata for the index by looking at the table description.  One of the properties will read “INDEXES =>” followed by index names and ‘family:qualifier’ style column names in the index.

          KeyGeneratorFactory:

          package com.ir.store.hbase.indexes;

          import java.util.List;

          import org.apache.hadoop.hbase.client.tableindexed.IndexKeyGenerator;

          public class KeyGeneratorFactory {

          public static IndexKeyGenerator getInstance(List columns) {
          return new HBaseIndexKeyGenerator(columns);
          }
          }

          HBaseIndexKeyGenerator:

          package com.ir.store.hbase.indexes;

          import java.io.DataInput;
          import java.io.DataOutput;
          import java.io.IOException;
          import java.util.ArrayList;
          import java.util.List;
          import java.util.Map;
          import org.apache.hadoop.hbase.client.tableindexed.IndexKeyGenerator;
          import org.apache.hadoop.hbase.util.Bytes;

          public class HBaseIndexKeyGenerator extends Object implements IndexKeyGenerator {
          public static byte[] KEYSEPERATOR = "~;?".getBytes();

          private int columnCount;
          private List columnNames = new ArrayList();

          public HBaseIndexKeyGenerator(List memberColumns) {
          // For new key generators
          columnNames = memberColumns;
          columnCount = memberColumns.size();
          }

          public HBaseIndexKeyGenerator() {
          // Hollow constructor for deserializing -- should call readFields shortly
          columnCount = 0;
          }

          public void readFields(DataInput binaryInput) throws IOException {
          columnCount = binaryInput.readInt();
          for (int currentColumn = 0; currentColumn < columnCount; currentColumn++)
          columnNames.add(Bytes.readByteArray(binaryInput));
          }

          public void write(DataOutput binaryOutput) throws IOException {
          binaryOutput.writeInt(columnCount);
          for (byte[] columnName : columnNames)
          Bytes.writeByteArray(binaryOutput, columnName);
          }

          public byte[] createIndexKey(byte[] baseRowIdentifier, Map baseRowData) {
          byte[] indexRowIdentifier = null;
          for (byte[] columnName: columnNames) {
          if (indexRowIdentifier == null)
          indexRowIdentifier = baseRowData.get(columnName);
          else indexRowIdentifier = Bytes.add(indexRowIdentifier, HBaseIndexKeyGenerator.KEYSEPERATOR, baseRowData.get(columnName));
          }
          if (baseRowIdentifier != null)
          return Bytes.add(indexRowIdentifier, HBaseIndexKeyGenerator.KEYSEPERATOR, baseRowIdentifier);
          return indexRowIdentifier;
          }
          }
          posted on 2011-06-11 16:21 ivaneeo 閱讀(407) 評論(0)  編輯  收藏 所屬分類:
          主站蜘蛛池模板: 英吉沙县| 泰兴市| 裕民县| 云霄县| 邓州市| 海淀区| 姜堰市| 新平| 南丰县| 台山市| 佛山市| 璧山县| 大方县| 昆明市| 武宣县| 泸溪县| 开封县| 青阳县| 玛沁县| 平果县| 乳山市| 衡水市| 永顺县| 二连浩特市| 宁明县| 甘南县| 永德县| 亳州市| 丹寨县| 文化| 桂林市| 务川| 海伦市| 英德市| 张家口市| 东阳市| 兰溪市| 富阳市| 谷城县| 德阳市| 南投县|