简体   繁体   中英

How to pattern match string expression in array_contains function in hive?

Consider a column A as an array of strings. Now I would like to search for all the rows in the table to have a string of a particular pattern. I know array_contains() function can be used to check for the exact matches, but is there a way to check for patterned matches.

select * from table 
where array_contains(A,"%123%");

Is it possible to do pattern matches like this?

First generate row number. Then explode your array column with lateral view , then filter out rows that didn't match the pattern, eg,

with table1 as (
    select row_number() over () as rnum, *
    from your_table
),
table2 as (
    select *, arr
    from table1 lateral view explode(A) tmp as arr
    where arr like '%123%'
),
table3 as (
    select distinct rnum from table2
)
select *
  from table1 t1
  left semi
  join table3 t3
  on t1.rnum = t3.rnum

I think you can use:

select distinct t1.*
from table1 t1 lateral view
     explode(t1.A) tmp as arr
where arr like '%123%';

There is a way to eliminate the select distinct , but I'm not sure if Hive supports it:

select t1.*
from table1 t1
where exists (select 1
              from explode(t1.A) as arr
              where arr like '%123%'
             );

Or you can use this somewhat kludgy method:

select t1.*
from table1 t1
where concat_ws('|', t1.A) like '%123%';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM