【问题标题】:hive check comma separated String contains a stringhive 检查逗号分隔的 String 是否包含字符串
【发布时间】:2014-03-25 07:00:38
【问题描述】:

我在 hive 表 list_ids 中有一个列,它是存储为逗号分隔字符串的 id 列表。

如何为该列编写查询以检查它是否存储特定的 id

例子:

 list_ids = "abc,cde,efg"

我想要类似的东西

 select * from table_name where list_ids contains cde; 

【问题讨论】:

    标签: string hive


    【解决方案1】:

    使用 Hive 标准函数 splitarray_contains

    split(string str, string pat) 通过在 pat 周围拆分 str(正则表达式)返回 array<string>

    array_contains(array<T>, value) 如果数组包含值,则返回 true

    select * from table_name where array_contains(split(list_ids,','),'cde')

    【讨论】:

    • 有没有办法做多个值?即 array_contains(split(list_ids,','), 'cde|abc|xyz')
    • @blueskin 首先拆分数组,然后根据需要执行任意数量的包含:from (select split(list_ids,',') as ids_array from table_name) where array_contains(ids_array, 'abc') or array_contains(ids_array, 'xyz');
    【解决方案2】:

    Hive 支持LIKE 运算符。您可以使用以下方法轻松完成:

    select * from table_name where list_ids like '%cde%';

    查看此语言手册了解更多信息:

    https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

    【讨论】:

    • 最好写 '%,cde,%' 这样像 'cdee' 这样的 ID 不会返回
    • @dimamah 不幸的是,'%,abc,%' 与 OP 中的示例不匹配,因为列表中的第一项前面没有逗号。同样,最后一项后面也没有逗号。
    • 没错。所以我们可以这样做:select * from table_name where concat(',',list_ids,',') like '%,cde,%';
    【解决方案3】:

    使用Hive函数explode就可以实现。

    示例

    select *
    from table_name
    LATERAL VIEW explode(list_ids) exploded_table as list_id_tbl
    where list_ids='cde'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      相关资源
      最近更新 更多