javahive,javahive的config

本文目錄一覽:

如何在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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-23 06:41
下一篇 2024-11-23 06:41

相關推薦

  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分散式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25
  • Javahive:打破桎梏,自由編程

    一、Javahive是什麼 Javahive是一款集成化的、能夠進行多語言編程的開發平台。它支持Java、Python、JavaScript等多種編程語言,同時具備強大的編程輔助工…

    編程 2025-04-24
  • 深入理解nginx.config配置文件

    一、nginx.config文件結構 nginx是一款高性能的Web伺服器,運行在Linux下,其配置文件位於「/etc/nginx/nginx.conf」。nginx.confi…

    編程 2025-04-23
  • 深入理解postcss.config.js

    一、postcss.config.js是什麼 postcss.config.js是一個JavaScript文件,它用於配置PostCSS的插件和選項。它可以被用來處理CSS,從而增…

    編程 2025-02-25
  • Python Config模塊

    一、 Config模塊概述 Python的config模塊是一個很常用的處理配置文件的庫。 Python config模塊提供了一個方便的方法來讀寫 INI 配置文件。 Pytho…

    編程 2025-01-20
  • config.json文件(configjson報錯)

    本文目錄一覽: 1、config.json是什麼文件 2、伺服器參數配置文件config.json該怎麼設置 3、微信小程序(上) 4、python中四種配置文件 5、OrthoF…

    編程 2025-01-11
  • config.php配置mysql(configsamplephp)

    本文目錄一覽: 1、apache php mysql配置 2、安裝Phorum論壇時,怎樣在config.php配置mysql連接? 3、config.php文件的配置問題!! 4…

    編程 2025-01-05
  • Git查看config

    一、基本介紹 在使用git進行版本控制時,我們需要使用git config來配置全局以及項目級別的變數,比如用戶名和郵箱等。那麼如何查看已經配置好的git config呢? 首先,…

    編程 2025-01-05
  • 包含config.json門羅幣的詞條

    本文目錄一覽: 1、xml幣怎麼挖 2、config.json是什麼文件 3、如何修改遊戲mod中後綴為json的文件? xml幣怎麼挖 具體步驟如下: 門羅幣xmr挖礦需要準備兩…

    編程 2024-12-29
  • layui.config詳解

    一、layui.config什麼意思 在了解layui.config之前,我們需要先了解 layui 靜態文件載入器。它是一個簡化的資源載入器,可以自動為模塊分析和處理其依賴關係。…

    編程 2024-12-26

發表回復

登錄後才能評論