Hive query with multiple LIKE operators

爷,独闯天下 提交于 2019-12-21 02:19:11

问题


What would be the right way to write a Hive query with multiple LIKE operators like this:

SELECT * 
FROM some_table
WHERE
some_col LIKE '%abc%'
OR
some_col LIKE '%xyz%'
OR
some_col LIKE '%pqr%'
OR
... (some more LIKE statements)

I tried doing the above as well as

WHERE some_col LIKE '%abc|pqr|xyz%' 

but they didn't return any results. It works fine if I write separate queries, i.e.

WHERE some_col LIKE '%abc%' -> returns results

and

WHERE some_col LIKE '%pqr%' -> also returns results

回答1:


From the docs:

A RLIKE B

NULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B, otherwise FALSE. For example, 'foobar' RLIKE 'foo' evaluates to TRUE and so does 'foobar' RLIKE '^f.*r$'.


A REGEXP B

Same as RLIKE.

So, use

WHERE some_col RLIKE 'abc|pqr|xyz' 



回答2:


You can probably use rlike(regular_expression).

WHERE some_col RLIKE '*abc*|*pqr*|*xyz*' 



回答3:


I believe that the issue might be you need to group the like statement. Your Example done:

SELECT * 
FROM some_table
WHERE
(some_col LIKE '%abc%'
OR
some_col LIKE '%xyz%'
OR
some_col LIKE '%pqr%')



回答4:


You can try to use UNION if you have to use multiple condition in LIKE like this:

SELECT * FROM some_table WHERE some_col LIKE '%abc%'
UNION
SELECT * FROM some_table WHERE some_col LIKE '%xyz%'
UNION
SELECT * FROM some_table WHERE some_col LIKE '%pqr%'



回答5:


If all you need is to check for a list of specific substrings, you could use a different approach e.g.

WHERE InStr(some_col, 'abc') +InStr(some_col, 'pqr') +... >0

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-StringFunctions



来源:https://stackoverflow.com/questions/33626645/hive-query-with-multiple-like-operators

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!