Search in sources :

Example 21 with ObjectInspectorFactory.getReflectionObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getReflectionObjectInspector in project hive by apache.

the class TestThriftObjectInspectors method testThriftObjectInspectors.

public void testThriftObjectInspectors() throws Throwable {
    try {
        ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        ObjectInspector oi2 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        assertEquals(oi1, oi2);
        // metadata
        assertEquals(Category.STRUCT, oi1.getCategory());
        StructObjectInspector soi = (StructObjectInspector) oi1;
        List<? extends StructField> fields = soi.getAllStructFieldRefs();
        assertEquals(10, fields.size());
        assertEquals(fields.get(0), soi.getStructFieldRef("aint"));
        // null
        for (int i = 0; i < fields.size(); i++) {
            assertNull(soi.getStructFieldData(null, fields.get(i)));
        }
        ObjectInspector oi = ObjectInspectorFactory.getReflectionObjectInspector(PropValueUnion.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        assertNotNull(oi.toString());
        // real object
        Complex c = new Complex();
        c.setAint(1);
        c.setAString("test");
        List<Integer> c2 = Arrays.asList(new Integer[] { 1, 2, 3 });
        c.setLint(c2);
        List<String> c3 = Arrays.asList(new String[] { "one", "two" });
        c.setLString(c3);
        List<IntString> c4 = new ArrayList<IntString>();
        c.setLintString(c4);
        c.setMStringString(null);
        c.setAttributes(null);
        c.setUnionField1(null);
        c.setUnionField2(null);
        c.setUnionField3(null);
        assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
        assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
        assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
        assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
        assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
        assertNull(soi.getStructFieldData(c, fields.get(5)));
        assertNull(soi.getStructFieldData(c, fields.get(6)));
        assertNull(soi.getStructFieldData(c, fields.get(7)));
        assertNull(soi.getStructFieldData(c, fields.get(8)));
        assertNull(soi.getStructFieldData(c, fields.get(9)));
        ArrayList<Object> cfields = new ArrayList<Object>();
        for (int i = 0; i < 10; i++) {
            cfields.add(soi.getStructFieldData(c, fields.get(i)));
        }
        assertEquals(cfields, soi.getStructFieldsDataAsList(c));
        // sub fields
        assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector, fields.get(0).getFieldObjectInspector());
        assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector), fields.get(2).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(3).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT)), fields.get(4).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(5).getFieldObjectInspector());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : ArrayList(java.util.ArrayList) SetIntString(org.apache.hadoop.hive.serde2.thrift.test.SetIntString) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString) Complex(org.apache.hadoop.hive.serde2.thrift.test.Complex) SetIntString(org.apache.hadoop.hive.serde2.thrift.test.SetIntString) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString)

Example 22 with ObjectInspectorFactory.getReflectionObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getReflectionObjectInspector in project cdap by caskdata.

the class ObjectInspectorFactoryTest method assertObjectInspection.

private void assertObjectInspection(Type t, Object data) throws Exception {
    Field[] tmpFields;
    // Build the expected fields, based on the type t. Exclude the transient fields.
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        // TODO either test getDeclaredNonStaticFields or use another method
        tmpFields = ObjectInspectorUtils.getDeclaredNonStaticFields((Class<?>) pt.getRawType());
    } else {
        tmpFields = ObjectInspectorUtils.getDeclaredNonStaticFields((Class<?>) t);
    }
    ImmutableList.Builder builder = ImmutableList.builder();
    for (Field f : tmpFields) {
        if (!Modifier.isTransient(f.getModifiers()) && !f.isSynthetic()) {
            builder.add(f);
        }
    }
    List<Field> expectedFields = builder.build();
    ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(t);
    ObjectInspector oi2 = ObjectInspectorFactory.getReflectionObjectInspector(t);
    Assert.assertEquals(oi1, oi2);
    // metadata
    Assert.assertEquals(ObjectInspector.Category.STRUCT, oi1.getCategory());
    StructObjectInspector soi = (StructObjectInspector) oi1;
    List<? extends StructField> inspectorFields = soi.getAllStructFieldRefs();
    Assert.assertEquals(expectedFields.size(), inspectorFields.size());
    // null
    for (int i = 0; i < inspectorFields.size(); i++) {
        Assert.assertNull(soi.getStructFieldData(null, inspectorFields.get(i)));
    }
    Assert.assertNull(soi.getStructFieldsDataAsList(null));
    // non nulls
    ArrayList<Object> afields = new ArrayList<>();
    for (int i = 0; i < expectedFields.size(); i++) {
        Assert.assertEquals(expectedFields.get(i).get(data), soi.getStructFieldData(data, inspectorFields.get(i)));
        afields.add(soi.getStructFieldData(data, inspectorFields.get(i)));
    }
    Assert.assertEquals(afields, soi.getStructFieldsDataAsList(data));
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 23 with ObjectInspectorFactory.getReflectionObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getReflectionObjectInspector in project cdap by caskdata.

