HiveQL:使用查询结果作为变量 [英] HiveQL: Using query results as variables

查看:2130
本文介绍了HiveQL:使用查询结果作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hive中我想从表中动态提取信息,将其保存在一个变量中并进一步使用它。请考虑以下示例,其中检索列var的最大值并希望将其用作后续查询中的条件。

 设置maximo =从表中选择max(var); 


表格
中选择
*
其中
var = $ {hiveconf:maximo}
  



设置maximo =从表中选择max(var);

$ {hiveconf:maximo}

显示预期结果。 / p>

执行操作:

  select'$ {hiveconf:maximo}'

给出

 <$ c 

尽管。$ b $select max(var)from table
b

Best

解决方案

Hive按原样替换变量并且不执行它们。使用shell包装脚本将结果导入变量并将其传递给Hive脚本。

  maximo = $(hive -eset hive.cli.print.header = false;从表中选择max(var) ;)
hive -hiveconfmaximo=$ maximo-f your_hive_script.hql

在脚本之后,你可以使用选择'$ {hiveconf:maximo}'


in Hive I'd like to dynamically extract information from a table, save it in a variable and further use it. Consider the following example, where I retrieve the maximum of column var and want to use it as a condition in the subsequent query.

set maximo=select max(var) from table;

select
  *
from
  table
where
  var=${hiveconf:maximo}

It does not work, although

set maximo=select max(var) from table;

${hiveconf:maximo}

shows me the intended result.

Doing:

select '${hiveconf:maximo}'

gives

"select max(var) from table"

though.

Best

解决方案

Hive substitutes variables as is and does not execute them. Use shell wrapper script to get result into variable and pass it to your Hive script.

maximo=$(hive -e "set hive.cli.print.header=false; select max(var) from table;")
hive -hiveconf "maximo"="$maximo" -f your_hive_script.hql

And after this inside your script you can use select '${hiveconf:maximo}'

这篇关于HiveQL:使用查询结果作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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