【问题标题】:hive regexp_extract after second occurrence of delimiter第二次出现分隔符后配置单元 regexp_extract
【发布时间】:2018-08-16 15:26:06
【问题描述】:

我们有一个 Hive 表列,其中的字符串由 ';' 分隔我们需要在第二次出现';'后提取字符串

+-----------------+
| col1            |
+-----------------+
| a;b;c;d         |
| e;f; ;h         |
| i;j;k;l         |  
+-----------------+

Required output:

+-----------+
| col1      |
+-----------+
| c         |
| <null>    |
| k         |  
+-----------+

选择正则表达式提取

【问题讨论】:

    标签: hive


    【解决方案1】:

    ; 上拆分字符串,这将返回一个值数组,您可以从中获取索引 2 处的元素。

    select split(str,';')[2]
    from tbl
    

    【讨论】:

      【解决方案2】:

      如果您想像示例中那样将空字符串和仅空格字符串转换为 NULL,那么此宏可能很有用:

      create temporary macro empty_to_null(s string) case when trim(s)!='' then s end;
      select empty_to_null(split(col1,'\\;')[2]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        相关资源
        最近更新 更多