the class SimpleMapEqualComparerTest method testCompatibleType.

@Test
public void testCompatibleType() throws SerDeException, IOException {
    // empty maps
    TextStringMapHolder o1 = new TextStringMapHolder();
    StructObjectInspector oi1 = (StructObjectInspector) ObjectInspectorFactory.getReflectionObjectInspector(TextStringMapHolder.class);
    LazySimpleSerDe serde = new LazySimpleSerDe();
    Configuration conf = new Configuration();
    Properties tbl = new Properties();
    tbl.setProperty(serdeConstants.LIST_COLUMNS, ObjectInspectorUtils.getFieldNames(oi1));
    tbl.setProperty(serdeConstants.LIST_COLUMN_TYPES, ObjectInspectorUtils.getFieldTypes(oi1));
    LazySerDeParameters serdeParams = new LazySerDeParameters(conf, tbl, LazySimpleSerDe.class.getName());
    serde.initialize(conf, tbl);
    ObjectInspector oi2 = serde.getObjectInspector();
    Object o2 = serializeAndDeserialize(o1, oi1, serde, serdeParams);
    int rc = ObjectInspectorUtils.compare(o1, oi1, o2, oi2, new SimpleMapEqualComparer());
    Assert.assertEquals(0, rc);
    // equal maps
    o1.mMap.put(new Text("42"), "The answer to Life, Universe And Everything");
    o1.mMap.put(new Text("1729"), "A taxi cab number");
    o2 = serializeAndDeserialize(o1, oi1, serde, serdeParams);
    rc = ObjectInspectorUtils.compare(o1, oi1, o2, oi2, new SimpleMapEqualComparer());
    Assert.assertEquals(0, rc);
    // unequal maps
    o1.mMap.put(new Text("1729"), "Hardy-Ramanujan Number");
    rc = ObjectInspectorUtils.compare(o1, oi1, o2, oi2, new SimpleMapEqualComparer());
    Assert.assertFalse(0 == rc);
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) SimpleMapEqualComparer(org.apache.hadoop.hive.serde2.objectinspector.SimpleMapEqualComparer) Configuration(org.apache.hadoop.conf.Configuration) LazySerDeParameters(org.apache.hadoop.hive.serde2.lazy.LazySerDeParameters) LazySimpleSerDe(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe) Text(org.apache.hadoop.io.Text) Properties(java.util.Properties) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) Test(org.junit.Test)

Example 24 with ObjectInspectorFactory.getReflectionObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getReflectionObjectInspector in project cdap by caskdata.

the class SimpleMapEqualComparerTest method testIncompatibleType.

@Test
public void testIncompatibleType() throws SerDeException, IOException {
    // empty maps
    StringTextMapHolder o1 = new StringTextMapHolder();
    StructObjectInspector oi1 = (StructObjectInspector) ObjectInspectorFactory.getReflectionObjectInspector(StringTextMapHolder.class);
    LazySimpleSerDe serde = new LazySimpleSerDe();
    Configuration conf = new Configuration();
    Properties tbl = new Properties();
    tbl.setProperty(serdeConstants.LIST_COLUMNS, ObjectInspectorUtils.getFieldNames(oi1));
    tbl.setProperty(serdeConstants.LIST_COLUMN_TYPES, ObjectInspectorUtils.getFieldTypes(oi1));
    LazySerDeParameters serdeParams = new LazySerDeParameters(conf, tbl, LazySimpleSerDe.class.getName());
    serde.initialize(conf, tbl);
    ObjectInspector oi2 = serde.getObjectInspector();
    Object o2 = serializeAndDeserialize(o1, oi1, serde, serdeParams);
    int rc = ObjectInspectorUtils.compare(o1, oi1, o2, oi2, new SimpleMapEqualComparer());
    Assert.assertEquals(0, rc);
    // equal maps
    o1.mMap.put("42", new Text("The answer to Life, Universe And Everything"));
    o1.mMap.put("1729", new Text("A taxi cab number"));
    o2 = serializeAndDeserialize(o1, oi1, serde, serdeParams);
    rc = ObjectInspectorUtils.compare(o1, oi1, o2, oi2, new SimpleMapEqualComparer());
    Assert.assertFalse(0 == rc);
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) SimpleMapEqualComparer(org.apache.hadoop.hive.serde2.objectinspector.SimpleMapEqualComparer) Configuration(org.apache.hadoop.conf.Configuration) LazySerDeParameters(org.apache.hadoop.hive.serde2.lazy.LazySerDeParameters) LazySimpleSerDe(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe) Text(org.apache.hadoop.io.Text) Properties(java.util.Properties) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) Test(org.junit.Test)

