8

I have a query whose result I wanted to store in a variable How can I do it ? I tried

./hive -e  "use telecom;insert overwrite local directory '/tmp/result' select
avg(a) from abc;"

./hive --hiveconf MY_VAR =`cat /tmp/result/000000_0`;

I am able to get average value in MY_VAR but it takes me in hive CLI which is not required and is there a way to access unix commands inside hive CLI?

6 Answers 6

15

Use Case: in mysql the following is valid:

set @max_date := select max(date) from some_table;
select * from some_other_table where date > @max_date;

This is super useful for scripts that need to repeatedly call this variable since you only need to execute the max date query once rather than every time the variable is called.

HIVE does not currently support this. (please correct me if I'm wrong! I have been trying to figure out how to do this all all afternoon)

My workaround is to store the required variable in a table that is small enough to map join onto the query in which it is used. Because the join is a map rather than a broadcast join it should not significantly hurt performance. For example:

drop table if exists var_table;

create table var_table as
select max(date) as max_date from some_table;

select some_other_table.*
from some_other_table
left join var_table
where some_other_table.date > var_table.max_date;

The suggested solution by @visakh is not optimal because stores the string 'select count(1) from table_name;' rather than the returned value and so will not be helpful in cases where you need to call a var repeatedly during a script.

2
  • is it necessary to use @ before the name of the variable? or is @ part of the variable name? should variable names start with @?
    – shrewquest
    May 19, 2016 at 3:55
  • DROP TABLE IF EXISTS temp_key; CREATE TABLE temp_key AS SELECT max(t.id) AS id FROM refined.transactions AS t; SELECT (row_number() OVER ())+tk.id as newid, t2.* FROM raw.transactions AS t2 JOIN temp_key as tk ON 1=1; Feb 28, 2017 at 15:10
7

Storing hive query output in a variable and using it in another query.

In shell create a variable with desired value by doing:

var=`hive -S -e "select max(datekey) from ....;"`
echo $var

Use the variable value in another hive query by:

hive -hiveconf MID_DATE=$var -f test.hql
2

You can simply achieve this using a shell script.

create a shell script file: avg_op.sh

#!/bin/sh
hive -e 'use telecom;select avg(a) from abc;' > avg.txt
wait
value=`cat avg.txt`
hive --hiveconf avgval=$value -e "set avgval;set hiveconf:avgval;
use telecom;
select * from abc2 where avg_var=\${hiveconf:avgval};"

execute the .sh file

>bash avg_op.sh
2

If you trying to capture the number from a Hive query or impala query in Linux, you can achieve this by executing the query and selecting numbers from the regex.

With Hive,

max=`beeline -u ${hiveConnectionUrl} -e "select max(col1) from schema_name.table_name;" | sed 's/[^0-9]*//g'`

The main part is to extract the number from the result. Also, if you're getting too large a result, you can use --silent=true flag to silent the execution which would reduce the log messages.

1

You can use BeeTamer for that. It allows to store result (or part of it) in a variable, and use this variable later in your code.

Beetamer is a macro language / macro processor that allows to extend functionality of the Apache Hive and Cloudera Impala engines.

select avg(a) from abc;
%capture MY_AVERAGE;
select * from abc2 where avg_var=#MY_AVERAGE#;

In here you save average value from you query into macro variable MY_AVERAGE and then reusing it in the second query.

1

try below :

$ var=$(hive -e "select '12' ")

$ echo $var

12 -- output

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.