2017-05-26 34 views
0

我有一個配置單元表,我想用下劃線('_')替換連字符(' - ')。 示例查詢是:如何替換表名中的連字符(短劃線)?

CREATE TABLE test_${yearAndMonth} ...... 
INSERT OVERWRITE TABLE test_${yearAndMonth} ...... 

在 'yearAndMonth' 包含類似值:2017-05;所以,我想要表格名稱爲test_2017_05;然而,'年和月'必須包含連字符值。

我有嘗試:正則表達式替換 例如:

CREATE TABLE test_${regexp_replace(yearAndMonth, '-', '_')} ...... 
INSERT OVERWRITE TABLE test_${regexp_replace(yearAndMonth, '-', '_')} ...... 

但是,我得到的錯誤是:
無法識別附近 'TEST_' 輸入 '$' '{' 表吶

請任何建議。

更新: 在這種嘗試是這樣:

CREATE TABLE test_regexp_replace(${yearAndMonth}, "-", "_") ...... 
INSERT OVERWRITE TABLE test_regexp_replace(${yearAndMonth}, "-", "_") ...... 

我收到此錯誤: 疏漏之處 '(' 近 'test_regexp_replace'

+0

**(1)**您對Hive變量有什麼誤解。檢查https://stackoverflow.com/questions/42887401/storing-result-of-query-in-hive-variable/42887453#42887453。 **(2)**您必須以正確的格式傳遞變量。 –

回答

0

改變變量格式EOF配置單元不是一個好主意,嘗試在傳遞之前更改格式。做類似於下面的操作將會起作用(添加id int作爲示例列,如果需要,您可以添加自己的或從另一個變量傳遞它們)

hive --hiveconf table_name=table_$(date '+%Y')_$(date '+%m') -e "create table \${hiveconf:table_name}(id int); insert overwrite table \${hiveconf:table_name}" 
相關問題