Example 25 with ObjectInspectorFactory.getReflectionObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getReflectionObjectInspector in project cdap by caskdata.

the class StandardObjectInspectorsTest method testPrimitiveTypesListObjectInspector.

@Test
public void testPrimitiveTypesListObjectInspector() throws Throwable {
    ObjectInspector oi;
    StandardListObjectInspector loi;
    // Byte array
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<List<Byte>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    byte[] bytes = new byte[] { 0, 1, 2 };
    Assert.assertEquals(3, loi.getListLength(bytes));
    Assert.assertEquals((byte) 0, loi.getListElement(bytes, 0));
    Assert.assertEquals((byte) 1, loi.getListElement(bytes, 1));
    Assert.assertEquals((byte) 2, loi.getListElement(bytes, 2));
    // Int array
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<List<Integer>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    int[] ints = new int[] { 0, 1, 2 };
    Assert.assertEquals(3, loi.getListLength(ints));
    Assert.assertEquals(0, loi.getListElement(ints, 0));
    Assert.assertEquals(1, loi.getListElement(ints, 1));
    Assert.assertEquals(2, loi.getListElement(ints, 2));
    // long array
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<List<Long>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    long[] longs = new long[] { 0, 1, 2 };
    Assert.assertEquals((long) 3, loi.getListLength(longs));
    Assert.assertEquals((long) 0, loi.getListElement(longs, 0));
    Assert.assertEquals((long) 1, loi.getListElement(longs, 1));
    Assert.assertEquals((long) 2, loi.getListElement(longs, 2));
    // double array
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<List<Double>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    double[] doubles = new double[] { 0.1d, 1.0d, 2.0d };
    Assert.assertEquals(3, loi.getListLength(doubles));
    Assert.assertEquals(0.1d, loi.getListElement(doubles, 0));
    Assert.assertEquals(1.0d, loi.getListElement(doubles, 1));
    Assert.assertEquals(2.0d, loi.getListElement(doubles, 2));
    // float array
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<List<Float>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    float[] floats = new float[] { 0.1f, 1.0f, 2.0f };
    Assert.assertEquals(3, loi.getListLength(floats));
    Assert.assertEquals(0.1f, loi.getListElement(floats, 0));
    Assert.assertEquals(1.0f, loi.getListElement(floats, 1));
    Assert.assertEquals(2.0f, loi.getListElement(floats, 2));
    // short array
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<List<Short>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    short[] shorts = new short[] { 0, 1, 2 };
    Assert.assertEquals(3, loi.getListLength(shorts));
    Assert.assertEquals((short) 0, loi.getListElement(shorts, 0));
    Assert.assertEquals((short) 1, loi.getListElement(shorts, 1));
    Assert.assertEquals((short) 2, loi.getListElement(shorts, 2));
    // short array
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<List<Boolean>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    boolean[] booleans = new boolean[] { true, false, false };
    Assert.assertEquals(3, loi.getListLength(booleans));
    Assert.assertEquals(true, loi.getListElement(booleans, 0));
    Assert.assertEquals(false, loi.getListElement(booleans, 1));
    Assert.assertEquals(false, loi.getListElement(booleans, 2));
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)73 Test (org.junit.Test)64 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)60 Configuration (org.apache.hadoop.conf.Configuration)25 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)25 InputSplit (org.apache.hadoop.mapred.InputSplit)25 BinaryObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector)24 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)23 Properties (java.util.Properties)20 IntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector)20 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)18 BooleanObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector)18 ByteObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector)18 DoubleObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector)18 FloatObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)18 HiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector)18 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)18 ShortObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector)18 TimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector)18 RecordWriter (org.apache.hadoop.mapred.RecordWriter)18