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
LIKE is an operator similar to LIKE in SQL. We use LIKE to search for string with similar text. RLIKE (Right-Like) is a special function in Hive where if any substring of A matches with B then it evaluates to true. It also obeys Java regular expression pattern.
Whenever a user requirement is to run single or multiple queries (separated by semicolon) on Hive CLI with terminating the Hive shell as soon as the query got fired one can use the -e option with Hive to enable this functionality.
You can search for string by matching patterns. Note that, Hive LIKE statement is case-sensitive. Apache Hive LIKE statements returns TRUE if string that you are searching for. The Hive NOT LIKE is negation of LIKE and vice-versa.
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'
You can probably use rlike(regular_expression).
WHERE some_col RLIKE '*abc*|*pqr*|*xyz*'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With