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语句需要使用单引号'',否则无法
解析获取到变量