Search in sources :

Example 46 with Tuple

use of org.apache.flink.api.java.tuple.Tuple in project flink by apache.

the class StormTupleTest method testLongTuple.

@Test
public void testLongTuple() {
    final Long data = this.r.nextLong();
    final int index = this.r.nextInt(5);
    final Tuple flinkTuple = new Tuple5<Object, Object, Object, Object, Object>();
    flinkTuple.setField(data, index);
    final StormTuple<Tuple> tuple = new StormTuple<Tuple>(flinkTuple, null, -1, null, null, null);
    Assert.assertEquals(flinkTuple.getField(index), tuple.getLong(index));
}
Also used : Tuple5(org.apache.flink.api.java.tuple.Tuple5) Tuple(org.apache.flink.api.java.tuple.Tuple) Test(org.junit.Test) AbstractTest(org.apache.flink.storm.util.AbstractTest)

Example 47 with Tuple

use of org.apache.flink.api.java.tuple.Tuple in project flink by apache.

the class StormTupleTest method testBinaryTuple.

@Test
public void testBinaryTuple() {
    final byte[] data = new byte[this.r.nextInt(15)];
    this.r.nextBytes(data);
    final int index = this.r.nextInt(5);
    final Tuple flinkTuple = new Tuple5<Object, Object, Object, Object, Object>();
    flinkTuple.setField(data, index);
    final StormTuple<Tuple> tuple = new StormTuple<Tuple>(flinkTuple, null, -1, null, null, null);
    Assert.assertEquals(flinkTuple.getField(index), tuple.getBinary(index));
}
Also used : Tuple5(org.apache.flink.api.java.tuple.Tuple5) Tuple(org.apache.flink.api.java.tuple.Tuple) Test(org.junit.Test) AbstractTest(org.apache.flink.storm.util.AbstractTest)

Example 48 with Tuple

use of org.apache.flink.api.java.tuple.Tuple in project flink by apache.

the class StormTupleTest method testShortTuple.

@Test
public void testShortTuple() {
    final Short data = (short) this.r.nextInt();
    final int index = this.r.nextInt(5);
    final Tuple flinkTuple = new Tuple5<Object, Object, Object, Object, Object>();
    flinkTuple.setField(data, index);
    final StormTuple<Tuple> tuple = new StormTuple<Tuple>(flinkTuple, null, -1, null, null, null);
    Assert.assertEquals(flinkTuple.getField(index), tuple.getShort(index));
}
Also used : Tuple5(org.apache.flink.api.java.tuple.Tuple5) Tuple(org.apache.flink.api.java.tuple.Tuple) Test(org.junit.Test) AbstractTest(org.apache.flink.storm.util.AbstractTest)

Example 49 with Tuple

use of org.apache.flink.api.java.tuple.Tuple in project flink by apache.

the class StormTupleTest method testBinary.

@Test
public void testBinary() {
    final byte[] data = new byte[this.r.nextInt(15)];
    this.r.nextBytes(data);
    final int index = this.r.nextInt(5);
    final Tuple flinkTuple = new Tuple5<Object, Object, Object, Object, Object>();
    flinkTuple.setField(data, index);
    final StormTuple<Tuple> tuple = new StormTuple<Tuple>(flinkTuple, null, -1, null, null, null);
    Assert.assertEquals(flinkTuple.getField(index), tuple.getBinary(index));
}
Also used : Tuple5(org.apache.flink.api.java.tuple.Tuple5) Tuple(org.apache.flink.api.java.tuple.Tuple) Test(org.junit.Test) AbstractTest(org.apache.flink.storm.util.AbstractTest)

Example 50 with Tuple

use of org.apache.flink.api.java.tuple.Tuple in project flink by apache.

the class TypeExtractor method createTypeInfoFromInput.

