1.Hive配置屬性
Hive配置屬性存儲于 hiveconf 命名空間中,該命名空間中的屬性是可讀寫的。在查詢語句中插入 '${hiveconf:變量名}',就可以通過 hive -hiveconf來替換變量。例如,查詢語句和執(zhí)行方式如下:
[root]$cat test.sql #查看該文件
SELECT * FROM ${hiveconf:tablename}
limit ${hiveconf:var_rows};
[root]$hive -hiveconf tablename='t1' -hiveconf var_rows=10 -f test.sql
需要注意的是:
- 如果有多個變量,每個變量前都要有參數(shù) -hiveconf
- 變量賦值等號左右不能有空格(例如var_rows=10不能有空格)
2.Hive命令行變量
Hive命令行變量,存儲于 hivevar 命名空間中,該命名空間中的變量是可讀寫的。使用方式和hive配置屬性類似,只是在查詢語句中插入的是'${hivecar:變量名}',其中命名空間"hivecar:"可以省略。例如:
[root]$cat test.sql
SELECT * FROM ${hivevar:tablename} #等同于${tablename}
limit ${hiveconf:var_rows};
[root]$hive -hivevar tablename='t1' -hiveconf var_rows=10 -f test.sql
因為命令行變量的命名空間是唯一可以省略的,因此:
- ${hivevar:變量名}等價于${變量名}
- 除了用hive -hivevar 變量賦值,還可以用hive -d,d是define的簡寫,例如下面三個執(zhí)行方式是一樣的:
[root]$hive -hivevar tablename='t1' -hiveconf var_rows=10 -f test.sql
[root]$hive -define tablename='t1' -hiveconf var_rows=10 -f test.sql
[root]$hive -d tablename='t1' -hiveconf var_rows=10 -f test.sql
其他替換變量的方法:
利用shell腳本設(shè)置hive查詢語句中的變量
利用Python替換Hive查詢語句中的變量