Java Code Examples for org.apache.flink.api.java.tuple.Tuple#getArity()

The following examples show how to use org.apache.flink.api.java.tuple.Tuple#getArity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeeplyEqualsChecker.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean deepEqualsTuple(Tuple tuple1, Tuple tuple2) {
	if (tuple1.getArity() != tuple2.getArity()) {
		return false;
	}

	for (int i = 0; i < tuple1.getArity(); i++) {
		Object o1 = tuple1.getField(i);
		Object o2 = tuple2.getField(i);

		if (!deepEquals(o1, o2)) {
			return false;
		}
	}

	return true;
}
 
Example 2
Source File: ArrayFromTuple.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Object[] extract(Tuple in) {
	Object[] output;

	if (order == null) {
		// copy the whole tuple
		output = new Object[in.getArity()];
		for (int i = 0; i < in.getArity(); i++) {
			output[i] = in.getField(i);
		}
	} else {
		// copy user specified order
		output = new Object[order.length];
		for (int i = 0; i < order.length; i++) {
			output[i] = in.getField(order[i]);
		}
	}

	return output;
}
 
Example 3
Source File: DriverTestData.java    From flink with Apache License 2.0 6 votes vote down vote up
public static final void compareTupleArrays(Object[] expected, Object[] found) {
	if (expected.length != found.length) {
		Assert.assertEquals("Length of result is wrong", expected.length, found.length);
	}
	
	for (int i = 0; i < expected.length; i++) {
		Tuple v1 = (Tuple) expected[i];
		Tuple v2 = (Tuple) found[i];
		
		for (int k = 0; k < v1.getArity(); k++) {
			Object o1 = v1.getField(k);
			Object o2 = v2.getField(k);
			Assert.assertEquals(o1, o2);
		}
	}
}
 
Example 4
Source File: DeeplyEqualsChecker.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean deepEqualsTuple(Tuple tuple1, Tuple tuple2) {
	if (tuple1.getArity() != tuple2.getArity()) {
		return false;
	}

	for (int i = 0; i < tuple1.getArity(); i++) {
		Object o1 = tuple1.getField(i);
		Object o2 = tuple2.getField(i);

		if (!deepEquals(o1, o2)) {
			return false;
		}
	}

	return true;
}
 
Example 5
Source File: ArrayFromTuple.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Object[] extract(Tuple in) {
	Object[] output;

	if (order == null) {
		// copy the whole tuple
		output = new Object[in.getArity()];
		for (int i = 0; i < in.getArity(); i++) {
			output[i] = in.getField(i);
		}
	} else {
		// copy user specified order
		output = new Object[order.length];
		for (int i = 0; i < order.length; i++) {
			output[i] = in.getField(order[i]);
		}
	}

	return output;
}
 
Example 6
Source File: DriverTestData.java    From flink with Apache License 2.0 6 votes vote down vote up
public static final void compareTupleArrays(Object[] expected, Object[] found) {
	if (expected.length != found.length) {
		Assert.assertEquals("Length of result is wrong", expected.length, found.length);
	}
	
	for (int i = 0; i < expected.length; i++) {
		Tuple v1 = (Tuple) expected[i];
		Tuple v2 = (Tuple) found[i];
		
		for (int k = 0; k < v1.getArity(); k++) {
			Object o1 = v1.getField(k);
			Object o2 = v2.getField(k);
			Assert.assertEquals(o1, o2);
		}
	}
}
 
Example 7
Source File: ArrayFromTuple.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public Object[] extract(Tuple in) {
	Object[] output;

	if (order == null) {
		// copy the whole tuple
		output = new Object[in.getArity()];
		for (int i = 0; i < in.getArity(); i++) {
			output[i] = in.getField(i);
		}
	} else {
		// copy user specified order
		output = new Object[order.length];
		for (int i = 0; i < order.length; i++) {
			output[i] = in.getField(order[i]);
		}
	}

	return output;
}
 
