Search in sources :

Example 21 with PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector

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

the class ObjectInspectorFactory method getReflectionObjectInspectorNoCache.

private static ObjectInspector getReflectionObjectInspectorNoCache(Type t) {
    if (t instanceof GenericArrayType) {
        GenericArrayType at = (GenericArrayType) t;
        return getStandardListObjectInspector(getReflectionObjectInspector(at.getGenericComponentType()));
    }
    Map<TypeVariable, Type> genericTypes = null;
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        Type rawType = pt.getRawType();
        // Collection?
        if (Collection.class.isAssignableFrom((Class<?>) rawType)) {
            return getStandardListObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0]));
        }
        // Map?
        if (Map.class.isAssignableFrom((Class<?>) rawType)) {
            return getStandardMapObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0]), getReflectionObjectInspector(pt.getActualTypeArguments()[1]));
        }
        // Otherwise convert t to RawType so we will fall into the following if block.
        t = rawType;
        ImmutableMap.Builder<TypeVariable, Type> builder = ImmutableMap.builder();
        for (int i = 0; i < pt.getActualTypeArguments().length; i++) {
            builder.put(((Class<?>) t).getTypeParameters()[i], pt.getActualTypeArguments()[i]);
        }
        genericTypes = builder.build();
    }
    // Must be a class.
    if (!(t instanceof Class)) {
        throw new RuntimeException(ObjectInspectorFactory.class.getName() + " internal error:" + t);
    }
    Class<?> c = (Class<?>) t;
    // Java Primitive Type?
    if (PrimitiveObjectInspectorUtils.isPrimitiveJavaType(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaType(c).primitiveCategory);
    }
    // Java Primitive Class?
    if (PrimitiveObjectInspectorUtils.isPrimitiveJavaClass(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaClass(c).primitiveCategory);
    }
    // Primitive Writable class?
    if (PrimitiveObjectInspectorUtils.isPrimitiveWritableClass(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveWritableClass(c).primitiveCategory);
    }
    // Enum class?
    if (Enum.class.isAssignableFrom(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
    }
    // Array
    if (c.isArray()) {
        return getStandardListObjectInspector(getReflectionObjectInspector(c.getComponentType()));
    }
    // Must be struct because List and Map need to be ParameterizedType
    Preconditions.checkState(!List.class.isAssignableFrom(c));
    Preconditions.checkState(!Map.class.isAssignableFrom(c));
    Preconditions.checkState(!c.isInterface(), "Cannot inspect an interface.");
    ReflectionStructObjectInspector oi = new ReflectionStructObjectInspector();
    // put it into the cache BEFORE it is initialized to make sure we can catch
    // recursive types.
    objectInspectorCache.put(t, oi);
    Field[] fields = ObjectInspectorUtils.getDeclaredNonStaticFields(c);
    List<ObjectInspector> structFieldObjectInspectors = new ArrayList<>(fields.length);
    for (Field field : fields) {
        // "this" pointer present in nested classes and that references the parent.
        if (Modifier.isTransient(field.getModifiers()) || field.isSynthetic()) {
            continue;
        }
        if (!oi.shouldIgnoreField(field.getName())) {
            Type newType = field.getGenericType();
            if (newType instanceof TypeVariable) {
                Preconditions.checkNotNull(genericTypes, "Type was not recognized as a parameterized type.");
                Preconditions.checkNotNull(genericTypes.get(newType), "Generic type " + newType + " not a parameter of class " + c);
                newType = genericTypes.get(newType);
            }
            structFieldObjectInspectors.add(getReflectionObjectInspector(newType));
        }
    }
    oi.init(c, structFieldObjectInspectors);
    return oi;
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType) ImmutableMap(com.google.common.collect.ImmutableMap) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) PrimitiveObjectInspectorFactory(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory)

Aggregations

ArrayList (java.util.ArrayList)12 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)10 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)9 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)7 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)7 Date (java.sql.Date)5 Timestamp (java.sql.Timestamp)5 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)5 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)5 Test (org.junit.Test)5 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Text (org.apache.hadoop.io.Text)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 List (java.util.List)2