Skip to content

Instantly share code, notes, and snippets.

@dongjinleekr
Created January 27, 2021 09:20
Show Gist options
  • Save dongjinleekr/1dbc24a13fdc5dda8c1da4c55104b77c to your computer and use it in GitHub Desktop.
Save dongjinleekr/1dbc24a13fdc5dda8c1da4c55104b77c to your computer and use it in GitHub Desktop.
Unit Testing HBase Application

Tested on Hadoop 2.7.7 + HBase 2.0.5.

  1. Add following dependencies:
  • org.apache.hbase:hbase-client:${hbase.version}
  • org.apache.hbase:hbase-common:${hbase.version}
  • org.apache.hbase:hbase-testing-util:test:${hbase.version}
  1. Use HbaseTestUtil to startup a mini cluster in setup/teardown of the suite.
@Before
public void setUp() {
    HbaseTestUtil.startMiniCluster();
}

@After
public void tearDown() {
    HbaseTestUtil.stopMiniCluster();
}
  1. Create Table and run a test.
HBaseTestingUtility utility = HbaseTestUtil.getUtility();
utility.createTable(TableName.valueOf(HBASE_TABLE), HBASE_COLUMN_FAMILY);

try (Connection connection = ConnectionFactory.createConnection(configuration);
     Table table = connection.getTable(TableName.valueOf(tableName))) {
    try {
        Put put = new Put(Bytes.toBytes(key));
        put.addImmutable(columnFamily.getBytes(), column.getBytes(), parsedValue.getBytes());
        table.put(put);
    } catch (IOException e) {
        ...
    }
} catch (IOException e) {
    ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment