如何使用Java api像jdbc一样直接发送hbase shell命令?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用Java api像jdbc一样直接发送hbase shell命令?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2319字,纯文字阅读大概需要4分钟。
内容图文
如何使用Java api像jdbc一样直接发送hbase shell命令?
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执行它是不常见的做法.
如果您仍然想从Java中准备文本文件中的命令或命令清单并使用RunTime.execute,则可以执行hbase shell< yourhbaseshelllcommands.txt>.
看到我的answer1或answer2可以做到这一点.我已经完成了spark提交和mapreduce工作.您可以对hbase shell执行与上述相同的方法.
another way to achieve the goal
要以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) {
// 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,则以下是依赖项…
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.6.0-HBase-1.1</version>
</dependency>
内容总结
以上是互联网集市为您收集整理的如何使用Java api像jdbc一样直接发送hbase shell命令?全部内容,希望文章能够帮你解决如何使用Java api像jdbc一样直接发送hbase shell命令?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。