/**
	 * Finds the type information to a type variable.
	 *
	 * It solve the following:
	 *
	 * Return the type information for "returnTypeVar" given that "inType" has type information "inTypeInfo".
	 * Thus "inType" must contain "returnTypeVar" in a "inputTypeHierarchy", otherwise null is returned.
	 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private <IN1> TypeInformation<?> createTypeInfoFromInput(TypeVariable<?> returnTypeVar, ArrayList<Type> inputTypeHierarchy, Type inType, TypeInformation<IN1> inTypeInfo) {
    TypeInformation<?> info = null;
    // use a factory to find corresponding type information to type variable
    final ArrayList<Type> factoryHierarchy = new ArrayList<>(inputTypeHierarchy);
    final TypeInfoFactory<?> factory = getClosestFactory(factoryHierarchy, inType);
    if (factory != null) {
        // the type that defines the factory is last in factory hierarchy
        final Type factoryDefiningType = factoryHierarchy.get(factoryHierarchy.size() - 1);
        // defining type has generics, the factory need to be asked for a mapping of subtypes to type information
        if (factoryDefiningType instanceof ParameterizedType) {
            final Type[] typeParams = typeToClass(factoryDefiningType).getTypeParameters();
            final Type[] actualParams = ((ParameterizedType) factoryDefiningType).getActualTypeArguments();
            // go thru all elements and search for type variables
            for (int i = 0; i < actualParams.length; i++) {
                final Map<String, TypeInformation<?>> componentInfo = inTypeInfo.getGenericParameters();
                final String typeParamName = typeParams[i].toString();
                if (!componentInfo.containsKey(typeParamName) || componentInfo.get(typeParamName) == null) {
                    throw new InvalidTypesException("TypeInformation '" + inTypeInfo.getClass().getSimpleName() + "' does not supply a mapping of TypeVariable '" + typeParamName + "' to corresponding TypeInformation. " + "Input type inference can only produce a result with this information. " + "Please implement method 'TypeInformation.getGenericParameters()' for this.");
                }
                info = createTypeInfoFromInput(returnTypeVar, factoryHierarchy, actualParams[i], componentInfo.get(typeParamName));
                if (info != null) {
                    break;
                }
            }
        }
    } else // the input is a type variable
    if (sameTypeVars(inType, returnTypeVar)) {
        return inTypeInfo;
    } else if (inType instanceof TypeVariable) {
        Type resolvedInType = materializeTypeVariable(inputTypeHierarchy, (TypeVariable<?>) inType);
        if (resolvedInType != inType) {
            info = createTypeInfoFromInput(returnTypeVar, inputTypeHierarchy, resolvedInType, inTypeInfo);
        }
    } else // input is an array
    if (inType instanceof GenericArrayType) {
        TypeInformation<?> componentInfo = null;
        if (inTypeInfo instanceof BasicArrayTypeInfo) {
            componentInfo = ((BasicArrayTypeInfo<?, ?>) inTypeInfo).getComponentInfo();
        } else if (inTypeInfo instanceof PrimitiveArrayTypeInfo) {
            componentInfo = BasicTypeInfo.getInfoFor(inTypeInfo.getTypeClass().getComponentType());
        } else if (inTypeInfo instanceof ObjectArrayTypeInfo) {
            componentInfo = ((ObjectArrayTypeInfo<?, ?>) inTypeInfo).getComponentInfo();
        }
        info = createTypeInfoFromInput(returnTypeVar, inputTypeHierarchy, ((GenericArrayType) inType).getGenericComponentType(), componentInfo);
    } else // the input is a tuple
    if (inTypeInfo instanceof TupleTypeInfo && isClassType(inType) && Tuple.class.isAssignableFrom(typeToClass(inType))) {
        ParameterizedType tupleBaseClass;
        // get tuple from possible tuple subclass
        while (!(isClassType(inType) && typeToClass(inType).getSuperclass().equals(Tuple.class))) {
            inputTypeHierarchy.add(inType);
            inType = typeToClass(inType).getGenericSuperclass();
        }
        inputTypeHierarchy.add(inType);
        // we can assume to be parameterized since we
        // already did input validation
        tupleBaseClass = (ParameterizedType) inType;
        Type[] tupleElements = tupleBaseClass.getActualTypeArguments();
        // go thru all tuple elements and search for type variables
        for (int i = 0; i < tupleElements.length; i++) {
            info = createTypeInfoFromInput(returnTypeVar, inputTypeHierarchy, tupleElements[i], ((TupleTypeInfo<?>) inTypeInfo).getTypeAt(i));
            if (info != null) {
                break;
            }
        }
    } else // the input is a pojo
    if (inTypeInfo instanceof PojoTypeInfo && isClassType(inType)) {
        // build the entire type hierarchy for the pojo
        getTypeHierarchy(inputTypeHierarchy, inType, Object.class);
        // determine a field containing the type variable
        List<Field> fields = getAllDeclaredFields(typeToClass(inType), false);
        for (Field field : fields) {
            Type fieldType = field.getGenericType();
            if (fieldType instanceof TypeVariable && sameTypeVars(returnTypeVar, materializeTypeVariable(inputTypeHierarchy, (TypeVariable<?>) fieldType))) {
                return getTypeOfPojoField(inTypeInfo, field);
            } else if (fieldType instanceof ParameterizedType || fieldType instanceof GenericArrayType) {
                ArrayList<Type> typeHierarchyWithFieldType = new ArrayList<>(inputTypeHierarchy);
                typeHierarchyWithFieldType.add(fieldType);
                TypeInformation<?> foundInfo = createTypeInfoFromInput(returnTypeVar, typeHierarchyWithFieldType, fieldType, getTypeOfPojoField(inTypeInfo, field));
                if (foundInfo != null) {
                    return foundInfo;
                }
            }
        }
    }
    return info;
}
Also used : ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) 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) TypeVariable(java.lang.reflect.TypeVariable) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) List(java.util.List) ArrayList(java.util.ArrayList) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo) Tuple(org.apache.flink.api.java.tuple.Tuple)

Aggregations

Tuple (org.apache.flink.api.java.tuple.Tuple)59 Test (org.junit.Test)38 AbstractTest (org.apache.flink.storm.util.AbstractTest)17 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)14 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)14 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)13 Tuple5 (org.apache.flink.api.java.tuple.Tuple5)10 ArrayList (java.util.ArrayList)9 Configuration (org.apache.flink.configuration.Configuration)8 SuccessException (org.apache.flink.test.util.SuccessException)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)6 Fields (org.apache.storm.tuple.Fields)6 Tuple4 (org.apache.flink.api.java.tuple.Tuple4)5 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)5 Keys (org.apache.flink.api.common.operators.Keys)4 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)4 ComparableAggregator (org.apache.flink.streaming.api.functions.aggregation.ComparableAggregator)4 Values (org.apache.storm.tuple.Values)4