尝试用HQL中的变量替换HIVE字符串文字

时间:2017-05-31 04:46:49

标签: hadoop hive hdfs hiveql

您好我有一个简单的创建表语句...

set location_stage=${env:HDFS_STAGING_DIR};
CREATE EXTERNAL TABLE IF NOT EXISTS stage.test(
TEST_ID INT,
TEST_NAME STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
   "separatorChar" = ",",
   "quoteChar"     = "\"",
   "escapeChar"    = "\\")
STORED AS TEXTFILE
LOCATION "${hivevar:location_stage}";

我不需要'hivevar:location_stage' - 我只需要'env:HDFS_STAGING_DIR'。但是,两者都不起作用。我在“LOCATION”条款中都使用了它们。我用命令行运行

hive -f test_hive_variable.sql

但我收到以下错误......

FAILED: IllegalArgumentException java.net.URISyntaxException: Relative path in absolute URI: ${hivevar:location_stage%7D

hive版本是Hive 1.2.1000.2.4.2.0-258

1 个答案:

答案 0 :(得分:1)

您的脚本可以是:

CREATE EXTERNAL TABLE IF NOT EXISTS stage.test(
TEST_ID INT,
TEST_NAME STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
   "separatorChar" = ",",
   "quoteChar"     = "\"",
   "escapeChar"    = "\\")
STORED AS TEXTFILE
LOCATION "${HDFS_STAGING_DIR}";

然后传递HDFS_STAGING_DIR,您可以使用:

hive -f test_hive_variable.hql --hivevar HDFS_STAGING_DIR="/user/hive/warehouse/staging_dir"