Example 8
Source File: DriverTestData.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static final void compareTupleArrays(Object[] expected, Object[] found) {
	if (expected.length != found.length) {
		Assert.assertEquals("Length of result is wrong", expected.length, found.length);
	}
	
	for (int i = 0; i < expected.length; i++) {
		Tuple v1 = (Tuple) expected[i];
		Tuple v2 = (Tuple) found[i];
		
		for (int k = 0; k < v1.getArity(); k++) {
			Object o1 = v1.getField(k);
			Object o2 = v2.getField(k);
			Assert.assertEquals(o1, o2);
		}
	}
}
 
Example 9
Source File: DeeplyEqualsChecker.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private boolean deepEqualsTuple(Tuple tuple1, Tuple tuple2) {
	if (tuple1.getArity() != tuple2.getArity()) {
		return false;
	}

	for (int i = 0; i < tuple1.getArity(); i++) {
		Object o1 = tuple1.getField(i);
		Object o2 = tuple2.getField(i);

		if (!deepEquals(o1, o2)) {
			return false;
		}
	}

	return true;
}
 
Example 10
Source File: PythonPlanReceiver.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple deserialize(boolean normalized) throws IOException {
	Tuple result = createTuple(deserializer.length);
	for (int x = 0; x < result.getArity(); x++) {
		result.setField(deserializer[x].deserialize(normalized), x);
	}
	return result;
}
 
Example 11
Source File: SiddhiTupleFactory.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends Tuple> T setTupleValue(Tuple tuple, Object[] row) {
    if (row.length != tuple.getArity()) {
        throw new IllegalArgumentException("Row length" + row.length + " is not equal with tuple's arity: " + tuple.getArity());
    }
    for (int i = 0; i < row.length; i++) {
        tuple.setField(row[i], i);
    }
    return (T) tuple;
}
 
Example 12
Source File: SiddhiTupleFactory.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends Tuple> T setTupleValue(Tuple tuple, Object[] row) {
    if (row.length != tuple.getArity()) {
        throw new IllegalArgumentException("Row length" + row.length + " is not equal with tuple's arity: " + tuple.getArity());
    }
    for (int i = 0; i < row.length; i++) {
        tuple.setField(row[i], i);
    }
    return (T) tuple;
}
 
Example 13
Source File: PythonOperationInfo.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static String[] tupleToStringArray(Tuple tuple) {
	String[] keys = new String[tuple.getArity()];
	for (int y = 0; y < tuple.getArity(); y++) {
		keys[y] = (String) tuple.getField(y);
	}
	return keys;
}
 
Example 14
Source File: TestBaseUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
private static <T> void compareResult(List<T> result, String expected, boolean asTuples, boolean sort) {
	String[] expectedStrings = expected.split("\n");
	String[] resultStrings = new String[result.size()];

	for (int i = 0; i < resultStrings.length; i++) {
		T val = result.get(i);

		if (asTuples) {
			if (val instanceof Tuple) {
				Tuple t = (Tuple) val;
				Object first = t.getField(0);
				StringBuilder bld = new StringBuilder(first == null ? "null" : first.toString());
				for (int pos = 1; pos < t.getArity(); pos++) {
					Object next = t.getField(pos);
					bld.append(',').append(next == null ? "null" : next.toString());
				}
				resultStrings[i] = bld.toString();
			}
			else {
				throw new IllegalArgumentException(val + " is no tuple");
			}
		}
		else {
			resultStrings[i] = (val == null) ? "null" : val.toString();
		}
	}

	if (sort) {
		Arrays.sort(expectedStrings);
		Arrays.sort(resultStrings);
	}

	// Include content of both arrays to provide more context in case of a test failure
	String msg = String.format(
		"Different elements in arrays: expected %d elements and received %d\n expected: %s\n received: %s",
		expectedStrings.length, resultStrings.length,
		Arrays.toString(expectedStrings), Arrays.toString(resultStrings));

	assertEquals(msg, expectedStrings.length, resultStrings.length);

	for (int i = 0; i < expectedStrings.length; i++) {
		assertEquals(msg, expectedStrings[i], resultStrings[i]);
	}
}
 
Example 15
Source File: TypeExtractor.java    From flink with Apache License 2.0 4 votes vote down vote up
@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())) {
			// not a tuple since it has more fields.
			return analyzePojo((Class<X>) value.getClass(), new ArrayList<Type>(), null, null, null); // we immediately call analyze Pojo here, because
			// 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>());
	}
}
 
