Search in sources :

Example 51 with TypeInformation

use of org.apache.flink.api.common.typeinfo.TypeInformation in project flink by apache.

the class FieldAccessorFactory method getAccessor.

/**
	 * Creates a {@link FieldAccessor} for the given field position, which can be used to get and set
	 * the specified field on instances of this type.
	 *
	 * @param pos The field position (zero-based)
	 * @param config Configuration object
	 * @param <F> The type of the field to access
	 * @return The created FieldAccessor
	 */
@Internal
public static <T, F> FieldAccessor<T, F> getAccessor(TypeInformation<T> typeInfo, int pos, ExecutionConfig config) {
    // In case of arrays
    if (typeInfo instanceof BasicArrayTypeInfo || typeInfo instanceof PrimitiveArrayTypeInfo) {
        return new FieldAccessor.ArrayFieldAccessor<>(pos, typeInfo);
    // In case of basic types
    } else if (typeInfo instanceof BasicTypeInfo) {
        if (pos != 0) {
            throw new CompositeType.InvalidFieldReferenceException("The " + ((Integer) pos).toString() + ". field selected on a " + "basic type (" + typeInfo.toString() + "). A field expression on a basic type can only select " + "the 0th field (which means selecting the entire basic type).");
        }
        @SuppressWarnings("unchecked") FieldAccessor<T, F> result = (FieldAccessor<T, F>) new FieldAccessor.SimpleFieldAccessor<>(typeInfo);
        return result;
    // In case of case classes
    } else if (typeInfo.isTupleType() && ((TupleTypeInfoBase) typeInfo).isCaseClass()) {
        TupleTypeInfoBase tupleTypeInfo = (TupleTypeInfoBase) typeInfo;
        @SuppressWarnings("unchecked") TypeInformation<F> fieldTypeInfo = (TypeInformation<F>) tupleTypeInfo.getTypeAt(pos);
        return new FieldAccessor.RecursiveProductFieldAccessor<>(pos, typeInfo, new FieldAccessor.SimpleFieldAccessor<>(fieldTypeInfo), config);
    // In case of tuples
    } else if (typeInfo.isTupleType()) {
        @SuppressWarnings("unchecked") FieldAccessor<T, F> result = new FieldAccessor.SimpleTupleFieldAccessor(pos, typeInfo);
        return result;
    // Default case, PojoType is directed to this statement
    } else {
        throw new CompositeType.InvalidFieldReferenceException("Cannot reference field by position on " + typeInfo.toString() + "Referencing a field by position is supported on tuples, case classes, and arrays. " + "Additionally, you can select the 0th field of a primitive/basic type (e.g. int).");
    }
}
Also used : TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) TupleTypeInfoBase(org.apache.flink.api.java.typeutils.TupleTypeInfoBase) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) Internal(org.apache.flink.annotation.Internal)

Aggregations

TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)51 Test (org.junit.Test)28 Row (org.apache.flink.types.Row)21 Configuration (org.apache.flink.configuration.Configuration)20 FileInputSplit (org.apache.flink.core.fs.FileInputSplit)20 TupleTypeInfo (org.apache.flink.api.java.typeutils.TupleTypeInfo)10 ArrayList (java.util.ArrayList)9 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)8 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)8 IOException (java.io.IOException)7 Type (java.lang.reflect.Type)7 GenericArrayType (java.lang.reflect.GenericArrayType)6 ParameterizedType (java.lang.reflect.ParameterizedType)6 Random (java.util.Random)6 InvalidTypesException (org.apache.flink.api.common.functions.InvalidTypesException)6 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)6 ValueTypeInfo (org.apache.flink.api.java.typeutils.ValueTypeInfo)6 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)5 TypeVariable (java.lang.reflect.TypeVariable)4 MutableObjectIterator (org.apache.flink.util.MutableObjectIterator)4