Creating HBase Indexes

Another interesting session trying to learn how to create HBase indexes. A few things i've picked up on so far (and there is a good possibility i'm wrong) is that you can not convert a table after it's been created to have indexes.  but then, maybe you can with alter.  for another day.

Create a new table + index:

public static void createIndex(String TABLE_NAME) throws IOException {
   String familyName = "entry:";
   byte[] FAMILY = Bytes.toBytes(familyName);

   IndexedTableAdmin admin;
   IndexedTable table;
   HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
   desc.addFamily(new HColumnDescriptor(FAMILY));
   String[] columns = { "hostname", "msg" };
   for (int i = 0; i <>
byte[] COL_NAME = Bytes.toBytes(familyName + columns[i].toString());
String INDEX_COL_NAME = columns[i].toString();
IndexSpecification colIndex = new IndexSpecification(INDEX_COL_NAME, COL_NAME);
desc.addIndex(colIndex);
   }

   admin = new IndexedTableAdmin(getConfig());
   // creates new table
   admin.createTable(desc);
   table = new IndexedTable(getConfig(), desc.getName());
}

So, once this is run, the following happens:

Table:  TABLE_NAME is created with indexes on "entry:hostname" and "entry:msg"
Table:  TABLE_NAME-hostname is created
Table:  TABLE_NAME-msg is created

Great, so now there is one table and two index tables. 

We can now push some data into it.  Before doing this however, we need to make a configuration change to HBase.

In $HBASE_HOME/conf/hbase-site.xml add the following:

  
        hbase.regionserver.class
        org.apache.hadoop.hbase.ipc.IndexedRegionInterface
        enable indexing
  

  
        hbase.regionserver.impl
        org.apache.hadoop.hbase.regionserver.tableindexed.IndexedRegionServer
        enable indexing
  

This will start the indexing service (if that's the right terminology).  Restart HBase and push some data into the table.  Once you've done this and you scan either of the index tables you'll see it working.