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

how to get hive output in variable?

如何将hive查询结果放到一个变量中?在 SQL 中,我们使用以下查询。

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

@cnt 将在整个过程中使用。 我不知道如何在 HIVE 中实现相同的目标。有人可以告诉我如何在配置单元提示和 Shell 提示中实现这一点吗?

没有直接的方法可以做到这一点....

  1. 通过 shell 的一个天真的方法是:

    catchData=$(beeline/hive -u jdbc:hive2://$hiveServer2:10000 -n $userName -e "select count(*) from table")

但这并不有效,因为它甚至会连同数据一起打印配置单元连接语句、信息和警告语句

2。最好的方法是..

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

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

希望第 2 点对您有所帮助...

好吧,这很简单。 在你的 shell、

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

**hive -hivevar countNum="$countinfo" \
     -f '/Filepath/file.hql' **

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

做起来有点难度shell脚本。

我可以建议您使用 Perl 脚本吗? 因为在 Perl 中它相当简单。

您可以在 Perl 脚本中这样做。

$query_total_recs ="SELECT COUNT(*) FROM table";
#!/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

中 table 第一行的逗号分隔输出

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'