Using parameters and variables in Hive CLI

Uli Bethke

Uli has been rocking the data world since 2001. As the Co-founder of Sonra, the data liberation company, he’s on a mission to set data free. Uli doesn’t just talk the talk—he writes the books, leads the communities, and takes the stage as a conference speaker.

Any questions or comments for Uli? Connect with him on LinkedIn.


Published on February 19, 2015
Updated on November 20, 2024

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:

Uli Bethke

About the author:

Uli Bethke

Co-founder of Sonra

Uli has been rocking the data world since 2001. As the Co-founder of Sonra, the data liberation company, he’s on a mission to set data free. Uli doesn’t just talk the talk—he writes the books, leads the communities, and takes the stage as a conference speaker.

Any questions or comments for Uli? Connect with him on LinkedIn.