hive/shell:变量:基本使用

本文介绍了如何在Shell脚本中传递变量给Hive SQL脚本,包括通过`hive -f`方式和直接在Shell脚本中使用`hive -e`执行SQL语句。示例中展示了如何在不同场景下利用变量查询Hive表数据。

一.变量传入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
[root@hadoop1 bin]# hive SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/servers/hive-3.1.3/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/servers/hadoop-3.3.0/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] /usr/bin/which: no hbase in (/export/servers/hive-3.1.3/bin:/export/servers/flume-1.9.0/bin:/root/.local/bin:/root/bin:/export/servers/flume-1.9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/export/servers/jdk1.8.0_241/bin:/export/servers/hadoop-3.3.0/bin:/export/servers/hadoop-3.3.0/sbin:/export/servers/jdk1.8.0_241/bin:/export/servers/hadoop-3.3.0/bin:/export/servers/hadoop-3.3.0/sbin) SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/servers/hive-3.1.3/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/servers/hadoop-3.3.0/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] Hive Session ID = c684811f-5c32-43ba-b621-c41d4ecf957a Logging initialized using configuration in jar:file:/export/servers/hive-3.1.3/lib/hive-common-3.1.3.jar!/hive-log4j2.properties Async: true Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases. hive (default)>
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值