【发布时间】: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;
【问题讨论】:
我在 hive 表 list_ids 中有一个列,它是存储为逗号分隔字符串的 id 列表。
如何为该列编写查询以检查它是否存储特定的 id
例子:
list_ids = "abc,cde,efg"
我想要类似的东西
select * from table_name where list_ids contains cde;
【问题讨论】:
使用 Hive 标准函数 split 和 array_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')
【讨论】:
from (select split(list_ids,',') as ids_array from table_name) where array_contains(ids_array, 'abc') or array_contains(ids_array, 'xyz');
Hive 支持LIKE 运算符。您可以使用以下方法轻松完成:
select * from table_name where list_ids like '%cde%';
查看此语言手册了解更多信息:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
【讨论】:
'%,cde,%' 这样像 'cdee' 这样的 ID 不会返回
select * from table_name where concat(',',list_ids,',') like '%,cde,%';
使用Hive函数explode就可以实现。
示例
select *
from table_name
LATERAL VIEW explode(list_ids) exploded_table as list_id_tbl
where list_ids='cde'
【讨论】: