shell 脚本执行带参数的hivesql

操作的表:

hive> select * from hbase_table_1;
OK
1       zhangsan
2       lisi

hive> desc hbase_table_1;
OK
key                     int                                         
value                   string 

变量可以在 linux 命令行下定义,也可以在 hive 命令行下定义。

在命令行下定义如下:

hive> set zzz=5;
hive> set zzz;
zzz=5
hive> set system:xxx=5;
hive> set system:xxx;
system:xxx=5
hive> set system:yyy=${system:xxx};
hive> set system:yyy;
system:yyy=5
hive> set go=${hiveconf:zzz};
hive> set go;
go=5
hive> set a=1;
hive> set b=a;
hive> set b;
b=a
hive> set c=${hiveconf:${hiveconf:b}};
hive> set c;
c=1

在 CLI 中定义如下几节描述:

1、在 shell 脚本中定义变量,在 hive -e 中使用

[root@zgg ~]# cat test.sh
table="hbase_table_1"
hive -e "desc $table"

[root@zgg ~]# sh test.sh
key                     int                                         
value                   string     

2、通过 --hiveconf 定义变量

在 hive -e 中使用

[root@zgg ~]# hive --hiveconf a=1 -e 'select * from hbase_table_1 where key=${hiveconf:a};'
....
1       zhangsan

在 hive -f 中使用

[root@zgg ~]# cat test.sql
select * from hbase_table_1 where key=${hiveconf:b};

[root@zgg ~]# hive --hiveconf b=2 -f test.sql
....
2       lisi

3、通过 --hivevar 定义变量

[root@zgg ~]# hive --hivevar a=1 -e 'select * from hbase_table_1 where key=${hivevar:a};'
....
1       zhangsan
[root@zgg ~]# cat test.sql
select * from hbase_table_1 where key=${hivevar:b};

[root@zgg ~]# hive --hivevar b=2 -f test.sql
....
2       lisi
  • Create a separate namespace for managing Hive variables.
  • Add support for setting variables on the command line via '-hivevar x=y'
  • Add support for setting variables through the CLI via 'set hivevar:x=y'
  • Add support for referencing variables in statements using either '${hivevar:var_name}' or '${var_name}'
  • Provide a means for differentiating between hiveconf, hivevar, system, and environment properties in the output of 'set -v'

原文连接:https://issues.apache.org/jira/browse/HIVE-2020

4、通过 -define 定义变量

-define 可以简写为 -d

[root@zgg ~]# hive -d a=1 -e 'select * from hbase_table_1 where key=${a};'
....
1       zhangsan
[root@zgg ~]# cat test.sql
select * from hbase_table_1 where key=${b};

[root@zgg ~]# hive -d b=2 -f test.sql
....
2       lisi
posted @ 2022-01-24 17:47  DB乐之者  阅读(1098)  评论(0编辑  收藏  举报