一.变量传入shell脚本并通过hive -f 方式传入变量
shell脚本test.sh:
$1 $2 两个参数是执行脚本时传入的
#! /bin/bash
start_date=$1
end_date=$2
company=$3
while [ "$start_date" -le "$end_date" ];
do
current_date=`date -d "$start_date" +%Y-%m-%d`
echo $company
echo $current_date
--hive -S -e "select * from tmp.csv_t1 limit 3;"
hive -hiveconf company=$company -hiveconf current_date=$current_date -f /var/lib/hadoop-hdfs/spride_sqoop_beijing/bi_table/tang/qt/test1.sql
start_date=$(date -d "$start_date+1days" +%Y%m%d) --循环一次日期增加一天
done
hive的sql脚本test1.sql:
select '${hiveconf:company}' as company,'${hiveconf:current_date}' as effectivedate
查看表中有这条数据,这里通过shell执行sql文件执行hive语句,同时使用变量传参
可以看到hive表中有这条数据,我这里要把它查出来
执行脚本(循环查询两天的日期,sql中未用到这个变量,并且传入公司名为 ALCOElECTRONICSLTD):
/bin/bash test.sh 20190601 20190602 ALCOElECTRONICSLTD
打印:
[root@pf-bigdata1 qt]# /bin/bash test.sh 20190601 20190602 ALCOElECTRONICSLTD
ALCOElECTRONICSLTD
2019-06-01
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
Java HotSpot