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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With