Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive query with multiple LIKE operators

Tags:

sql-like

hive

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 image 882
Shobit Avatar asked Nov 10 '15 09:11

Shobit


People also ask

Can we use like operator in Hive?

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.

How do I run multiple queries in Hive?

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.

How do I check my likes on Hive?

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.


2 Answers

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' 
like image 181
mattinbits Avatar answered Jan 02 '23 19:01

mattinbits


You can probably use rlike(regular_expression).

WHERE some_col RLIKE '*abc*|*pqr*|*xyz*' 
like image 22
Shivanand Pawar Avatar answered Jan 02 '23 20:01

Shivanand Pawar