hql 过滤掉逗号分割的字符串中存在某个值的字符串

在 hive 中如何过滤掉逗号分割的字符串中存在某个值的字符串呢?

假设给定一个表:

col
'1'
'1,2,3,4,5'
'1,2,4,5'
'3,4,5'

希望过滤掉行中带有 '3' 的数据,最开始的想法是用 string 的 split 产生一个 string 的数组,然后再通过 array 的 find 判断是否存在有 '3',但是并没有发现相关的 find 函数,于是转变思路,尝试正则解决:

select col from (
    select '1' as col
    union all
    select '1,2,3,4,5' as col
    union all
    select '1,2,4,5' as col
    union all
    select '3,4,5' as col
)t
where col not regexp  ',*3,*'

即可得到想要的结果,正则 ,* 代表我需要匹配的字符开头要出现0次或者n次 ,,解决我需要的情况。

如果是 presto 的语法可以用:

not regexp_like(col, ',*3,*')
posted @ 2022-04-22 19:38  pokpok  阅读(177)  评论(0编辑  收藏  举报