Skip to content

Instantly share code, notes, and snippets.

@sankars
Created August 12, 2013 11:47
Show Gist options
  • Save sankars/6210182 to your computer and use it in GitHub Desktop.
Save sankars/6210182 to your computer and use it in GitHub Desktop.
Coprocessor for creating secondary index in Hbase
public class TestCoprocessor extends BaseRegionObserver{
private HTablePool pool = null;
private final static String INDEX_TABLE = "INDEX_TBL";
private final static String SOURCE_TABLE = "SOURCE_TBL";
@Override
public void start(CoprocessorEnvironment env) throws IOException {
pool = new HTablePool(env.getConfiguration(), 10);
}
@Override
public void postPut(
final ObserverContext<RegionCoprocessorEnvironment> observerContext,
final Put put,
final WALEdit edit,
final boolean writeToWAL)
throws IOException {
byte[] table = observerContext.getEnvironment(
).getRegion().getRegionInfo().getTableName();
// Not necessary though if you register the coprocessor
// for the specific table, SOURCE_TBL
if (!Bytes.equals(table, Bytes.toBytes(SOURCE_TABLE))) {
return;
}
try {
final List<KeyValue> filteredList = put.get(
Bytes.toBytes ( "colfam1"), Bytes.toBytes(" qaul"));
filteredList.get( 0 ); //get the column value
// get the values
HTableInterface table = pool.getTable(Bytes.toBytes(INDEX_TABLE));
// create row key
byte [] rowkey = mkRowKey () //make the row key
Put indexput = new Put(rowkey);
indexput.add(
Bytes.toBytes( "colfam1"),
Bytes.toBytes(" qaul"),
Bytes.toBytes(" value.."));
table.put(indexput);
table.close();
} catch ( IllegalArgumentException ex) {
// handle excepion.
}
}
@Override
public void stop(CoprocessorEnvironment env) throws IOException {
pool.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment