4

How to use java api to send hbase shell command directly like jdbc?

public static void main(String args[]) {
    // get Connection to connect hbase
    Connection conn = ....;
    // hbase shell command
    String cmd = "get 't1','r1'";

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(cmd);
    while(rs.next()) {
       ...
    }
}

if there is no java api for this, is there another way to achieve the goal?

Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121
Guo
  • 1,761
  • 2
  • 22
  • 45
  • 1
    If the Jar is executed directly on one of Hbase cluster's node, you can execute any **hbase shell command** using Runtime.getRuntime().exec(command). I used it in a Java program where i needed to use the complete bulkload tool. If you want I give you the snippet code. – Umberto Griffo Sep 01 '16 at 08:42
  • I've done something similar in JRuby (which is what the Hbase shell is written in). Otherwise, there is definitely an Hbase java client API, so I'm not sure I understand why passing raw shell commands is a good idea – OneCricketeer Sep 01 '16 at 13:54
  • Have you researched the use of the Java Hbase API? http://www.bogotobogo.com/Hadoop/BigData_hadoop_HBase_Table_with_Java_API.php – OneCricketeer Sep 16 '16 at 02:00
  • @cricket007 : yes above link is java hbase client which is common practise... but user wanted "java api to send hbase shell command directly " which is not! @ Guo, you can use hbase java client which is flexible way of doing things in a right way. – Ram Ghadiyaram Sep 16 '16 at 03:32

1 Answers1

3

Please note that, Phoenix can execute queries in jdbc style... If you want to execute get commands then you can use Hbase java client api directly. Its not common practice to execute from java through shell.

If you still want to do it from java prepare gets or list of commands in a text file and using RunTime.execute you can execute hbase shell <yourhbaseshelllcommands.txt>

see my answer1 or answer2 this to do that. I have done that for spark submit and mapreduce jobs. you can use the same method for your hbase shell execution which was described above.

another way to achieve the goal

To access Hbase in SQL way you can use Phoenix. - https://phoenix.apache.org/faq.html - see this

Also you can check with Impala or hive.

JDBC Client Driver :

import java.sql.*;

public class PhoenixExample {

    public static void main(String[] args) {
        // Create variables
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        PreparedStatement ps = null;

        try {
            // Connect to the database
            connection = DriverManager.getConnection("jdbc:phoenix:localhost");

            // Create a JDBC statement
            statement = connection.createStatement();

            // Execute our statements
            statement.executeUpdate("create table javatest (mykey integer not null primary key, mycolumn varchar)");
            statement.executeUpdate("upsert into javatest values (1,'Hello')");
            statement.executeUpdate("upsert into javatest values (2,'Java Application')");
            connection.commit();

            // Query for table
            ps = connection.prepareStatement("select * from javatest");
            rs = ps.executeQuery();
            System.out.println("Table Values");
            while(rs.next()) {
                Integer myKey = rs.getInt(1);
                String myColumn = rs.getString(2);
                System.out.println("\tRow: " + myKey + " = " + myColumn);
            }
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        finally {
            if(ps != null) {
                try {
                    ps.close();
                }
                catch(Exception e) {}
            }
            if(rs != null) {
                try {
                    rs.close();
                }
                catch(Exception e) {}
            }
            if(statement != null) {
                try {
                    statement.close();
                }
                catch(Exception e) {}
            }
            if(connection != null) {
                try {
                    connection.close();
                }
                catch(Exception e) {}
            }
        }
    }
}

If you are using maven below are dependencies...

<dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-core</artifactId>
            <version>4.6.0-HBase-1.1</version>
        </dependency>
Community
  • 1
  • 1
Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121
  • Isn't this assuming the Phoenix server is installed? The question seems to want to send Hbase shell commands, which is definitely possible given the Hbase client is installed and configured. – OneCricketeer Sep 01 '16 at 13:53
  • user specified that he wanted to access through/like jdbc.thats why I suggested phoenix. – Ram Ghadiyaram Sep 01 '16 at 14:01
  • I suppose OP is confused that JDBC doesn't typically send arbitrary shell commands. Mostly just SQL – OneCricketeer Sep 01 '16 at 14:06
  • @RamPrasadG Thank you for your reply! But I want to send **hbase shell command** directly, not **SQL**. – Guo Sep 16 '16 at 00:05
  • 1
    @cricket_007 Thanks! You are right! I want to send **hbase shell command** directly, not **SQL**. – Guo Sep 16 '16 at 00:20
  • Please note that, Phoenix can execute queries in jdbc style... If you want to execute get commands then you can use Hbase java client api directly. Its not common practice to execute from shell. if you still want to do it from java prepare gets or list of commands in a text file and using `RunTime.execute` you can execute `hbase shell ` – Ram Ghadiyaram Sep 16 '16 at 02:51
  • see my [answer](http://stackoverflow.com/questions/35866640/execute-spark-submit-programmatically-from-java/39451973#39451973) to do that. I have done that for spark submit and mapreduce jobs. you can use the same method for your hbase shell execution which was described above. – Ram Ghadiyaram Sep 16 '16 at 02:59
  • updated my answer.@cricket_007 you can demonstrate your jruby approach which can be useful for the user – Ram Ghadiyaram Sep 16 '16 at 03:16