Skip to content
ykulbak edited this page Sep 14, 2010 · 1 revision

To start using Indexed HBase do the following:

  1. Set the hbase.region.impl property to IdxRegion

    <property>
      <name>hbase.hregion.impl</name>
      <value>org.apache.hadoop.hbase.regionserver.IdxRegion</value>
    </property>


  2. Make sure that ihbase-*.jar and commons-lang version 2.4 jar are added to the hbase classpath (you’ll need to edit hbase-env.sh)
  3. When creating a table define which columns to index using IdxColumnDescriptor. The supported types are all the java primitive data types as well as byte[], char[] and BigDecimal

    Example: Creating an HTable with an index on family:qual column
    Note that this snippet below assumes that all the values assigned to family:qual are exactly 8 bytes, preferably created using Bytes.toBytes(long). The table may have rows in which family:qual is missing, those rows will not be included in the index.


    byte[] tableName = Bytes.toBytes(“table”);
    byte[] familyName = Bytes.toBytes(“family”);
    byte[] qualifier = Bytes.toBytes(“qual”);
    IdxColumnDescriptor idxColumnDescriptor = new IdxColumnDescriptor(familyName);
    IdxIndexDescriptor indexDescriptor = new IdxIndexDescriptor(qualifier, IdxQualifierType.LONG);
    idxColumnDescriptor.addIndexDescriptor(indexDescriptor);
    HTableDescriptor htd = new HTableDescriptor(tableName);
    htd.addFamily(idxColumnDescriptor);
    HBaseConfiguration conf = new HBaseConfiguration();
    HBaseAdmin admin = new HBaseAdmin(conf);
    admin.createTable(htd);
    HTable table = new HTable(conf, htd.getName());
    . . .

  4. When scanning make sure you instantiate an instance of IdxScan and that you set the Expression property

Indexed scans notes

  • When creating an IdxScan instance you must provide both an expression and filter. The filter is essential for correct scan results
  • The index expression must accept any row accepted by the filter
  • The filter may accept a subset of the rows accepted by the index expression (e.g. narrow down the results set)
  • Setting a filter without setting an expression is supported and would revert to a ‘good old scan’
  • The supported expression types are comparison, and, or. Comparisons support GT, GTE, EQ, LTE, LT
  • The caller may combine any number of index expressions using any of the existing indexes. Trying to add an expression for a non-indexed column would result in a runtime error

    byte[] tableName = Bytes.toBytes(“table”);
    . . .
    IdxScan idxScan = new IdxScan();
    idxScan.setExpression(Expression.comparison(familyName, qualifier, Comparison.Operator.EQ, Bytes.toBytes(42L));
    idxScan.setFilter(new SingleColumnValueFilter(familyName, qualifier, CompareFilter.CompareOp.EQUAL, Bytes.toBytes(42L)));
    idxScan.setCaching(1000);
    ResultScanner scanner = table.getScanner(idxScan);
    for (Result res : scanner) {
    // Do stuff with res
    }

Clone this wiki locally