【问题标题】:passing multiple dates as a paramters to Hive query将多个日期作为参数传递给 Hive 查询
【发布时间】:2025-11-26 13:10:02
【问题描述】:

我正在尝试将日期列表作为参数传递给我的配置单元查询。

#!/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') 注意:TransactionMainHistoryTable 以字符串类型在 tran_date 列上进行分区。

【问题讨论】:

  • 在您的选择查询本身中制作您想要的输出。在您的第一个 select 语句中添加引号和逗号。
  • 有没有办法使用shell创建列表或数组?

标签: shell hive parameters hiveql hadoop-partitioning


【解决方案1】:

使用collect_set 收集不同值的数组,并将其与分隔符',' 连接起来。这将生成没有外引号2009-02-01','2009-04-01 的列表,并在第二个脚本中添加外引号',或者您可以在第一个查询中添加它们。并且在内联 sql(-e 选项)中执行时,您不需要传递 hiveconf 变量,直接 shell 变量替换将起作用。从文件执行脚本时使用 hiveconf(-f 选项)

工作示例(使用您的表格而不是堆栈):

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}');"

返回:

好的

2017-01
2017-02
Time taken: 1.221 seconds, Fetched: 2 row(s)

【讨论】:

  • @Vikrant,我没有检查它,稍后会做,希望你明白。我的回答中的引号可能有些混乱,也许有必要进行一些屏蔽。
  • @leftjoin-- 谢谢。我对第一个查询进行了一些更改,因为 select concat("'",(concat_ws("','",collect_set(substr(Transaction_date,0,10)))),"'") from test_dev_db.TransactionUpdateTable;它给我的输出为 '2009-02-01','2009-04-01' 但在通过 shell 执行时遇到问题。
  • @vikrantrana 已修复。我从堆栈中选择,因为我没有这样的表。请改用您的查询,
  • @vikrantrana 终于找到了类似的问题:*.com/a/53279839/2700344