hive sql 将结果输出到变量中并用于后续使用(未完全解决)

问题场景描述:遇到会有需要反复使用的结果,比如总数,一直join就感觉不够优雅,就想着能不能把结果输出到变量中,然后利用变量就好了。

然后百度搜了半天的结果无一例外是像下面这样,先设定了变量,然后用变量去做判断。这个结果和我要的其实算是相反的?

set cur_time = '2012-01-01'
select a
from table_name
where date_time = ${hiveconf:cur_time}

无奈之下换个语言搜索,stack overflow大法好,我竟然在差评回答中找到了我要的答案
在这里插入图片描述

正解:
像写其他代码初始化变量一样输出赋值,${hiveconf:xxx}这样子检查结果(或者${hivevar:xxx}如果你设置了用户变量的话

set num = select count(1) from table_name where visitTime = current_date;
${hiveconf:num};//usage

也可以存储多个结果字段

set total = select (case when user_type = '1' then 'Apple'
else 'Orange' end) as fruit,count(1) as cnt
from store_list
group by fruit;

${hiveconf:total};

但有个问题是,这样子赋的值,无论是单个字段还是多个,他们都是以类似dataframe的形式存储在变量中的,要取出来的话不知道该怎么弄,感觉只能改成用python写?(在线蹲个大佬解答

补充:hiveconf vs. hivevar vs. system
(贴一下链接上的解释

Most of the answers here have suggested to either use hiveconf or hivevar namespace to store the variable. And all those answers are right. However, there is one more namespace.
There are total three namespaces available for holding variables.

  • hiveconf - hive started with this, all the hive configuration is stored as part of this conf. Initially, variable substitution was not part of hive and when it got introduced, all the user-defined variables were stored as part of this as well. Which is definitely not a good idea. So two more namespaces were created.
  • hivevar: To store user variables
  • system: To store system variables.

And so if you are storing a variable as part of a query (i.e. date or product_number) you should use hivevar namespace and not hiveconf namespace.
And this is how it works.
hiveconf is still the default namespace, so if you don’t provide any namespace it will store your variable in hiveconf namespace.
However, when it comes to referring a variable, it’s not true. By default it refers to hivevar namespace.
If you do not provide namespace as mentioned below, variable var will be stored in hiveconf namespace.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Hive,可以使用变量来在SQL文件传递参数值。有几种方法可以在SQL使用变量。 一种方法是使用-hivevar参数来定义变量。在SQL文件,可以使用${变量名}来引用这些变量。例如,可以使用以下命令执行SQL脚本: ``` hive --hivevar minscore=60 --hivevar maxscore=85 -S -f test.sql ``` 在test.sql文件,可以使用${minscore}和${maxscore}来引用这些变量。 另一种方法是使用-hiveconf参数来定义变量。在SQL文件,同样可以使用${变量名}来引用这些变量。例如,可以使用以下命令执行SQL脚本: ``` hive -S -d minscore=60 -d maxscore=85 -f test.sql ``` 在test.sql文件,同样可以使用${minscore}和${maxscore}来引用这些变量。 此外,在shell脚本也可以定义变量并在Hive使用。例如,可以创建一个shell脚本文件shelltest,其定义了minscore和maxscore变量,并在Hive使用这些变量: ``` #!/bin/bash minscore=60 maxscore=70 hive -S -e "use hive_test;select * from score where score >= ${minscore} and score <= ${maxscore};" ``` 然后,通过给shell脚本文件赋予执行权限并执行该脚本来运行Hive查询: ``` chmod 777 shelltest sh shelltest ``` 需要注意的是,在命令行或shell脚本定义变量时,等号左右两侧不能有空格。另外,使用-hiveconf、-hivevar或-d时,如果需要定义多个变量,每个变量前面都需要加对应的参数,一个参数只对应一个变量。在命令行使用hive -e参数调用变量时,-e参数后面的SQL语句需要使用单引号'',否则无法解析获取到变量。 #### 引用[.reference_title] - *1* *3* [hive变量使用](https://blog.csdn.net/weixin_43990245/article/details/124433738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Hive开发使用变量的两种方法](https://blog.csdn.net/peishuai1987/article/details/89882731)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值