如何使用java api直接发送hbase shell命令,如jdbc?

如何使用java api直接发送hbase shell命令如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()) { ... } } 

如果没有这个的java api,还有另一种方法来实现目标吗?

请注意,Phoenix可以以jdbc样式执行查询…如果要执行get命令,则可以直接使用Hbase java客户端api。 从java到shell执行它并不常见。

如果您仍想从文本文件中使用java prepare gets或命令列表并使用RunTime.execute执行hbase shell RunTime.execute ,则可以执行hbase shell

看到我的回答1或回答2这样做。 我已经为spark submit和mapreduce工作做了这些。 您可以使用相同的方法执行上面描述的hbase shell执行。

另一种实现目标的方法

要以SQL方式访问Hbase,您可以使用Phoenix。 – https://phoenix.apache.org/faq.html – 看到这个

您也可以使用Impala或hive进行检查。

JDBC客户端驱动:

 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) {} } } } } 

如果你使用下面的maven是依赖…

  org.apache.phoenix phoenix-core 4.6.0-HBase-1.1