【问题标题】:Filtering in hive在蜂巢中过滤
【发布时间】:2017-07-12 18:35:37
【问题描述】:

我有一个配置单元表演示(id bigint,项目字符串),其中数据集是一组逗号分隔的字符串,例如

id  dataset 
1   ,2,asd,as,a,1 
2   sda,saa,2,fds 

有没有一种本地方法可以过滤掉非数字字符串并只保留数字字符串,而不是分解表格,过滤然后分组或编写我自己的 UDF。

id  dataset 
1   2,1 
2   2

【问题讨论】:

    标签: hadoop filter hive user-defined-functions


    【解决方案1】:
    select  id
           ,regexp_replace(regexp_replace(dataset,'(?<=^|,)((\\d+)|([^,]*))(?=,|$)','$2'),'^,+|,+$|(,)+','$1')
    
    from    demo
    ;
    

    +----+-----+
    | id | c1  |
    +----+-----+
    |  1 | 2,1 |
    |  2 | 2   |
    +----+-----+
    

    【讨论】:

      【解决方案2】:

      试试这样的正则表达式:

      select id
        ,regexp_replace(dataset, '(,[a-zA-Z]+|^,|[a-zA-Z]+,)' , '') as dataset
       from yourdata;
      

      这会导致:

      id dataset
      1 2,1
      2 2
      

      【讨论】:

      • 没有。使用以下字符串测试自己:@,#,$,10x,a,,,b,a,b,1,,2
      • 我使用了op提供的数据
      • 这是另一个“解决方案”,它“按照操作的要求”:select regexp_replace(dataset,'\\D+(\\d)\\D+(,\\d)?$','$1$2') from demo
      猜你喜欢
      • 2016-12-19
      • 2019-02-07
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多