1+ package cc .shanruifeng .functions .array ;
2+
3+ import com .google .common .collect .ImmutableList ;
4+ import com .google .common .collect .Iterables ;
5+ import org .apache .hadoop .hive .ql .metadata .HiveException ;
6+ import org .apache .hadoop .hive .ql .udf .generic .GenericUDF ;
7+ import org .apache .hadoop .hive .serde2 .objectinspector .ObjectInspector ;
8+ import org .apache .hadoop .hive .serde2 .objectinspector .ObjectInspectorFactory ;
9+ import org .apache .hadoop .hive .serde2 .objectinspector .primitive .PrimitiveObjectInspectorFactory ;
10+ import org .junit .Test ;
11+
12+ import java .util .ArrayList ;
13+ import java .util .List ;
14+
15+ import static org .junit .Assert .*;
16+
17+ /**
18+ * @author ruifeng.shan
19+ * @date 2018-07-18
20+ * @time 13:00
21+ */
22+ public class UDFArrayIntersectTest {
23+ @ Test
24+ public void testArrayIntersect () throws HiveException {
25+ UDFArrayIntersect udf = new UDFArrayIntersect ();
26+
27+ ObjectInspector leftArrayOI = ObjectInspectorFactory .getStandardListObjectInspector (PrimitiveObjectInspectorFactory .javaIntObjectInspector );
28+ ObjectInspector rightArrayOI = ObjectInspectorFactory .getStandardListObjectInspector (PrimitiveObjectInspectorFactory .javaIntObjectInspector );
29+ ObjectInspector [] arguments = {leftArrayOI , rightArrayOI };
30+
31+ udf .initialize (arguments );
32+
33+ assertTrue (Iterables .elementsEqual (ImmutableList .of (1 ,2 ,5 ), evaluate (ImmutableList .of (0 ,1 ,2 ,3 ,4 ,5 ), ImmutableList .of (1 ,1 ,2 ,2 ,5 ,5 ), udf )));
34+ assertTrue (Iterables .elementsEqual (ImmutableList .of (1 ,2 ,3 ,4 ), evaluate (ImmutableList .of (0 ,1 ,2 ,3 ,4 ,4 ), ImmutableList .of (1 ,1 ,2 ,2 ,3 ,4 ), udf )));
35+ assertTrue (Iterables .elementsEqual (ImmutableList .of (1 ,2 ,3 ,4 ), evaluate (ImmutableList .of (0 ,1 ,1 ,2 ,3 ,4 ,4 ), ImmutableList .of (1 ,1 ,2 ,2 ,3 ,4 ), udf )));
36+ }
37+
38+ private ArrayList <Object > evaluate (List <Integer > leftArray , List <Integer > rightArray , UDFArrayIntersect udf ) throws HiveException {
39+ GenericUDF .DeferredObject leftArrayObj = new GenericUDF .DeferredJavaObject (leftArray );
40+ GenericUDF .DeferredObject rightArrayObj = new GenericUDF .DeferredJavaObject (rightArray );
41+ GenericUDF .DeferredObject [] args = {leftArrayObj , rightArrayObj };
42+ ArrayList <Object > output = (ArrayList <Object >) udf .evaluate (args );
43+ return output ;
44+ }
45+ }
0 commit comments