本文目錄一覽:
如何在Java中執行Hive命令或HiveQL
這裡所說的在Java中執行Hive命令或HiveQL並不是指Hive
Client通過JDBC的方式連接HiveServer(or
HiveServer2)執行查詢,而是簡單的在部署了HiveServer的伺服器上執行Hive命令。當然這是一個簡單的事情,平常我們通過Hive做簡單的數據分析實驗的時候,都是直接進入Hive執行HiveQL
通過進入Hive執行HiveQL,只能將分析結果列印到屏幕或是存入臨時表,如果想把分析結果寫入文件,或者對分析結果做進一步的分析,用程序做分析,就是為什麼要在Java中執行Hive命令。
Java在1.5過後提供了ProcessBuilder根據運行時環境啟動一個Process調用執行運行時環境下的命令或應用程序(1.5以前使用Runtime),關於ProcessBuilder請參考Java相關文檔。調用代碼如下:
String
sql=”show
tables;
select
*
from
test_tb
limit
10″;
ListString
command
=
new
ArrayListString();
command.add(“hive”);
command.add(“-e”);
command.add(sql);
ListString
results
=
new
ArrayListString();
ProcessBuilder
hiveProcessBuilder
=
new
ProcessBuilder(command);
hiveProcess
=
hiveProcessBuilder.start();
BufferedReader
br
=
new
BufferedReader(new
InputStreamReader(
hiveProcess.getInputStream()));
String
data
=
null;
while
((data
=
br.readLine())
!=
null)
{
results.add(data);
}其中command可以是其它Hive命令,不一定是HiveQL。
java中怎麼實現查詢出hive下所有資料庫下表名
try {
Class.forName(“org.apache.hadoop.hive.jdbc.HiveDriver”);
String selectSql = “select * from db.data where address = ‘11111111’”;
Connection connect = DriverManager.getConnection(“jdbc:hive://192.168.xx.xx:10000/db”, “xxx”, “xxx”);
PreparedStatement state = null;
state = connect.prepareStatement(selectSql);
ResultSet resultSet = state.executeQuery();
while (resultSet != null resultSet.next()) {
System.out.println(resultSet.getString(1) + ” ” + resultSet.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
hive 需要寫java代碼嗎
如果你的項目是java項目的話,就需要使用hive提供的java api,如下代碼:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
/**
* Hive的JavaApi
*
* 啟動hive的遠程服務介面命令行執行:hive –service hiveserver /dev/null 2/dev/null
*
* @author 吖大哥
*
*/
public class HiveJdbcCli {
private static String driverName = “org.apache.hadoop.hive.jdbc.HiveDriver”;
private static String url = “jdbc:hive://hadoop3:10000/default”;
private static String user = “hive”;
private static String password = “mysql”;
private static String sql = “”;
private static ResultSet res;
private static final Logger log = Logger.getLogger(HiveJdbcCli.class);
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
conn = getConn();
stmt = conn.createStatement();
// 第一步:存在就先刪除
String tableName = dropTable(stmt);
// 第二步:不存在就創建
createTable(stmt, tableName);
// 第三步:查看創建的表
showTables(stmt, tableName);
// 執行describe table操作
describeTables(stmt, tableName);
// 執行load data into table操作
loadData(stmt, tableName);
// 執行 select * query 操作
selectData(stmt, tableName);
// 執行 regular hive query 統計操作
countData(stmt, tableName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
log.error(driverName + ” not found!”, e);
System.exit(1);
} catch (SQLException e) {
e.printStackTrace();
log.error(“Connection error!”, e);
System.exit(1);
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void countData(Statement stmt, String tableName)
throws SQLException {
sql = “select count(1) from ” + tableName;
System.out.println(“Running:” + sql);
res = stmt.executeQuery(sql);
System.out.println(“執行「regular hive query」運行結果:”);
while (res.next()) {
System.out.println(“count ——” + res.getString(1));
}
}
private static void selectData(Statement stmt, String tableName)
throws SQLException {
sql = “select * from ” + tableName;
System.out.println(“Running:” + sql);
res = stmt.executeQuery(sql);
System.out.println(“執行 select * query 運行結果:”);
while (res.next()) {
System.out.println(res.getInt(1) + “\t” + res.getString(2));
}
}
private static void loadData(Statement stmt, String tableName)
throws SQLException {
String filepath = “/home/hadoop01/data”;
sql = “load data local inpath ‘” + filepath + “‘ into table ”
+ tableName;
System.out.println(“Running:” + sql);
res = stmt.executeQuery(sql);
}
private static void describeTables(Statement stmt, String tableName)
throws SQLException {
sql = “describe ” + tableName;
System.out.println(“Running:” + sql);
res = stmt.executeQuery(sql);
System.out.println(“執行 describe table 運行結果:”);
while (res.next()) {
System.out.println(res.getString(1) + “\t” + res.getString(2));
}
}
private static void showTables(Statement stmt, String tableName)
throws SQLException {
sql = “show tables ‘” + tableName + “‘”;
System.out.println(“Running:” + sql);
res = stmt.executeQuery(sql);
System.out.println(“執行 show tables 運行結果:”);
if (res.next()) {
System.out.println(res.getString(1));
}
}
private static void createTable(Statement stmt, String tableName)
throws SQLException {
sql = “create table ”
+ tableName
+ ” (key int, value string) row format delimited fields terminated by ‘\t'”;
stmt.executeQuery(sql);
}
private static String dropTable(Statement stmt) throws SQLException {
// 創建的表名
String tableName = “testHive”;
sql = “drop table ” + tableName;
stmt.executeQuery(sql);
return tableName;
}
private static Connection getConn() throws ClassNotFoundException,
SQLException {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
}
java連接Hive的幾種方式
2、JDBC連接的方式,當然還有其他的連接方式,比如ODBC等, 這種方式很常用,可以在網上隨便找到,就不再累贅了。不穩定,經常會被大數據量沖掛,不建議使用。 3、這種方式是直接利用Hive的 Driver class 來直接連接,感覺這種方式不通過JDBC,應該速度會比較快一點(未經驗證)。我只是在local模式下測試過。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/181552.html