如何替换 table 名称中的连字符(破折号)?

How to replace hyphen (dash) in table name?

我有一个配置单元 table,我想用下划线 ('_') 替换连字符 ('-')。 示例查询如下:

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

'yearAndMonth' 包含如下值:2017-05;所以,我想将 table 值名称设为 test_2017_05;但是,'yearAndMonth' 必须包含连字符值。

我试过:正则表达式替换 例如:

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

但是,我得到的错误是:
无法识别 'test_' '$' '{' in table name

附近的输入

有什么建议吗

更新: 以这种方式尝试:

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

我收到此错误: 在 'test_regexp_replace'

附近的“(”处缺少 EOF

更改hive中的变量格式不是一个好主意,尝试在通过之前更改格式。执行类似于下面的操作(添加 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}"