如何在变量中获取配置单元输出?

p1tboqfb  于 2021-05-30  发布在  Hadoop
关注(0)|答案(5)|浏览(309)

如何将配置单元查询结果放入一个变量中?在sql中,我们使用下面的查询。

Declare @cnt as int Select @cnt= count(*) from tabname

@cnt将在整个过程中使用。我不知道如何在Hive里达到同样的效果。有人能告诉我如何在hive提示符和shell提示符中实现这一点吗?

oiopk7p5

oiopk7p51#


# !/bin/bash -e

hive -e "use schema_name; INSERT OVERWRITE DIRECTORY '/tmp/ank' row format delimited 
FIELDS TERMINATED BY ',' select * from table_name limit 1;"

a=$(hdfs dfs -cat /tmp/ank/000000_0)
echo $a

这将给出变量a中表的第一行以逗号分隔的输出

xeufq47z

xeufq47z2#

last_processed_time=`hive -S -e "USE db; select MAX(processed_time) from job_details where event_id = 'XXXX'"`

`echo $last_processed_time
pig -useHCatalog -Dmapred.job.queue.name=highPriority -Dtez.queue.name=highPriority -x tez -param last_processed_time=$last_processed_time test.pig`

> After executing above shell , It displays the required MAX value as
> well as pass the same value to Pig as external parameter for internal
> use within pig. Hope this will be helpful to make the same case with
> hive call using 'hiveconf'
jgzswidk

jgzswidk3#

没有直接的方法可以做到这一点。。。。
通过shell的一种简单方法是: catchData=$(beeline/hive -u jdbc:hive2://$hiveServer2:10000 -n $userName -e "select count(*) from table") 但这是无效的,因为它甚至会打印hiveconnect语句、info&warn语句以及数据
2最好的办法是。。

INSERT OVERWRITE DIRECTORY <-directory location}->
SELECT COUNT(*) FROM TABLE

countinfo=$(hdfs dfs -cat <-directory location}->)

希望这一点2有帮助。。。

332nm8kg

332nm8kg4#

嗯,这很简单。在你的壳里,

countinfo=$(hdfs dfs -cat <-directory location}->) 

**hive -hivevar countNum="$countinfo" \

     -f '/Filepath/file.hql'**

在您的file.hql中使用 ${countNum} 在您的查询中。

vzgqcmou

vzgqcmou5#

用shell脚本做这件事有点困难。
我可以建议您使用perl脚本吗?因为在perl中这是相当容易的。
您可以在perl脚本中这样做。

$query_total_recs ="SELECT COUNT(*) FROM table";

相关问题