Search in sources :

Example 1 with ThriftStructFieldInfo

use of com.twitter.scrooge.ThriftStructFieldInfo in project parquet-mr by apache.

the class ScroogeStructConverter method getFieldInfosForUnion.

private Iterable<ThriftStructFieldInfo> getFieldInfosForUnion(Class klass) {
    ArrayList<ThriftStructFieldInfo> fields = new ArrayList<ThriftStructFieldInfo>();
    for (Field f : klass.getDeclaredFields()) {
        if (f.getType().equals(Manifest.class)) {
            Class unionClass = (Class) ((ParameterizedType) f.getGenericType()).getActualTypeArguments()[0];
            Class companionUnionClass = getCompanionClass(unionClass);
            try {
                Object companionUnionObj = companionUnionClass.getField("MODULE$").get(null);
                ThriftStructFieldInfo info = (ThriftStructFieldInfo) companionUnionClass.getMethod("fieldInfo").invoke(companionUnionObj);
                fields.add(info);
            } catch (NoSuchFieldException e) {
                throw new ScroogeSchemaConversionException("can not find fieldInfo for " + unionClass, e);
            } catch (InvocationTargetException e) {
                throw new ScroogeSchemaConversionException("can not find fieldInfo for " + unionClass, e);
            } catch (NoSuchMethodException e) {
                throw new ScroogeSchemaConversionException("can not find fieldInfo for " + unionClass, e);
            } catch (IllegalAccessException e) {
                throw new ScroogeSchemaConversionException("can not find fieldInfo for " + unionClass, e);
            }
        }
    }
    return fields;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ThriftField(org.apache.parquet.thrift.struct.ThriftField) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ThriftStructFieldInfo(com.twitter.scrooge.ThriftStructFieldInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ThriftStructFieldInfo

use of com.twitter.scrooge.ThriftStructFieldInfo in project parquet-mr by apache.

the class ScroogeStructConverter method convertCompanionClassToStruct.

private ThriftType.StructType convertCompanionClassToStruct(Class<?> companionClass) {
    ThriftStructCodec<?> companionObject;
    try {
        companionObject = (ThriftStructCodec<?>) companionClass.getField("MODULE$").get(null);
    } catch (NoSuchFieldException e) {
        throw new ScroogeSchemaConversionException("Can not get ThriftStructCodec from companion object of " + companionClass.getName(), e);
    } catch (IllegalAccessException e) {
        throw new ScroogeSchemaConversionException("Can not get ThriftStructCodec from companion object of " + companionClass.getName(), e);
    }
    // {@link ThriftType.StructType} uses foreach loop to iterate the children, yields O(n) time for linked list
    List<ThriftField> children = new LinkedList<ThriftField>();
    Iterable<ThriftStructFieldInfo> scroogeFields = getFieldInfos(companionObject);
    for (ThriftStructFieldInfo field : scroogeFields) {
        children.add(toThriftField(field));
    }
    StructOrUnionType structOrUnionType = isUnion(companionObject.getClass()) ? StructOrUnionType.UNION : StructOrUnionType.STRUCT;
    return new ThriftType.StructType(children, structOrUnionType);
}
Also used : StructOrUnionType(org.apache.parquet.thrift.struct.ThriftType.StructType.StructOrUnionType) ThriftStructFieldInfo(com.twitter.scrooge.ThriftStructFieldInfo) LinkedList(java.util.LinkedList) ThriftField(org.apache.parquet.thrift.struct.ThriftField)

Aggregations

ThriftStructFieldInfo (com.twitter.scrooge.ThriftStructFieldInfo)2 ThriftField (org.apache.parquet.thrift.struct.ThriftField)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 StructOrUnionType (org.apache.parquet.thrift.struct.ThriftType.StructType.StructOrUnionType)1