将多个日期作为参数传递给Hive查询 [英] passing multiple dates as a paramters to Hive query

查看:664
本文介绍了将多个日期作为参数传递给Hive查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to pass a list of dates as parameter to my 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') 注意:TransactionMainHistoryTable在tran_date列上以字符串类型进行了分区.

It's only taking only first date as input. I would like to pass my dates as ('2009-02-01','2009-04-01') Note:TransactionMainHistoryTable is partitioned on tran_date column with string type.

推荐答案

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

Collect array of distinct values using collect_set and concatenate it with delimiter ','. This will produce list without outer quotes 2009-02-01','2009-04-01 and in the second script add outer quotes ' also, or you can add them in the first query. And when executing in inline sql (-e option) you do not need to pass hiveconf variable, direct shell variable substitution will work. Use hiveconf when you are executing script from file (-f option)

工作示例:

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)

这篇关于将多个日期作为参数传递给Hive查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