Created
August 12, 2013 11:47
-
-
Save sankars/6210182 to your computer and use it in GitHub Desktop.
Coprocessor for creating secondary index in Hbase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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