蜂巢:加入正则表达式

时间:2017-07-14 01:31:12

标签: hive

我想用regex / rlike条件实现连接。但是Hive并没有做不平等加入

select a.col_1, b.col_2 
from table1 a left join table2 b
on a.col_1 rlike b.col_2

这实际上有效,但我希望将b.col2中的全文与a.col_1中的字符串相匹配。有没有办法做到这一点?

示例数据集:

**table1**
apple iphone 
apple iphone 6s
google nexus
samsung galaxy tab

**table2**
apple
google
nexus

**outcome**
col1                   col2
apple iphone          apple
apple iphone 6s       apple
google nexus          google
samsung galaxy tab    null

1 个答案:

答案 0 :(得分:1)

select  col1
       ,col2

from   (select  t1.col1
               ,t2.col2
               ,count      (col2) over (partition by col1)      as count_col2
               ,row_number ()     over (partition by col1,col2) as rn

        from                   (select  *

                                from    table1 t1 
                                        lateral view explode(split(col1,'\\s+')) e as token
                                ) t1

                left join      (select  *

                                from    table2 t2
                                        lateral view explode(split(col2,'\\s+')) e as token
                                ) t2


                on              t2.token = 
                                t1.token     
        ) t  

where   (   count_col2 = 0
        or  col1 rlike concat ('\\b',col2,'\\b')
        )

    and rn = 1
;
+--------------------+--------+
|        col1        |  col2  |
+--------------------+--------+
| apple iphone       | apple  |
| apple iphone 6s    | apple  |
| google nexus       | google |
| google nexus       | nexus  |
| samsung galaxy tab | (null) |
+--------------------+--------+