Example 16
Source File: TestBaseUtils.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static <T> void compareResult(List<T> result, String expected, boolean asTuples, boolean sort) {
	String[] expectedStrings = expected.split("\n");
	String[] resultStrings = new String[result.size()];

	for (int i = 0; i < resultStrings.length; i++) {
		T val = result.get(i);

		if (asTuples) {
			if (val instanceof Tuple) {
				Tuple t = (Tuple) val;
				Object first = t.getField(0);
				StringBuilder bld = new StringBuilder(first == null ? "null" : first.toString());
				for (int pos = 1; pos < t.getArity(); pos++) {
					Object next = t.getField(pos);
					bld.append(',').append(next == null ? "null" : next.toString());
				}
				resultStrings[i] = bld.toString();
			}
			else {
				throw new IllegalArgumentException(val + " is no tuple");
			}
		}
		else {
			resultStrings[i] = (val == null) ? "null" : val.toString();
		}
	}

	if (sort) {
		Arrays.sort(expectedStrings);
		Arrays.sort(resultStrings);
	}

	// Include content of both arrays to provide more context in case of a test failure
	String msg = String.format(
		"Different elements in arrays: expected %d elements and received %d\n expected: %s\n received: %s",
		expectedStrings.length, resultStrings.length,
		Arrays.toString(expectedStrings), Arrays.toString(resultStrings));

	assertEquals(msg, expectedStrings.length, resultStrings.length);

	for (int i = 0; i < expectedStrings.length; i++) {
		assertEquals(msg, expectedStrings[i], resultStrings[i]);
	}
}
 
Example 17
Source File: SerializationUtils.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public TupleSerializer(Tuple value) {
	serializer = new Serializer[value.getArity()];
	for (int x = 0; x < serializer.length; x++) {
		serializer[x] = getSerializer(value.getField(x));
	}
}
 
Example 18
Source File: TestBaseUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
private static <T> void compareResult(List<T> result, String expected, boolean asTuples, boolean sort) {
	String[] expectedStrings = expected.split("\n");
	String[] resultStrings = new String[result.size()];

	for (int i = 0; i < resultStrings.length; i++) {
		T val = result.get(i);

		if (asTuples) {
			if (val instanceof Tuple) {
				Tuple t = (Tuple) val;
				Object first = t.getField(0);
				StringBuilder bld = new StringBuilder(first == null ? "null" : first.toString());
				for (int pos = 1; pos < t.getArity(); pos++) {
					Object next = t.getField(pos);
					bld.append(',').append(next == null ? "null" : next.toString());
				}
				resultStrings[i] = bld.toString();
			}
			else {
				throw new IllegalArgumentException(val + " is no tuple");
			}
		}
		else {
			resultStrings[i] = (val == null) ? "null" : val.toString();
		}
	}

	if (sort) {
		Arrays.sort(expectedStrings);
		Arrays.sort(resultStrings);
	}

	// Include content of both arrays to provide more context in case of a test failure
	String msg = String.format(
		"Different elements in arrays: expected %d elements and received %d\n expected: %s\n received: %s",
		expectedStrings.length, resultStrings.length,
		Arrays.toString(expectedStrings), Arrays.toString(resultStrings));

	assertEquals(msg, expectedStrings.length, resultStrings.length);

	for (int i = 0; i < expectedStrings.length; i++) {
		assertEquals(msg, expectedStrings[i], resultStrings[i]);
	}
}
 
Example 19
Source File: TypeExtractor.java    From flink with Apache License 2.0 4 votes vote down vote up
@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())) {
			// not a tuple since it has more fields.
			return analyzePojo((Class<X>) value.getClass(), new ArrayList<Type>(), null, null, null); // we immediately call analyze Pojo here, because
			// 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>());
	}
}
 
Example 20
Source File: TypeExtractor.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@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())) {
			// not a tuple since it has more fields.
			return analyzePojo((Class<X>) value.getClass(), new ArrayList<Type>(), null, null, null); // we immediately call analyze Pojo here, because
			// 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>());
	}
}