【问题标题】:Equivalent of Presto REPLACE function in HiveHive 中 Presto REPLACE 函数的等价物
【发布时间】:2021-04-19 01:41:35
【问题描述】:

我目前有如下情况

replace(replace(replace(replace(replace(replace(replace(replace(UPPER(FIELDNAME),' ',''),'LLC',''),'INC',''),'INTERNATIONAL',''),'LTD',''),'.',''),',',''),'QMT','')

在 Hive 中服务于完全相同的目的的等效功能是什么? regexp_replace 是否适用于上述场景?

在上述情况下使用了 regexp_replace 而不是替换,但它给我抛出了以下错误。

FAILED: SemanticException [Error 10014]: Line 1:255 Wrong arguments ''('': No matching method for class org.apache.hadoop.hive.ql.udf.UDFRegExpReplace with (string, string). Possible choices: _FUNC_(string, string, string) (state=42000,code=10014)

如果有人能在这方面提供帮助,那就太好了。谢谢

【问题讨论】:

    标签: hive presto trino


    【解决方案1】:

    确实如此。 regexp_replace 是您可以使用的命令。您可以将要替换的子字符串列表作为由“|”分隔的列表并且您需要使用 '\' 来提供任何可能充当通配符的特殊字符,例如 - 。要么 ?或 * 等。下面是示例用法。

    SELECT regexp_replace(upper(fieldname),'LTD|QMT|INTERNATIONAL|ORG|INC|\\.|,| ','') from my_table;
    

    【讨论】:

    • 上面提到的请求可以正常工作,但有一个不同的条件,如下所示,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(upper(pod.fieldname),' ',''),'.',''),')',''),'('),',',''),'-',''),'/',''),'&',''),'_',''),'''','')。这会在 '''','' 周围引发错误。原帖中显示的错误。有什么解决方法吗?
    • 在你的替换函数中,你忘记使用字符串替换'('。在下面使用:replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(upper))) (pod.fieldname),'',''),'.',''),')',''),'(',''),',',''),'-',' '),'/',''),'&',''),'_',''),'''','')
    • 我实际上也为此使用了 regexp_replace 并且它有效。唯一的缺点是。当我尝试在 yaml 中解析它时不起作用。所以我必须使用 translate(regexp_replace(fieldname), |,|-|_|,''),'.','')
    猜你喜欢
    • 2019-01-21
    • 2022-07-28
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2017-03-18
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多