是否有任何Hive数组比较函数/ udf' s

时间:2016-09-05 11:30:18

标签: arrays hadoop hive udf

hive是否有任何数组比较函数/ udf来查看是否array=array

例如:

Select a.xyz, b.abc from a left join b on a.C=b.D

CD是数组

3 个答案:

答案 0 :(得分:2)

数组是有序结构,

 concat_ws( <separator> , <array> ) 

此函数将使用分隔符将所有数组元素连接成一个字符串。

create table arrayDemo( id bigint, list array<String> );
create table dummy( a int );
insert into table dummy values ( 1 ) ; 
insert into arraydemo select 1, array("Paperino", "Topolino") from dummy;
insert into arraydemo select 2, array("Pippo", "Pluto") from dummy;
insert into arraydemo select 1, array("Pippo", "Pluto") from dummy; 

select * from arraydemo; 
+---------------+--------------------------+--+
| arraydemo.id  |      arraydemo.list      |
+---------------+--------------------------+--+
| 1             | ["Paperino","Topolino"]  |
| 2             | ["Pippo","Pluto"]        |
| 1             | ["Pippo","Pluto"]        |
+---------------+--------------------------+--+

select * 
     from arraydemo as a1 
          inner join arraydemo as a2 
          on concat_ws( "|", a1.list ) = concat_ws("|", a2.list ); 

+--------+--------------------------+--------+--------------------------+--+
| a1.id  |         a1.list          | a2.id  |         a2.list          |
+--------+--------------------------+--------+--------------------------+--+
| 1      | ["Paperino","Topolino"]  | 1      | ["Paperino","Topolino"]  |
| 2      | ["Pippo","Pluto"]        | 2      | ["Pippo","Pluto"]        |
| 1      | ["Pippo","Pluto"]        | 2      | ["Pippo","Pluto"]        |
| 2      | ["Pippo","Pluto"]        | 1      | ["Pippo","Pluto"]        |
| 1      | ["Pippo","Pluto"]        | 1      | ["Pippo","Pluto"]        |
+--------+--------------------------+--------+--------------------------+--+

我在这个例子中使用它在&#34; toString()&#34;时尚。有时最好只比较数组的重要部分

 select * 
      from arraydemo as a1 
      inner join arraydemo as a2 
      on a1.list[0] = a2.list[0]; 

希望这有帮助。

答案 1 :(得分:0)

您可以通过

比较两个数组的值

array_diff($array1,$array2);

它返回一个数组,其中包含array1中不存在于array2或array3中的条目等。

答案 2 :(得分:0)

您可以使用hive-third-functions。它提供了一些有用的json,数组和map函数。对于这个问题,你可以这样使用:

Select a.xyz, b.abc from a left join b on array_equals(a.C,b.D)