本文介绍了如何使用java api直接发送hbase shell命令如jdbc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何使用java api直接发送 hbase shell命令 像jdbc ? public static void main(String args []){ // get Connection to connect hbase 连接conn = ....; // hbase shell命令 String cmd =get't1','r1'; 语句stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(cmd); while(rs.next()){ ... } } 如果没有java api,是否有另一种方法来达到目标​​? 解决方案 请注意,Phoenix可以执行jdbc样式的查询...如果您想执行get命令,那么您可以直接使用Hbase java client api。通过shell从java执行它并不常见。 如果你仍然想从java准备获取或者在文本文件中使用 RunTime.execute 您可以执行 hbase shell< yourhbaseshelllcommands.txt> answer1 或 answer2 这样做。我已经完成了火花提交和mapreduce工作。你可以使用相同的方法来执行上面描述的hbase shell执行。 实现目标的另一种方法 要以SQL方式访问Hbase,您可以使用Phoenix。 - https://phoenix.apache.org/faq.html - see this 您也可以使用Impala或配置单元进行检查。 JDBC客户端驱动程序: import java.sql。*; public class PhoenixExample { public static void main(String [] args){ //创建变量连接连接=空; Statement statement = null; ResultSet rs = null; PreparedStatement ps = null; try { //连接数据库 connection = DriverManager.getConnection(jdbc:phoenix:localhost); //创建一个JDBC语句 statement = connection.createStatement(); //执行我们的语句 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(); //查询表 ps = connection.prepareStatement(select * from javatest); rs = ps.executeQuery(); System.out.println(表值); 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是依赖关系...... <依赖性> < groupId> org.apache.phoenix< / groupId> < artifactId> phoenix-core< / artifactId> < version> 4.6.0-HBase-1.1< / version> < /依赖关系> 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? 解决方案 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 goalTo access Hbase in SQL way you can use Phoenix. - https://phoenix.apache.org/faq.html- see thisAlso 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> 这篇关于如何使用java api直接发送hbase shell命令如jdbc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 01:21