【问题标题】:Hive - regexp_replace function for multiple stringsHive - 多个字符串的 regexp_replace 函数
【发布时间】:2015-02-18 12:11:53
【问题描述】:

我使用的是 hive 0.13!我想在我的数据中找到多个标记,如“hip hop”和“rock music”,并将它们替换为“hiphop”和“rockmusic” - 基本上替换它们没有空格。我在 hive 中使用了 regexp_replace 函数。以下是我的查询,它适用于上述 2 个示例。

drop table vp_hiphop;
create table vp_hiphop as
select userid, ntext,
       regexp_replace(regexp_replace(ntext, 'hip hop', 'hiphop'), 'rock music', 'rockmusic') as ntext1
from  vp_nlp_protext_males
;

但是我有 100 个这样的双元组/ngrams,并且希望能够在我只删除空格的地方有效地进行替换。我可以匹配短语 - 嘻哈和摇滚音乐,但在替换中我想简单地修剪空白。以下是我尝试过的。我也尝试将 trim 与 regexp_replace 一起使用,但它需要 regexp_replace 函数中的第三个参数。

drop table vp_hiphop;
create table vp_hiphop as
select  userid, ntext,
        regexp_replace(ntext, '(hip hop)|(rock music)') as ntext1
from  vp_nlp_protext_males
;

【问题讨论】:

    标签: regex hadoop hive hiveql


    【解决方案1】:

    您可以使用TRANSLATE 函数从字符串中去除所有出现的子字符串,以将子字符串替换为空字符串。对于您的查询,它将变为:

    drop table vp_hiphop;
    create table vp_hiphop as
    select  userid, ntext,
            translate(ntext, ' ', '') as ntext1
    from  vp_nlp_protext_males
    ;
    

    【讨论】:

      猜你喜欢
      • 2016-02-23
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2019-04-01
      • 2020-01-06
      • 2020-02-15
      • 2018-07-09
      相关资源
      最近更新 更多