hive变量的使用

hive常用的命令行参数

-d(--define)   定义变量
-e  		   运行命令行中的sql
-f   		   运行指定文件中的sql
-i   		   初始化sql文件
-S 		   	   静默模式,不打印mr的输出信息
-v  	  	   详细模式(将已执行的SQL回显到控制台

hiveconf定义变量

▶变量定义
	hive --hiveconf 变量名=属性值
▶变量使用
	${hiveconf:变量名} (此处的hiveconf不能省略)

1.命令行中直接使用变量

hive --hiveconf minscore=60 --hiveconf maxscore=85 -S -e 'use hive_test;select * from score where score >= ${hiveconf:minscore} and score <= ${hiveconf:maxscore};'

2.结合-f参数在sql文件中使用变量

vim hiveconf.sql
use hive_test;
select * from score where score >= ${hiveconf:minscore} and score <= ${hiveconf:maxscore};
执行sql文件
hive --hiveconf minscore=60 --hiveconf maxscore=85 -S -f hiveconf.sql

3.在hive客户端中结合 set 使用

set MIN_SCORE=60;
select * from score where score >= ${hiveconf:MIN_SCORE};

hivevar定义变量

▶变量定义
	hive --hivevar 变量名=属性值
▶变量使用
	${hivevar:变量名} (前缀hivevar可以省略,效果一样)

1.命令行中直接使用变量

hive --hivevar minscore=60 --hivevar maxscore=85 -S -e 'use hive_test;select * from score where score >= ${hivevar:minscore} and score <= ${maxscore};'

2.结合-f参数在sql文件中使用变量

vim test.sql
use hive_test;
select * from score where score >= ${minscore} and score <= ${maxscore};
执行sql脚本
hive --hivevar minscore=60 --hivevar maxscore=85 -S -f test.sql

hive -define(-d)定义变量

hive -d的定义与hivevar的变量定义相似
▶变量定义
	单个变量定义 hive -d 变量名=属性值
	多个变量定义 hive -d 变量名1=属性值 ... -d 变量名n=属性值
▶变量使用
	${变量名}

1.命令行中直接使用变量

hive -S -d minscore=60 -d maxscore=85 -e 'use hive_test;select * from score where score >= ${minscore} and score <= ${maxscore};'

2.结合-f参数在sql文件中使用变量

vim test.sql
use hive_test;
select * from score where score >= ${minscore} and score <= ${maxscore};
执行sql脚本
hive -S -d minscore=60 -d maxscore=85 -f test.sql

shell脚本中定义变量

▶变量定义
	变量定义 变量名=属性值
▶变量使用
	在hive -e中通过${变量名}使用定义的变量
vim shelltest
#!/bin/bash
minscore=60
maxscore=70
hive -S -e "use hive_test;select * from score where score >= ${minscore} and score <= ${maxscore};"
赋予文件读、写、执行权限
chmod 777 shelltest
执行shell脚本
sh shelltest

注意点

▶无论是在命令行还是在shell脚本中定义变量时,=左右两侧均不能有空格
▶使用hiveconf、hivevar或-d时,如果需要定义多个变量,每个变量前面都需要加对应的参
数,一个参数只对应一个变量
▶在命令行中用hive -e参数调用变量时,-e参数后面的sql语句需要使用单引号'',否则无法
解析获取到变量
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值