将多个日期作为参数传递给 Hive 查询
passing multiple dates as a paramters to Hive query
我正在尝试将日期列表作为参数传递给我的配置单元查询。
#!/bin/bash
echo "Executing the hive query - Get distinct dates"
var=`hive -S -e "select distinct substr(Transaction_date,0,10) from test_dev_db.TransactionUpdateTable;"`
echo $var
echo "Executing the hive query - Get the parition data"
hive -hiveconf paritionvalue=$var -e 'SELECT Product FROM test_dev_db.TransactionMainHistoryTable where tran_date in("${hiveconf:paritionvalue}");'
echo "Hive query - ends"
输出为:
Executing the hive query - Get distinct dates
2009-02-01 2009-04-01
Executing the hive query - Get the parition data
Logging initialized using configuration in file:/hive/conf/hive-log4j.properties
OK
Product1
Product1
Product1
Product1
Product1
Product1
Time taken: 0.523 seconds, Fetched: 6 row(s)
Hive query - ends
它只接受第一个日期作为输入。我想将我的日期传递为 ('2009-02-01','2009-04-01')
Note:TransactionMainHistoryTable 在字符串类型的 tran_date 列上分区。
使用 collect_set
收集不同值的数组,并将其与分隔符 ','
连接起来。这将生成没有外引号 2009-02-01','2009-04-01
的列表,并且在第二个脚本中也添加外引号 '
,或者您可以在第一个查询中添加它们。并且在内联 sql(-e 选项)中执行时,您不需要传递 hiveconf 变量,直接 shell 变量替换就可以了。从文件执行脚本时使用 hiveconf(-f 选项)
工作示例(使用您的 table 而不是堆栈):
date_list=$(hive -S -e "select concat_ws('\',\'',collect_set(substr(dt,0,10))) from (select stack (2,'2017-01', '2017-02')as dt)s ;")
hive -e "select * from (select stack (2,'2017-01', '2017-02')as dt)s where dt in ('${date_list}');"
Returns:
好的
2017-01
2017-02
Time taken: 1.221 seconds, Fetched: 2 row(s)
我正在尝试将日期列表作为参数传递给我的配置单元查询。
#!/bin/bash
echo "Executing the hive query - Get distinct dates"
var=`hive -S -e "select distinct substr(Transaction_date,0,10) from test_dev_db.TransactionUpdateTable;"`
echo $var
echo "Executing the hive query - Get the parition data"
hive -hiveconf paritionvalue=$var -e 'SELECT Product FROM test_dev_db.TransactionMainHistoryTable where tran_date in("${hiveconf:paritionvalue}");'
echo "Hive query - ends"
输出为:
Executing the hive query - Get distinct dates
2009-02-01 2009-04-01
Executing the hive query - Get the parition data
Logging initialized using configuration in file:/hive/conf/hive-log4j.properties
OK
Product1
Product1
Product1
Product1
Product1
Product1
Time taken: 0.523 seconds, Fetched: 6 row(s)
Hive query - ends
它只接受第一个日期作为输入。我想将我的日期传递为 ('2009-02-01','2009-04-01') Note:TransactionMainHistoryTable 在字符串类型的 tran_date 列上分区。
使用 collect_set
收集不同值的数组,并将其与分隔符 ','
连接起来。这将生成没有外引号 2009-02-01','2009-04-01
的列表,并且在第二个脚本中也添加外引号 '
,或者您可以在第一个查询中添加它们。并且在内联 sql(-e 选项)中执行时,您不需要传递 hiveconf 变量,直接 shell 变量替换就可以了。从文件执行脚本时使用 hiveconf(-f 选项)
工作示例(使用您的 table 而不是堆栈):
date_list=$(hive -S -e "select concat_ws('\',\'',collect_set(substr(dt,0,10))) from (select stack (2,'2017-01', '2017-02')as dt)s ;")
hive -e "select * from (select stack (2,'2017-01', '2017-02')as dt)s where dt in ('${date_list}');"
Returns:
好的
2017-01
2017-02
Time taken: 1.221 seconds, Fetched: 2 row(s)