How to run HBase utility class from Java

Suppose you need to run HBase utility class from Java code. Such a class will have a main() method, which we will use. For demonstrating, we will use RowSplitter class.

Firstly, 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>

In some our own class, let’s call RegionSplitter’s main():

String args = String.format("-c %s -f %s %s %s", 10, "cf", "your_namespace:your_table", "HexStringSplit");

RegionSplitter.main(args.split(" "));

If you encounter an exception like this:

WARN zookeeper.ClientCnxn: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1119)

Then I found its cause and a way to eliminate it. The fact is that in the main() method of the RegionSplitter class, the HBase configuration is created using HBaseConfiguration.create(). Sometimes it happens, that some necessary properties are empty. But one of the RegionSplitter’s options is “-D <property = value>”. This is the gap that we can use to set all the necessary keys to the HBase configuration, created in the RegionSplitter class. 

Let’s say you already have a configured and ready-to-use Configuration object in your class. If you’re working with Spring Boot Yarn, this is easy:

@Autowired
private org.apache.hadoop.conf.Configuration ourConf;

Or you can create it in some other way that is convenient for you. We need to rewrite all properties that start with “hbase.” from our configuration to the internal RowSplitter configuration via the -D option. Let’s do it:

Iterator<Map.Entry<String, String>> iter = conf.iterator();
StringBuilder res = new StringBuilder();
while (iter.hasNext()) {
    Map.Entry<String, String> entry = iter.next();
    if (entry.getKey().startsWith("hbase.")) {
        if (!res.toString().equals("")) {
            res.append(" ");
        }
        res.append("-D ").append(entry.getKey()).append("=").append(entry.getValue());
    }
}
String args = String.format("-c %s -f %s %s %s %s", 10, "cf", res.toString(), "your_namespace:your_table", "HexStringSplit");

RegionSplitter.main(args.split(" "));

After that, the RowSplitter, such as other HBase utility class, called from the Java code, should work as expected.

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.