How to create HBase table with the constant number of regions in Java

Let’s say you need to create an HBase table with a certain constant number of regions. 

Regions pre-creation

First, you need to create a table with a certain pre-created number of regions. You can use one of the createTable() method overloads for that:

void createTable(TableDescriptor desc, byte[][] splitKeys)

You can make the byte[][] splitKeys array as follows:

byte[][] splitKeys = new RegionSplitter.HexStringSplit().split(n);

We need to add the hbase-server module (for example, version 1.2.3) to pom.xml:

<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-server</artifactId>
  <version>1.2.3</version>
</dependency>

You can use other supported region split algorithm, for example, UniformSplit:

byte[][] splitKeys = new RegionSplitter.UniformSplit().split(n);

Disabling regions count autoincrease

For this purpose, we need to set RegionSplitPolicy through the TableDescriptor object, passed to the createTable() method:

HTableDescriptor htableDescr = new HTableDescriptor(tableName);
HColumnDescriptor hcolumnDescr = new HColumnDescriptor(COLUMN_FAMILY);
htableDescr.addFamily(hcolumnDescr);
htableDescr.setRegionSplitPolicyClassName(DisabledRegionSplitPolicy.class.getName());

Conclusion

The full code is:

HTableDescriptor htableDescr = new HTableDescriptor(tableName);
HColumnDescriptor hcolumnDescr = new HColumnDescriptor(COLUMN_FAMILY);
htableDescr.addFamily(hcolumnDescr);
htableDescr.setRegionSplitPolicyClassName(DisabledRegionSplitPolicy.class.getName());
byte[][] splitKeys = new RegionSplitter.HexStringSplit().split(n);
connection.getAdmin().createTable(htableDescr, splitKeys);

If you still have any questions, feel free to ask me in the comments under this article, or write me on promark33@gmail.com.

If I saved your day, you can support me :)

Leave a Reply

Your email address will not be published.