Specify default parameter values within a hive scr

2019-06-02 17:01发布

问题:

I am aware that it is possible to specify parameters for a hive query/script like so:

>hive -e "USE uk_pers_dev;set hive.cli.print.header=true;CREATE TABLE IF NOT EXISTS ${hiveconf:tablename} (mycol int);SELECT * FROM ${hiveconf:tablename};" -hiveconf tablename=mytable;

However what I would like to do is specify a default parameter value within my hive script that should be used in case no value is passed from the command-line. Is that possible?

回答1:

JT

Could you use

SET myvar = "Hello World"

inside your hive script do

SET myvar = COALESCE(${hiveconf:myvar},"Default");

SELECT * FROM MyTable WHERE MyColumn = ${hiveconf:myvar};

OR

SELECT * FROM MyTable WHERE MyColumn = COALESCE(${hiveconf:myvar},"Default");



回答2:

OK I have a table called airports

name: string country: string area_code int

in HIVE I do

SET mycol = name;
SELECT ${hiveconf:mycol} from airports;

works for me.



回答3:

OK So take the logic away from Hive

In POSH or a cmd file

  1. Do the check
  2. Substitute if needed
  3. Call Hive

Maybe look at that as a wrapper.

Allan



标签: hive