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)  編輯  收藏 所屬分類:
          主站蜘蛛池模板: 怀仁县| 叙永县| 晋宁县| 晋州市| 汉沽区| 黄平县| 阿拉善盟| 澄江县| 张掖市| 青冈县| 临湘市| 友谊县| 莲花县| 石首市| 长武县| 丁青县| 金秀| 买车| 曲周县| 板桥市| 乐至县| 平塘县| 光泽县| 合肥市| 漠河县| 六枝特区| 哈巴河县| 昔阳县| 甘洛县| 广灵县| 临潭县| 井冈山市| 万载县| 江北区| 手游| 湖南省| 钦州市| 葵青区| 安溪县| 绵阳市| 镇安县|