hive check逗号分隔String包含一个字符串

use*_*621 18 string hive

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

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

例:

 list_ids = "abc,cde,efg"
Run Code Online (Sandbox Code Playgroud)

我想要类似的东西

 select * from table_name where list_ids contains cde; 
Run Code Online (Sandbox Code Playgroud)

lib*_*ack 27

使用Hive标准功能splitarray_contains

split(string str, string pat)回报array<string>通过拆分周围轻拍STR(正则表达式)

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') (2认同)

Nee*_*els 17

Hive支持LIKE运营商.您可以使用以下方法轻松完成:

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

查看此语言手册以获取更多信息:

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

  • 最好写''%,cde,%'`所以像''cdee'这样的id不会返回 (2认同)
  • @dimamah不幸的是,'%,abc,%'与OP中的示例不匹配,因为列表中的第一项不会在前面有逗号.同样,最后一项不会有逗号. (2认同)
  • 这是正确的。所以我们可以这样做: `select * from table_name where concat(',',list_ids,',') like '%,cde,%';` (2认同)