Using parameters and variables in Hive CLI

February 19, 2015

In this blog post we look at how we can address a shortcoming in the Hive ALTER TABLE statement using parameters and variables in the Hive CLI (Hive 0.13 was used).
There’s a simple way to query Hive parameter values directly from CLI
You simply execute (without specifying the value to be set):

for instance

You may use those parameters directly in your queries if you need to. For that you either use hiveconf or hivevar prefixes set and used as below:

The hivevar prefix works for custom defined variables and may be also initiated by defining –hivevar cli switch (see here https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Cli).
The following example illustrates a quick workaround for Hive shortcoming when renaming table that has a custom defined LOCATION.
In such case, after ALTER TABLE statement hive loses the previously defined LOCATION and sets the default one.
This needs to be restored by changing the LOCATION back and moving HDFS files.
So having the following script relocate.sql:

we simply call it like: