hive傳遞參數與調用


在運行hive命令時傳入參數,使用-hivevar 和 -hiveconf 兩種參數選項,來給此次的執行腳本傳入參數

-hivevar : 傳參數 ,專門提供給用戶自定義變量。

-hiveconf : 傳參數,包括了hive-site.xml中配置的hive全局變量。

 (1)腳本調用

test.sql

use default;
select 
'${hiveconf:month}' as month
from default.user1;
use default;
select 
'${hivevar:month}' as month
from default.user1;

 

start.sh

#!/bin/sh
#ecoding=utf-8

set -x
month=`date -d 'last month' +%Y-%m`
hive --hiveconf month=$month -f test.sql
#!/bin/sh
#ecoding=utf-8

set -x
month=`date -d 'last month' +%Y-%m`
hive --hivevar month=$month -f test.sql

 (2)直接傳參

bash:hive -f 參數傳遞,執行文件

CDH 集群

  命令: hive --hivevar v_date='20170630' -S -f test1.hql    -- 返回3條記錄

  命令: hive --hivevar v_date='20170101' -S -f test1.hql    -- 返回0條記錄

FI集群:

      命令: beeline --hivevar v_date='20170630' -S -f test1.hql    -- 返回3條記錄

 

查看文件內容:

  cat test1.hql

    use default; select * from logs where dt='${hivevar:v_date}' limit 3;

  cat test2.hql 

    use default; select * from logs limit 3;

-------------------------------------------------------更正規的解釋------------------------------------------------------

1. hiveconf

hiveconf用於定義HIVE執行上下文的屬性(配置參數),可覆蓋覆蓋hive-site.xml(hive-default.xml)中的參數值,如用戶執行目錄、日志打印級別、執行隊列等,常用的配置屬性如下:

參數名稱 參數解釋
hive.metastore.warehouse.dir 啟動時指定用戶目錄,不同的用戶不同的目錄
hive.cli.print.current.db 顯示當前數據庫
hive.root.logger 輸出日志信息
hive.cli.print.header 顯示列名稱
mapred.job.queue.name 執行隊列名稱

如果熟悉HIVE操作的話,這些配置屬性還可以利用“set”指令進行修正,如下:

# 首先啟動HIVE
hive
# 然后設置參數
set mapred.job.queue.name=root.default

上面的指令可等價於“hive --hiveconf”命令,如下:

hive --hiveconf "mapred.job.queue.name=root.default"

2. hivevar

hivevar用於定義HIVE運行時的變量替換,類似於JAVA中的“PreparedStatement”,與“${}”配合使用,示例如下:

#  定義變量,並啟動HIVE CLI
hive  --hivevar my="201809" --database deafult -e 'select * from a1 where concat(year, month) = ${my} limit 10';

 

3. define

define與hivevar用途完全一樣,還有一種簡寫“-d”,示例如下:

#   定義變量
hive --hiveconf "mapred.job.queue.name=root.default" -d my="201809" --database default -e
'select * from mydb where concat(year, month) = ${my} limit 10';

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM