Search in sources :

Example 16 with PrimitiveObjectInspectorFactory.javaIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector in project cdap by caskdata.

the class StandardObjectInspectorsTest method testStandardQueueObjectInspector.

@Test
public void testStandardQueueObjectInspector() throws Throwable {
    try {
        ObjectInspector oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<Queue<Integer>>() {
        }.getType());
        Assert.assertTrue(oi instanceof StandardListObjectInspector);
        StandardListObjectInspector loi = (StandardListObjectInspector) oi;
        // metadata
        Assert.assertEquals(Category.LIST, loi.getCategory());
        Assert.assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector, loi.getListElementObjectInspector());
        // Test queue inspection
        Queue<Integer> queue = new ArrayDeque<>();
        queue.add(0);
        queue.add(1);
        queue.add(2);
        queue.add(3);
        Assert.assertEquals(4, loi.getList(queue).size());
        Assert.assertEquals(4, loi.getListLength(queue));
        Assert.assertEquals(0, loi.getListElement(queue, 0));
        Assert.assertEquals(3, loi.getListElement(queue, 3));
        Assert.assertNull(loi.getListElement(queue, -1));
        Assert.assertNull(loi.getListElement(queue, 4));
        // Settable
        List<String> list = (List<String>) loi.set(queue, 0, 5);
        Assert.assertFalse(queue.contains(5));
        Assert.assertTrue(list.contains(5));
        list = (List<String>) loi.resize(queue, 5);
        Assert.assertEquals(4, queue.size());
        Assert.assertEquals(5, list.size());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) TypeToken(com.google.common.reflect.TypeToken) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 17 with PrimitiveObjectInspectorFactory.javaIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector in project cdap by caskdata.

the class StandardObjectInspectorsTest method testStandardMapObjectInspector.

@Test
public void testStandardMapObjectInspector() throws Throwable {
    try {
        StandardMapObjectInspector moi1 = ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaIntObjectInspector);
        StandardMapObjectInspector moi2 = ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaIntObjectInspector);
        Assert.assertEquals(moi1, moi2);
        // metadata
        Assert.assertEquals(Category.MAP, moi1.getCategory());
        Assert.assertEquals(moi1.getMapKeyObjectInspector(), PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        Assert.assertEquals(moi2.getMapValueObjectInspector(), PrimitiveObjectInspectorFactory.javaIntObjectInspector);
        // null
        Assert.assertNull(moi1.getMap(null));
        Assert.assertNull(moi1.getMapValueElement(null, null));
        Assert.assertNull(moi1.getMapValueElement(null, "nokey"));
        Assert.assertEquals(-1, moi1.getMapSize(null));
        Assert.assertEquals("map<" + PrimitiveObjectInspectorFactory.javaStringObjectInspector.getTypeName() + "," + PrimitiveObjectInspectorFactory.javaIntObjectInspector.getTypeName() + ">", moi1.getTypeName());
        // HashMap
        HashMap<String, Integer> map = new HashMap<>();
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);
        Assert.assertEquals(map, moi1.getMap(map));
        Assert.assertEquals(3, moi1.getMapSize(map));
        Assert.assertEquals(1, moi1.getMapValueElement(map, "one"));
        Assert.assertEquals(2, moi1.getMapValueElement(map, "two"));
        Assert.assertEquals(3, moi1.getMapValueElement(map, "three"));
        Assert.assertNull(moi1.getMapValueElement(map, null));
        Assert.assertNull(moi1.getMapValueElement(map, "null"));
        // Settable
        Object map3 = moi1.create();
        moi1.put(map3, "one", 1);
        moi1.put(map3, "two", 2);
        moi1.put(map3, "three", 3);
        Assert.assertEquals(map, map3);
        moi1.clear(map3);
        Assert.assertEquals(0, moi1.getMapSize(map3));
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) UnionObject(org.apache.hadoop.hive.serde2.objectinspector.UnionObject) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)10 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)10 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)6 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)6 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)6 Test (org.junit.Test)5 UnionObject (org.apache.hadoop.hive.serde2.objectinspector.UnionObject)4 HashMap (java.util.HashMap)3 GenericUDFDateAdd (org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateAdd)3 GenericUDFDateSub (org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateSub)3 Text (org.apache.hadoop.io.Text)3 TypeToken (com.google.common.reflect.TypeToken)2 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)2 Complex (org.apache.hadoop.hive.serde2.thrift.test.Complex)2 IntString (org.apache.hadoop.hive.serde2.thrift.test.IntString)2 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)2