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

The following examples show how to use org.apache.flink.api.java.tuple.Tuple#getField() . 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: 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 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: 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 4
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 5
Source File: PythonPlanBinder.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void receiveParameters(ExecutionEnvironment env) throws IOException {
	for (int x = 0; x < Parameters.values().length; x++) {
		Tuple value = (Tuple) streamer.getRecord(true);
		switch (Parameters.valueOf(((String) value.getField(0)).toUpperCase())) {
			case DOP:
				Integer dop = value.<Integer>getField(1);
				env.setParallelism(dop);
				break;
			case RETRY:
				int retry = value.<Integer>getField(1);
				env.setRestartStrategy(RestartStrategies.fixedDelayRestart(retry, 10000L));
				break;
			case ID:
				currentEnvironmentID = value.<Integer>getField(1);
				break;
		}
	}
	if (env.getParallelism() < 0) {
		env.setParallelism(1);
	}
}
 
Example 6
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 7
Source File: ComputeFunction.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given message to all vertices that adjacent to the changed vertex.
 * This method is mutually exclusive to the method {@link #getEdges()} and may be called only once.
 *
 * @param m The message to send.
 */
public final void sendMessageToAllNeighbors(Message m) {
	verifyEdgeUsage();
	outMsg.f1 = m;
	while (edges.hasNext()) {
		Tuple next = edges.next();
		outMsg.f0 = next.getField(1);
		out.collect(Either.Right(outMsg));
	}
}
 
Example 8
Source File: StreamSerializer.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
public Object[] getRow(T input) {
    Preconditions.checkArgument(input.getClass() == schema.getTypeInfo().getTypeClass(),
        "Invalid input type: " + input + ", expected: " + schema.getTypeInfo());

    Object[] data;
    if (schema.isAtomicType()) {
        data = new Object[]{input};
    } else if (schema.isTupleType()) {
        Tuple tuple = (Tuple) input;
        data = new Object[schema.getFieldIndexes().length];
        for (int i = 0; i < schema.getFieldIndexes().length; i++) {
            data[i] = tuple.getField(schema.getFieldIndexes()[i]);
        }
    } else if (schema.isRowType()) {
        Row row = (Row) input;
        data = new Object[schema.getFieldIndexes().length];
        for (int i = 0; i < schema.getFieldIndexes().length; i++) {
            data[i] = row.getField(schema.getFieldIndexes()[i]);
        }
    } else if (schema.isPojoType() || schema.isCaseClassType()) {
        data = new Object[schema.getFieldIndexes().length];
        for (int i = 0; i < schema.getFieldNames().length; i++) {
            data[i] = getFieldValue(schema.getFieldNames()[i], input);
        }
    } else {
        throw new IllegalArgumentException("Failed to get field values from " + schema.getTypeInfo());
    }
    return data;
}
 
Example 9
Source File: ComputeFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given message to all vertices that adjacent to the changed vertex.
 * This method is mutually exclusive to the method {@link #getEdges()} and may be called only once.
 *
 * @param m The message to send.
 */
public final void sendMessageToAllNeighbors(Message m) {
	verifyEdgeUsage();
	outMsg.f1 = m;
	while (edges.hasNext()) {
		Tuple next = edges.next();
		outMsg.f0 = next.getField(1);
		out.collect(Either.Right(outMsg));
	}
}
 
Example 10
Source File: FieldsFromTuple.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public double[] extract(Tuple in) {
	double[] out = new double[indexes.length];
	for (int i = 0; i < indexes.length; i++) {
		out[i] = (Double) in.getField(indexes[i]);
	}
	return out;
}
 
Example 11
Source File: ComputeFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given message to all vertices that adjacent to the changed vertex.
 * This method is mutually exclusive to the method {@link #getEdges()} and may be called only once.
 *
 * @param m The message to send.
 */
public final void sendMessageToAllNeighbors(Message m) {
	verifyEdgeUsage();
	outMsg.f1 = m;
	while (edges.hasNext()) {
		Tuple next = edges.next();
		outMsg.f0 = next.getField(1);
		out.collect(Either.Right(outMsg));
	}
}
 
Example 12
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 13
Source File: DataSetUtilsITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSummarize() throws Exception {
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	List<Tuple8<Short, Integer, Long, Float, Double, String, Boolean, DoubleValue>> data = new ArrayList<>();
	data.add(new Tuple8<>((short) 1, 1, 100L, 0.1f, 1.012376, "hello", false, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 2, 2, 1000L, 0.2f, 2.003453, "hello", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 4, 10, 10000L, 0.2f, 75.00005, "null", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 10, 4, 100L, 0.9f, 79.5, "", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 5, 5, 1000L, 0.2f, 10.0000001, "a", false, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 6, 6, 10L, 0.1f, 0.0000000000023, "", true, new DoubleValue(100.0)));
	data.add(new Tuple8<>((short) 7, 7, 1L, 0.2f, Double.POSITIVE_INFINITY, "abcdefghijklmnop", true, new DoubleValue(100.0)));
	data.add(new Tuple8<>((short) 8, 8, -100L, 0.001f, Double.NaN, "abcdefghi", true, new DoubleValue(100.0)));

	Collections.shuffle(data);

	DataSet<Tuple8<Short, Integer, Long, Float, Double, String, Boolean, DoubleValue>> ds = env.fromCollection(data);

	// call method under test
	Tuple results = DataSetUtils.summarize(ds);

	Assert.assertEquals(8, results.getArity());

	NumericColumnSummary<Short> col0Summary = results.getField(0);
	Assert.assertEquals(8, col0Summary.getNonMissingCount());
	Assert.assertEquals(1, col0Summary.getMin().shortValue());
	Assert.assertEquals(10, col0Summary.getMax().shortValue());
	Assert.assertEquals(5.375, col0Summary.getMean().doubleValue(), 0.0);

	NumericColumnSummary<Integer> col1Summary = results.getField(1);
	Assert.assertEquals(1, col1Summary.getMin().intValue());
	Assert.assertEquals(10, col1Summary.getMax().intValue());
	Assert.assertEquals(5.375, col1Summary.getMean().doubleValue(), 0.0);

	NumericColumnSummary<Long> col2Summary = results.getField(2);
	Assert.assertEquals(-100L, col2Summary.getMin().longValue());
	Assert.assertEquals(10000L, col2Summary.getMax().longValue());

	NumericColumnSummary<Float> col3Summary = results.getField(3);
	Assert.assertEquals(8, col3Summary.getTotalCount());
	Assert.assertEquals(0.001000, col3Summary.getMin().doubleValue(), 0.0000001);
	Assert.assertEquals(0.89999999, col3Summary.getMax().doubleValue(), 0.0000001);
	Assert.assertEquals(0.2376249988883501, col3Summary.getMean().doubleValue(), 0.000000000001);
	Assert.assertEquals(0.0768965488108089, col3Summary.getVariance().doubleValue(), 0.00000001);
	Assert.assertEquals(0.27730226975415995, col3Summary.getStandardDeviation().doubleValue(), 0.000000000001);

	NumericColumnSummary<Double> col4Summary = results.getField(4);
	Assert.assertEquals(6, col4Summary.getNonMissingCount());
	Assert.assertEquals(2, col4Summary.getMissingCount());
	Assert.assertEquals(0.0000000000023, col4Summary.getMin().doubleValue(), 0.0);
	Assert.assertEquals(79.5, col4Summary.getMax().doubleValue(), 0.000000000001);

	StringColumnSummary col5Summary = results.getField(5);
	Assert.assertEquals(8, col5Summary.getTotalCount());
	Assert.assertEquals(0, col5Summary.getNullCount());
	Assert.assertEquals(8, col5Summary.getNonNullCount());
	Assert.assertEquals(2, col5Summary.getEmptyCount());
	Assert.assertEquals(0, col5Summary.getMinLength().intValue());
	Assert.assertEquals(16, col5Summary.getMaxLength().intValue());
	Assert.assertEquals(5.0, col5Summary.getMeanLength().doubleValue(), 0.0001);

	BooleanColumnSummary col6Summary = results.getField(6);
	Assert.assertEquals(8, col6Summary.getTotalCount());
	Assert.assertEquals(2, col6Summary.getFalseCount());
	Assert.assertEquals(6, col6Summary.getTrueCount());
	Assert.assertEquals(0, col6Summary.getNullCount());

	NumericColumnSummary<Double> col7Summary = results.getField(7);
	Assert.assertEquals(100.0, col7Summary.getMax().doubleValue(), 0.00001);
	Assert.assertEquals(50.0, col7Summary.getMin().doubleValue(), 0.00001);
}
 
Example 14
Source File: FieldFromTuple.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public OUT extract(Tuple in) {
	return in.getField(fieldId);
}
 
Example 15
Source File: FieldFromTuple.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public OUT extract(Tuple in) {
	return in.getField(fieldId);
}
 
Example 16
Source File: DataSetUtilsITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSummarize() throws Exception {
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	List<Tuple8<Short, Integer, Long, Float, Double, String, Boolean, DoubleValue>> data = new ArrayList<>();
	data.add(new Tuple8<>((short) 1, 1, 100L, 0.1f, 1.012376, "hello", false, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 2, 2, 1000L, 0.2f, 2.003453, "hello", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 4, 10, 10000L, 0.2f, 75.00005, "null", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 10, 4, 100L, 0.9f, 79.5, "", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 5, 5, 1000L, 0.2f, 10.0000001, "a", false, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 6, 6, 10L, 0.1f, 0.0000000000023, "", true, new DoubleValue(100.0)));
	data.add(new Tuple8<>((short) 7, 7, 1L, 0.2f, Double.POSITIVE_INFINITY, "abcdefghijklmnop", true, new DoubleValue(100.0)));
	data.add(new Tuple8<>((short) 8, 8, -100L, 0.001f, Double.NaN, "abcdefghi", true, new DoubleValue(100.0)));

	Collections.shuffle(data);

	DataSet<Tuple8<Short, Integer, Long, Float, Double, String, Boolean, DoubleValue>> ds = env.fromCollection(data);

	// call method under test
	Tuple results = DataSetUtils.summarize(ds);

	Assert.assertEquals(8, results.getArity());

	NumericColumnSummary<Short> col0Summary = results.getField(0);
	Assert.assertEquals(8, col0Summary.getNonMissingCount());
	Assert.assertEquals(1, col0Summary.getMin().shortValue());
	Assert.assertEquals(10, col0Summary.getMax().shortValue());
	Assert.assertEquals(5.375, col0Summary.getMean().doubleValue(), 0.0);

	NumericColumnSummary<Integer> col1Summary = results.getField(1);
	Assert.assertEquals(1, col1Summary.getMin().intValue());
	Assert.assertEquals(10, col1Summary.getMax().intValue());
	Assert.assertEquals(5.375, col1Summary.getMean().doubleValue(), 0.0);

	NumericColumnSummary<Long> col2Summary = results.getField(2);
	Assert.assertEquals(-100L, col2Summary.getMin().longValue());
	Assert.assertEquals(10000L, col2Summary.getMax().longValue());

	NumericColumnSummary<Float> col3Summary = results.getField(3);
	Assert.assertEquals(8, col3Summary.getTotalCount());
	Assert.assertEquals(0.001000, col3Summary.getMin().doubleValue(), 0.0000001);
	Assert.assertEquals(0.89999999, col3Summary.getMax().doubleValue(), 0.0000001);
	Assert.assertEquals(0.2376249988883501, col3Summary.getMean().doubleValue(), 0.000000000001);
	Assert.assertEquals(0.0768965488108089, col3Summary.getVariance().doubleValue(), 0.00000001);
	Assert.assertEquals(0.27730226975415995, col3Summary.getStandardDeviation().doubleValue(), 0.000000000001);

	NumericColumnSummary<Double> col4Summary = results.getField(4);
	Assert.assertEquals(6, col4Summary.getNonMissingCount());
	Assert.assertEquals(2, col4Summary.getMissingCount());
	Assert.assertEquals(0.0000000000023, col4Summary.getMin().doubleValue(), 0.0);
	Assert.assertEquals(79.5, col4Summary.getMax().doubleValue(), 0.000000000001);

	StringColumnSummary col5Summary = results.getField(5);
	Assert.assertEquals(8, col5Summary.getTotalCount());
	Assert.assertEquals(0, col5Summary.getNullCount());
	Assert.assertEquals(8, col5Summary.getNonNullCount());
	Assert.assertEquals(2, col5Summary.getEmptyCount());
	Assert.assertEquals(0, col5Summary.getMinLength().intValue());
	Assert.assertEquals(16, col5Summary.getMaxLength().intValue());
	Assert.assertEquals(5.0, col5Summary.getMeanLength().doubleValue(), 0.0001);

	BooleanColumnSummary col6Summary = results.getField(6);
	Assert.assertEquals(8, col6Summary.getTotalCount());
	Assert.assertEquals(2, col6Summary.getFalseCount());
	Assert.assertEquals(6, col6Summary.getTrueCount());
	Assert.assertEquals(0, col6Summary.getNullCount());

	NumericColumnSummary<Double> col7Summary = results.getField(7);
	Assert.assertEquals(100.0, col7Summary.getMax().doubleValue(), 0.00001);
	Assert.assertEquals(50.0, col7Summary.getMin().doubleValue(), 0.00001);
}
 
Example 17
Source File: ScatterFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Sends the given message to all vertices that are targets of an edge of the changed vertex.
 * This method is mutually exclusive to the method {@link #getEdges()} and may be called only once.
 *
 * <p>If the {@link EdgeDirection} is OUT (default), the message will be sent to out-neighbors.
 *
 * <p>If the {@link EdgeDirection} is IN, the message will be sent to in-neighbors.
 *
 * <p>If the {@link EdgeDirection} is ALL, the message will be sent to all neighbors.
 *
 * @param m The message to send.
 */
public void sendMessageToAllNeighbors(Message m) {
	if (edgesUsed) {
		throw new IllegalStateException("Can use either 'getEdges()' or 'sendMessageToAllNeighbors()'"
				+ "exactly once.");
	}

	edgesUsed = true;
	outValue.f1 = m;

	while (edges.hasNext()) {
		Tuple next = (Tuple) edges.next();

		/*
		 * When EdgeDirection is OUT, the edges iterator only has the out-edges
		 * of the vertex, i.e. the ones where this vertex is src.
		 * next.getField(1) gives the neighbor of the vertex running this ScatterFunction.
		 */
		if (getDirection().equals(EdgeDirection.OUT)) {
			outValue.f0 = next.getField(1);
		}
		/*
		 * When EdgeDirection is IN, the edges iterator only has the in-edges
		 * of the vertex, i.e. the ones where this vertex is trg.
		 * next.getField(10) gives the neighbor of the vertex running this ScatterFunction.
		 */
		else if (getDirection().equals(EdgeDirection.IN)) {
			outValue.f0 = next.getField(0);
		}
		 // When EdgeDirection is ALL, the edges iterator contains both in- and out- edges
		if (getDirection().equals(EdgeDirection.ALL)) {
			if (next.getField(0).equals(vertexId)) {
				// send msg to the trg
				outValue.f0 = next.getField(1);
			}
			else {
				// send msg to the src
				outValue.f0 = next.getField(0);
			}
		}
		out.collect(outValue);
	}
}
 
Example 18
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>());
	}
}
 
Example 19
Source File: ScatterFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Sends the given message to all vertices that are targets of an edge of the changed vertex.
 * This method is mutually exclusive to the method {@link #getEdges()} and may be called only once.
 *
 * <p>If the {@link EdgeDirection} is OUT (default), the message will be sent to out-neighbors.
 *
 * <p>If the {@link EdgeDirection} is IN, the message will be sent to in-neighbors.
 *
 * <p>If the {@link EdgeDirection} is ALL, the message will be sent to all neighbors.
 *
 * @param m The message to send.
 */
public void sendMessageToAllNeighbors(Message m) {
	if (edgesUsed) {
		throw new IllegalStateException("Can use either 'getEdges()' or 'sendMessageToAllNeighbors()'"
				+ "exactly once.");
	}

	edgesUsed = true;
	outValue.f1 = m;

	while (edges.hasNext()) {
		Tuple next = (Tuple) edges.next();

		/*
		 * When EdgeDirection is OUT, the edges iterator only has the out-edges
		 * of the vertex, i.e. the ones where this vertex is src.
		 * next.getField(1) gives the neighbor of the vertex running this ScatterFunction.
		 */
		if (getDirection().equals(EdgeDirection.OUT)) {
			outValue.f0 = next.getField(1);
		}
		/*
		 * When EdgeDirection is IN, the edges iterator only has the in-edges
		 * of the vertex, i.e. the ones where this vertex is trg.
		 * next.getField(10) gives the neighbor of the vertex running this ScatterFunction.
		 */
		else if (getDirection().equals(EdgeDirection.IN)) {
			outValue.f0 = next.getField(0);
		}
		 // When EdgeDirection is ALL, the edges iterator contains both in- and out- edges
		if (getDirection().equals(EdgeDirection.ALL)) {
			if (next.getField(0).equals(vertexId)) {
				// send msg to the trg
				outValue.f0 = next.getField(1);
			}
			else {
				// send msg to the src
				outValue.f0 = next.getField(0);
			}
		}
		out.collect(outValue);
	}
}
 
Example 20
Source File: DataSetUtilsITCase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testSummarize() throws Exception {
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	List<Tuple8<Short, Integer, Long, Float, Double, String, Boolean, DoubleValue>> data = new ArrayList<>();
	data.add(new Tuple8<>((short) 1, 1, 100L, 0.1f, 1.012376, "hello", false, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 2, 2, 1000L, 0.2f, 2.003453, "hello", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 4, 10, 10000L, 0.2f, 75.00005, "null", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 10, 4, 100L, 0.9f, 79.5, "", true, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 5, 5, 1000L, 0.2f, 10.0000001, "a", false, new DoubleValue(50.0)));
	data.add(new Tuple8<>((short) 6, 6, 10L, 0.1f, 0.0000000000023, "", true, new DoubleValue(100.0)));
	data.add(new Tuple8<>((short) 7, 7, 1L, 0.2f, Double.POSITIVE_INFINITY, "abcdefghijklmnop", true, new DoubleValue(100.0)));
	data.add(new Tuple8<>((short) 8, 8, -100L, 0.001f, Double.NaN, "abcdefghi", true, new DoubleValue(100.0)));

	Collections.shuffle(data);

	DataSet<Tuple8<Short, Integer, Long, Float, Double, String, Boolean, DoubleValue>> ds = env.fromCollection(data);

	// call method under test
	Tuple results = DataSetUtils.summarize(ds);

	Assert.assertEquals(8, results.getArity());

	NumericColumnSummary<Short> col0Summary = results.getField(0);
	Assert.assertEquals(8, col0Summary.getNonMissingCount());
	Assert.assertEquals(1, col0Summary.getMin().shortValue());
	Assert.assertEquals(10, col0Summary.getMax().shortValue());
	Assert.assertEquals(5.375, col0Summary.getMean().doubleValue(), 0.0);

	NumericColumnSummary<Integer> col1Summary = results.getField(1);
	Assert.assertEquals(1, col1Summary.getMin().intValue());
	Assert.assertEquals(10, col1Summary.getMax().intValue());
	Assert.assertEquals(5.375, col1Summary.getMean().doubleValue(), 0.0);

	NumericColumnSummary<Long> col2Summary = results.getField(2);
	Assert.assertEquals(-100L, col2Summary.getMin().longValue());
	Assert.assertEquals(10000L, col2Summary.getMax().longValue());

	NumericColumnSummary<Float> col3Summary = results.getField(3);
	Assert.assertEquals(8, col3Summary.getTotalCount());
	Assert.assertEquals(0.001000, col3Summary.getMin().doubleValue(), 0.0000001);
	Assert.assertEquals(0.89999999, col3Summary.getMax().doubleValue(), 0.0000001);
	Assert.assertEquals(0.2376249988883501, col3Summary.getMean().doubleValue(), 0.000000000001);
	Assert.assertEquals(0.0768965488108089, col3Summary.getVariance().doubleValue(), 0.00000001);
	Assert.assertEquals(0.27730226975415995, col3Summary.getStandardDeviation().doubleValue(), 0.000000000001);

	NumericColumnSummary<Double> col4Summary = results.getField(4);
	Assert.assertEquals(6, col4Summary.getNonMissingCount());
	Assert.assertEquals(2, col4Summary.getMissingCount());
	Assert.assertEquals(0.0000000000023, col4Summary.getMin().doubleValue(), 0.0);
	Assert.assertEquals(79.5, col4Summary.getMax().doubleValue(), 0.000000000001);

	StringColumnSummary col5Summary = results.getField(5);
	Assert.assertEquals(8, col5Summary.getTotalCount());
	Assert.assertEquals(0, col5Summary.getNullCount());
	Assert.assertEquals(8, col5Summary.getNonNullCount());
	Assert.assertEquals(2, col5Summary.getEmptyCount());
	Assert.assertEquals(0, col5Summary.getMinLength().intValue());
	Assert.assertEquals(16, col5Summary.getMaxLength().intValue());
	Assert.assertEquals(5.0, col5Summary.getMeanLength().doubleValue(), 0.0001);

	BooleanColumnSummary col6Summary = results.getField(6);
	Assert.assertEquals(8, col6Summary.getTotalCount());
	Assert.assertEquals(2, col6Summary.getFalseCount());
	Assert.assertEquals(6, col6Summary.getTrueCount());
	Assert.assertEquals(0, col6Summary.getNullCount());

	NumericColumnSummary<Double> col7Summary = results.getField(7);
	Assert.assertEquals(100.0, col7Summary.getMax().doubleValue(), 0.00001);
	Assert.assertEquals(50.0, col7Summary.getMin().doubleValue(), 0.00001);
}