Search in sources :

Example 1 with ThriftType

use of org.apache.parquet.thrift.struct.ThriftType in project parquet-mr by apache.

the class ScroogeStructConverter method convertMapTypeField.

private ThriftType convertMapTypeField(String fieldName, Manifest<?> keyManifest, Manifest<?> valueManifest, Requirement requirement) {
    String keyName = mapKeyName(fieldName);
    String valueName = mapValueName(fieldName);
    ThriftType keyType = convertClassToThriftType(keyName, requirement, keyManifest);
    ThriftField keyField = generateFieldWithoutId(keyName, requirement, keyType);
    ThriftType valueType = convertClassToThriftType(valueName, requirement, valueManifest);
    ThriftField valueField = generateFieldWithoutId(valueName, requirement, valueType);
    return new ThriftType.MapType(keyField, valueField);
}
Also used : ThriftType(org.apache.parquet.thrift.struct.ThriftType) ThriftField(org.apache.parquet.thrift.struct.ThriftField)

Example 2 with ThriftType

use of org.apache.parquet.thrift.struct.ThriftType in project parquet-mr by apache.

the class ScroogeStructConverter method convertEnumTypeField.

private ThriftType convertEnumTypeField(Class enumClass, String fieldName) {
    List<ThriftType.EnumValue> enumValues = new ArrayList<ThriftType.EnumValue>();
    String enumName = enumClass.getName();
    try {
        List enumCollection = getEnumList(enumName);
        for (Object enumObj : enumCollection) {
            ScroogeEnumDesc enumDesc = ScroogeEnumDesc.fromEnum(enumObj);
            enumValues.add(new ThriftType.EnumValue(enumDesc.id, enumDesc.originalName));
        }
        return new ThriftType.EnumType(enumValues);
    } catch (RuntimeException e) {
        throw new ScroogeSchemaConversionException("Can not convert enum field " + fieldName, e);
    } catch (NoSuchMethodException e) {
        throw new ScroogeSchemaConversionException("Can not convert enum field " + fieldName, e);
    } catch (IllegalAccessException e) {
        throw new ScroogeSchemaConversionException("Can not convert enum field " + fieldName, e);
    } catch (NoSuchFieldException e) {
        throw new ScroogeSchemaConversionException("Can not convert enum field " + fieldName, e);
    } catch (InvocationTargetException e) {
        throw new ScroogeSchemaConversionException("Can not convert enum field " + fieldName, e);
    } catch (ClassNotFoundException e) {
        throw new ScroogeSchemaConversionException("Can not convert enum field " + fieldName, e);
    }
}
Also used : ThriftType(org.apache.parquet.thrift.struct.ThriftType) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 3 with ThriftType

use of org.apache.parquet.thrift.struct.ThriftType in project parquet-mr by apache.

the class ScroogeStructConverter method convertClassToThriftType.

/**
 * In composite types,  such as the type of the key in a map, since we use reflection to get the type class, this method
 * does conversion based on the class provided.
 *
 * @return converted ThriftType
 */
private ThriftType convertClassToThriftType(String name, Requirement requirement, Manifest<?> typeManifest) {
    Class typeClass = typeManifest.runtimeClass();
    if (typeManifest.runtimeClass() == boolean.class) {
        return new ThriftType.BoolType();
    } else if (typeClass == byte.class) {
        return new ThriftType.ByteType();
    } else if (typeClass == double.class) {
        return new ThriftType.DoubleType();
    } else if (typeClass == short.class) {
        return new ThriftType.I16Type();
    } else if (typeClass == int.class) {
        return new ThriftType.I32Type();
    } else if (typeClass == long.class) {
        return new ThriftType.I64Type();
    } else if (typeClass == String.class) {
        return new ThriftType.StringType();
    } else if (typeClass == ByteBuffer.class) {
        return new ThriftType.StringType();
    } else if (typeClass == scala.collection.Seq.class) {
        Manifest<?> a = typeManifest.typeArguments().apply(0);
        return convertListTypeField(name, a, requirement);
    } else if (typeClass == scala.collection.Set.class) {
        Manifest<?> setElementManifest = typeManifest.typeArguments().apply(0);
        return convertSetTypeField(name, setElementManifest, requirement);
    } else if (typeClass == scala.collection.Map.class) {
        List<Manifest<?>> ms = JavaConversions.seqAsJavaList(typeManifest.typeArguments());
        Manifest keyManifest = ms.get(0);
        Manifest valueManifest = ms.get(1);
        return convertMapTypeField(name, keyManifest, valueManifest, requirement);
    } else if (com.twitter.scrooge.ThriftEnum.class.isAssignableFrom(typeClass)) {
        return convertEnumTypeField(typeClass, name);
    } else {
        return convertStructFromClass(typeClass);
    }
}
Also used : ThriftType(org.apache.parquet.thrift.struct.ThriftType) Manifest(scala.reflect.Manifest) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ThriftType

use of org.apache.parquet.thrift.struct.ThriftType in project parquet-mr by apache.

the class ScroogeStructConverter method convertSetTypeField.

private ThriftType convertSetTypeField(String fieldName, Manifest<?> valueManifest, Requirement requirement) {
    String elemName = setElemName(fieldName);
    ThriftType elementType = convertClassToThriftType(elemName, requirement, valueManifest);
    // Set only has one sub-field as element field, therefore using hard-coded 1 as fieldId,
    // it's the same as the solution used in ElephantBird
    ThriftField elementField = generateFieldWithoutId(elemName, requirement, elementType);
    return new ThriftType.SetType(elementField);
}
Also used : ThriftType(org.apache.parquet.thrift.struct.ThriftType) ThriftField(org.apache.parquet.thrift.struct.ThriftField)

Example 5 with ThriftType

use of org.apache.parquet.thrift.struct.ThriftType in project parquet-mr by apache.

the class TestThriftSchemaConverter method testToThriftType.

@Test
public void testToThriftType() throws Exception {
    final StructType converted = ThriftSchemaConverter.toStructType(AddressBook.class);
    final String json = converted.toJSON();
    final ThriftType fromJSON = StructType.fromJSON(json);
    assertEquals(json, fromJSON.toJSON());
}
Also used : ThriftType(org.apache.parquet.thrift.struct.ThriftType) StructType(org.apache.parquet.thrift.struct.ThriftType.StructType) Test(org.junit.Test)

Aggregations

ThriftType (org.apache.parquet.thrift.struct.ThriftType)9 ThriftField (org.apache.parquet.thrift.struct.ThriftField)5 ArrayList (java.util.ArrayList)2 StructType (org.apache.parquet.thrift.struct.ThriftType.StructType)2 Field (com.twitter.elephantbird.thrift.TStructDescriptor.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 GroupColumnIO (org.apache.parquet.io.GroupColumnIO)1 PrimitiveColumnIO (org.apache.parquet.io.PrimitiveColumnIO)1 Requirement (org.apache.parquet.thrift.struct.ThriftField.Requirement)1 EnumType (org.apache.parquet.thrift.struct.ThriftType.EnumType)1 ListType (org.apache.parquet.thrift.struct.ThriftType.ListType)1 MapType (org.apache.parquet.thrift.struct.ThriftType.MapType)1 SetType (org.apache.parquet.thrift.struct.ThriftType.SetType)1 ThriftTypeID (org.apache.parquet.thrift.struct.ThriftTypeID)1 TEnum (org.apache.thrift.TEnum)1 FieldMetaData (org.apache.thrift.meta_data.FieldMetaData)1 TProtocol (org.apache.thrift.protocol.TProtocol)1