Search in sources :

Example 31 with TypeInformation

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

the class HBaseTableSource method getReturnType.

@Override
public TypeInformation<Row> getReturnType() {
    String[] famNames = schema.getFamilyNames();
    TypeInformation<?>[] typeInfos = new TypeInformation[famNames.length];
    int i = 0;
    for (String family : famNames) {
        typeInfos[i] = new RowTypeInfo(schema.getQualifierTypes(family), schema.getQualifierNames(family));
        i++;
    }
    return new RowTypeInfo(typeInfos, famNames);
}
Also used : RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 32 with TypeInformation

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

the class HBaseTableSchema method getQualifierKeys.

/**
	 * Returns the HBase identifiers of all registered column qualifiers for a specific column family.
	 *
	 * @param family The name of the column family for which the column qualifier identifiers are returned.
	 * @return The HBase identifiers of all registered column qualifiers for a specific column family.
	 */
byte[][] getQualifierKeys(String family) {
    Map<String, TypeInformation<?>> qualifierMap = familyMap.get(family);
    if (qualifierMap == null) {
        throw new IllegalArgumentException("Family " + family + " does not exist in schema.");
    }
    Charset c = Charset.forName(charset);
    byte[][] qualifierKeys = new byte[qualifierMap.size()][];
    int i = 0;
    for (String name : qualifierMap.keySet()) {
        qualifierKeys[i++] = name.getBytes(c);
    }
    return qualifierKeys;
}
Also used : Charset(java.nio.charset.Charset) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 33 with TypeInformation

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

the class TypeExtractor method privateGetForObject.

@SuppressWarnings({ "unchecked", "rawtypes" })
private <X> TypeInformation<X> privateGetForObject(X value) {
    checkNotNull(value);
    // check if type information can be produced using a factory
    final ArrayList<Type> typeHierarchy = new ArrayList<>();
    typeHierarchy.add(value.getClass());
    final TypeInformation<X> typeFromFactory = createTypeInfoFromFactory(value.getClass(), typeHierarchy, null, null);
    if (typeFromFactory != null) {
        return typeFromFactory;
    }
    // check if we can extract the types from tuples, otherwise work with the class
    if (value instanceof Tuple) {
        Tuple t = (Tuple) value;
        int numFields = t.getArity();
        if (numFields != countFieldsInClass(value.getClass())) {
            // we immediately call analyze Pojo here, because
            return analyzePojo((Class<X>) value.getClass(), new ArrayList<Type>(), null, null, null);
        // there is currently no other type that can handle such a class.
        }
        TypeInformation<?>[] infos = new TypeInformation[numFields];
        for (int i = 0; i < numFields; i++) {
            Object field = t.getField(i);
            if (field == null) {
                throw new InvalidTypesException("Automatic type extraction is not possible on candidates with null values. " + "Please specify the types directly.");
            }
            infos[i] = privateGetForObject(field);
        }
        return new TupleTypeInfo(value.getClass(), infos);
    } else if (value instanceof Row) {
        Row row = (Row) value;
        int arity = row.getArity();
        for (int i = 0; i < arity; i++) {
            if (row.getField(i) == null) {
                LOG.warn("Cannot extract type of Row field, because of Row field[" + i + "] is null. " + "Should define RowTypeInfo explicitly.");
                return privateGetForClass((Class<X>) value.getClass(), new ArrayList<Type>());
            }
        }
        TypeInformation<?>[] typeArray = new TypeInformation<?>[arity];
        for (int i = 0; i < arity; i++) {
            typeArray[i] = TypeExtractor.getForObject(row.getField(i));
        }
        return (TypeInformation<X>) new RowTypeInfo(typeArray);
    } else {
        return privateGetForClass((Class<X>) value.getClass(), new ArrayList<Type>());
    }
}
Also used : ArrayList(java.util.ArrayList) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) GenericArrayType(java.lang.reflect.GenericArrayType) TypeExtractionUtils.isClassType(org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType) Type(java.lang.reflect.Type) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) ParameterizedType(java.lang.reflect.ParameterizedType) TypeExtractionUtils.typeToClass(org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass) Row(org.apache.flink.types.Row) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException) Tuple(org.apache.flink.api.java.tuple.Tuple)

Example 34 with TypeInformation

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

the class TypeExtractor method validateInfo.

