Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Hive query after substitutions (to debug it)?

Tags:

hive

hiveql

In order to avoid duplication or parameterize a query we can compose a query from many parts via substitution. However we might want to see the resulting query and check if it is correct. How to do it?

Consider a very minimal query:

hive> set hivevar:foo=bar;
hive> select "${foo}";

UPDATE:

Although hive -v prints the query to be executed the substitutions are not resolved:

It echoes the query but unfortunately without substitutions resolved:

$ hive -v
hive> set hivevar:foo=bar;
set hivevar:foo=bar
hive> set hivevar:query=select "${foo}";
set hivevar:query=select "${foo}"
hive> select "${foo}";
select "${foo}"
OK
bar
Time taken: 2.062 seconds, Fetched: 1 row(s)

Moreover I'd like a solution that works primarily in beeline, since hive CLI is deprecated.

like image 576
Bohumir Zamecnik Avatar asked Nov 16 '25 15:11

Bohumir Zamecnik


1 Answers

Since variable substitution in Hive is based on plain strings we can:

  • store a query in another variable:

    hive> set hivevar:foo=bar;
    hive> set hivevar:query=select "${foo}";
    
  • then print it (note that all substitutions are resolved):

    hive> set query;
    query=select "bar"
    
  • and execute it:

    hive> ${query};
    OK
    bar
    Time taken: 0.131 seconds, Fetched: 1 row(s)
    
like image 58
Bohumir Zamecnik Avatar answered Nov 19 '25 09:11

Bohumir Zamecnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!