Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hive check comma separated String contains a string

Tags:

string

hive

I have a column in hive table list_ids which is a list of ids stored as comma separated string.

how can I write a query for this column to check if it stores a particular id

Example:

 list_ids = "abc,cde,efg"

I want to something like

 select * from table_name where list_ids contains cde; 
like image 228
user2978621 Avatar asked Mar 25 '14 07:03

user2978621


2 Answers

Use Hive standard functions split and array_contains

split(string str, string pat) returns array<string> by splitting str around pat (regular expression)

array_contains(array<T>, value) returns true if array contains value

select * from table_name where array_contains(split(list_ids,','),'cde')

like image 160
libjack Avatar answered Oct 23 '22 19:10

libjack


Hive supports LIKE operator. You can do it easily by using:

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

Check out this language manual for more info:

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

like image 31
Neels Avatar answered Oct 23 '22 17:10

Neels