@SuppressWarnings("unchecked")
private static void validateInfo(ArrayList<Type> typeHierarchy, Type type, TypeInformation<?> typeInfo) {
    if (type == null) {
        throw new InvalidTypesException("Unknown Error. Type is null.");
    }
    if (typeInfo == null) {
        throw new InvalidTypesException("Unknown Error. TypeInformation is null.");
    }
    if (!(type instanceof TypeVariable<?>)) {
        // check for Java Basic Types
        if (typeInfo instanceof BasicTypeInfo) {
            TypeInformation<?> actual;
            // check if basic type at all
            if (!(type instanceof Class<?>) || (actual = BasicTypeInfo.getInfoFor((Class<?>) type)) == null) {
                throw new InvalidTypesException("Basic type expected.");
            }
            // check if correct basic type
            if (!typeInfo.equals(actual)) {
                throw new InvalidTypesException("Basic type '" + typeInfo + "' expected but was '" + actual + "'.");
            }
        } else // check for Java SQL time types
        if (typeInfo instanceof SqlTimeTypeInfo) {
            TypeInformation<?> actual;
            // check if SQL time type at all
            if (!(type instanceof Class<?>) || (actual = SqlTimeTypeInfo.getInfoFor((Class<?>) type)) == null) {
                throw new InvalidTypesException("SQL time type expected.");
            }
            // check if correct SQL time type
            if (!typeInfo.equals(actual)) {
                throw new InvalidTypesException("SQL time type '" + typeInfo + "' expected but was '" + actual + "'.");
            }
        } else // check for Java Tuples
        if (typeInfo instanceof TupleTypeInfo) {
            // check if tuple at all
            if (!(isClassType(type) && Tuple.class.isAssignableFrom(typeToClass(type)))) {
                throw new InvalidTypesException("Tuple type expected.");
            }
            // do not allow usage of Tuple as type
            if (isClassType(type) && typeToClass(type).equals(Tuple.class)) {
                throw new InvalidTypesException("Concrete subclass of Tuple expected.");
            }
            // go up the hierarchy until we reach immediate child of Tuple (with or without generics)
            while (!(isClassType(type) && typeToClass(type).getSuperclass().equals(Tuple.class))) {
                typeHierarchy.add(type);
                type = typeToClass(type).getGenericSuperclass();
            }
            if (type == Tuple0.class) {
                return;
            }
            // check if immediate child of Tuple has generics
            if (type instanceof Class<?>) {
                throw new InvalidTypesException("Parameterized Tuple type expected.");
            }
            TupleTypeInfo<?> tti = (TupleTypeInfo<?>) typeInfo;
            Type[] subTypes = ((ParameterizedType) type).getActualTypeArguments();
            if (subTypes.length != tti.getArity()) {
                throw new InvalidTypesException("Tuple arity '" + tti.getArity() + "' expected but was '" + subTypes.length + "'.");
            }
            for (int i = 0; i < subTypes.length; i++) {
                validateInfo(new ArrayList<Type>(typeHierarchy), subTypes[i], tti.getTypeAt(i));
            }
        } else // check for primitive array
        if (typeInfo instanceof PrimitiveArrayTypeInfo) {
            Type component;
            // check if array at all
            if (!(type instanceof Class<?> && ((Class<?>) type).isArray() && (component = ((Class<?>) type).getComponentType()) != null) && !(type instanceof GenericArrayType && (component = ((GenericArrayType) type).getGenericComponentType()) != null)) {
                throw new InvalidTypesException("Array type expected.");
            }
            if (component instanceof TypeVariable<?>) {
                component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
                if (component instanceof TypeVariable) {
                    return;
                }
            }
            if (!(component instanceof Class<?> && ((Class<?>) component).isPrimitive())) {
                throw new InvalidTypesException("Primitive component expected.");
            }
        } else // check for basic array
        if (typeInfo instanceof BasicArrayTypeInfo<?, ?>) {
            Type component;
            // check if array at all
            if (!(type instanceof Class<?> && ((Class<?>) type).isArray() && (component = ((Class<?>) type).getComponentType()) != null) && !(type instanceof GenericArrayType && (component = ((GenericArrayType) type).getGenericComponentType()) != null)) {
                throw new InvalidTypesException("Array type expected.");
            }
            if (component instanceof TypeVariable<?>) {
                component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
                if (component instanceof TypeVariable) {
                    return;
                }
            }
            validateInfo(typeHierarchy, component, ((BasicArrayTypeInfo<?, ?>) typeInfo).getComponentInfo());
        } else // check for object array
        if (typeInfo instanceof ObjectArrayTypeInfo<?, ?>) {
            // check if array at all
            if (!(type instanceof Class<?> && ((Class<?>) type).isArray()) && !(type instanceof GenericArrayType)) {
                throw new InvalidTypesException("Object array type expected.");
            }
            // check component
            Type component;
            if (type instanceof Class<?>) {
                component = ((Class<?>) type).getComponentType();
            } else {
                component = ((GenericArrayType) type).getGenericComponentType();
            }
            if (component instanceof TypeVariable<?>) {
                component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
                if (component instanceof TypeVariable) {
                    return;
                }
            }
            validateInfo(typeHierarchy, component, ((ObjectArrayTypeInfo<?, ?>) typeInfo).getComponentInfo());
        } else // check for value
        if (typeInfo instanceof ValueTypeInfo<?>) {
            // check if value at all
            if (!(type instanceof Class<?> && Value.class.isAssignableFrom((Class<?>) type))) {
                throw new InvalidTypesException("Value type expected.");
            }
            TypeInformation<?> actual;
            // check value type contents
            if (!((ValueTypeInfo<?>) typeInfo).equals(actual = ValueTypeInfo.getValueTypeInfo((Class<? extends Value>) type))) {
                throw new InvalidTypesException("Value type '" + typeInfo + "' expected but was '" + actual + "'.");
            }
        } else // check for POJO
        if (typeInfo instanceof PojoTypeInfo) {
            Class<?> clazz = null;
            if (!(isClassType(type) && ((PojoTypeInfo<?>) typeInfo).getTypeClass() == (clazz = typeToClass(type)))) {
                throw new InvalidTypesException("POJO type '" + ((PojoTypeInfo<?>) typeInfo).getTypeClass().getCanonicalName() + "' expected but was '" + clazz.getCanonicalName() + "'.");
            }
        } else // check for Enum
        if (typeInfo instanceof EnumTypeInfo) {
            if (!(type instanceof Class<?> && Enum.class.isAssignableFrom((Class<?>) type))) {
                throw new InvalidTypesException("Enum type expected.");
            }
            // check enum type contents
            if (!(typeInfo.getTypeClass() == type)) {
                throw new InvalidTypesException("Enum type '" + typeInfo.getTypeClass().getCanonicalName() + "' expected but was '" + typeToClass(type).getCanonicalName() + "'.");
            }
        } else // check for generic object
        if (typeInfo instanceof GenericTypeInfo<?>) {
            Class<?> clazz = null;
            if (!(isClassType(type) && (clazz = typeToClass(type)).isAssignableFrom(((GenericTypeInfo<?>) typeInfo).getTypeClass()))) {
                throw new InvalidTypesException("Generic type '" + ((GenericTypeInfo<?>) typeInfo).getTypeClass().getCanonicalName() + "' or a subclass of it expected but was '" + clazz.getCanonicalName() + "'.");
            }
        } else // check for Writable
        {
            validateIfWritable(typeInfo, type);
        }
    } else {
        type = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) type);
        if (!(type instanceof TypeVariable)) {
            validateInfo(typeHierarchy, type, typeInfo);
        }
    }
}
Also used : TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException) GenericArrayType(java.lang.reflect.GenericArrayType) SqlTimeTypeInfo(org.apache.flink.api.common.typeinfo.SqlTimeTypeInfo) GenericArrayType(java.lang.reflect.GenericArrayType) TypeExtractionUtils.isClassType(org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType) Type(java.lang.reflect.Type) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) ParameterizedType(java.lang.reflect.ParameterizedType) Value(org.apache.flink.types.Value) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) TypeExtractionUtils.typeToClass(org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass) Tuple(org.apache.flink.api.java.tuple.Tuple) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo)

