如何使用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
- 记录MapReduce作业的标准做法
- 如何将.txt / .csv文件转换为ORC格式
- Hadoop gzip压缩文件
- “hadoop namenode -format”返回java.net.UnknownHostException
- 在值上迭代两次(MapReduce)
- Hadoop:interval和JOIN
- 线程“main”中的exceptionjava.lang.NoClassDefFoundError:org / apache / hadoop / hbase / HBaseConfiguration
- 在没有SPnego的情况下访问kerberos可以保护WebHDFS
- 在本地/远程集群上运行java hadoop作业