------------------ --------1 执行不带参数的hql文件----------------------------
hive -f 文件名.后缀
实例:hive -f chensq_test1.hql
------------------ -----2 执行带1个普通参数的hql文件----------------------------
----- 直接在hive中执行----
select count(*)
from 用户名.表名 c
where c.列名 = '01' ;
----- hql文件内容------------
select count(*)
from 用户名.表名 c
where c.列名 = '${hiveconf:param1}' ;
---- 用命令执行文件
hive -hiveconf param1='01' -f test_param1.hql
----------------------3 执行带2个参数的hql文件----------------------------
----- 直接在hive中执行----
select count(*)
from 用户名.表名 c
where c.列名1 = '01'
and 列名2 = '61405'
----- hql文件内容------------
select count(*)
from 用户名.表名 c
where c.列名1 = '${hiveconf:param1}'
and 列名2 = '${hiveconf:param2}';
---- 用命令执行文件
hive -hiveconf param1=$'01' -hiveconf param2=$'61405' -f test_param2.hql
-----------参数传递实例4(执行与时间相关的hql文件):其中日期变量为日期格式
----- 直接在hive中执行----
select count(*)
from 用户名.表名 c
where c.date1 <= from_unixtime(unix_timestamp()}, 'yyyy-MM-dd HH:dd:ss')
and c.date1 >= add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:dd:ss'),-2);
----- hql文件内容------------
select count(*)
from 用户名.表名 c
where c.date1 <= from_unixtime(unix_timestamp()}, 'yyyy-MM-dd HH:dd:ss')
and c.date1 >= add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:dd:ss'),-${hiveconf:param2});
------用命令执行文件
hive -hiveconf param2=2 -f test_param3.hql
-----------参数传递实例5(执行与时间相关的hql文件):其中日期变量为字符串格式
----- 直接在hive中执行----
select count(*)
from 用户名.表名 a
where a.年月_col <= substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-1)), '-','') , 1 , 6)
and a.年月_col >= substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-12)), '-','') , 1 , 6);
----- hql文件内容------------
select count(*)
from 用户名.表名 a
where a.年月_col <= substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-${hiveconf:param1})), '-','') , 1 , 6)
and a.年月_col >= substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-${hiveconf:param2})), '-','') , 1 , 6);
---- 用命令执行文件
hive -hiveconf param1=$1 -hiveconf param2=2 -f test_param2.hql