Example 35 with TypeInformation

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

the class TypeExtractor method createTypeInfoFromFactory.

/**
	 * Creates type information using a factory if for this type or super types. Returns null otherwise.
	 */
@SuppressWarnings("unchecked")
private <IN1, IN2, OUT> TypeInformation<OUT> createTypeInfoFromFactory(Type t, ArrayList<Type> typeHierarchy, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type) {
    final ArrayList<Type> factoryHierarchy = new ArrayList<>(typeHierarchy);
    final TypeInfoFactory<? super OUT> factory = getClosestFactory(factoryHierarchy, t);
    if (factory == null) {
        return null;
    }
    final Type factoryDefiningType = factoryHierarchy.get(factoryHierarchy.size() - 1);
    // infer possible type parameters from input
    final Map<String, TypeInformation<?>> genericParams;
    if (factoryDefiningType instanceof ParameterizedType) {
        genericParams = new HashMap<>();
        final ParameterizedType paramDefiningType = (ParameterizedType) factoryDefiningType;
        final Type[] args = typeToClass(paramDefiningType).getTypeParameters();
        final TypeInformation<?>[] subtypeInfo = createSubTypesInfo(t, paramDefiningType, factoryHierarchy, in1Type, in2Type, true);
        assert subtypeInfo != null;
        for (int i = 0; i < subtypeInfo.length; i++) {
            genericParams.put(args[i].toString(), subtypeInfo[i]);
        }
    } else {
        genericParams = Collections.emptyMap();
    }
    final TypeInformation<OUT> createdTypeInfo = (TypeInformation<OUT>) factory.createTypeInfo(t, genericParams);
    if (createdTypeInfo == null) {
        throw new InvalidTypesException("TypeInfoFactory returned invalid TypeInformation 'null'");
    }
    return createdTypeInfo;
}
Also used : ArrayList(java.util.ArrayList) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) TypeExtractionUtils.isClassType(org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType) Type(java.lang.reflect.Type) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) ParameterizedType(java.lang.reflect.ParameterizedType) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException)

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