Java Code Examples for org.apache.flink.api.java.tuple.Tuple4

The following examples show how to use org.apache.flink.api.java.tuple.Tuple4. These examples are extracted from open source projects. 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 Project: Flink-CEPplus   Source File: JavaTableEnvironmentITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testAsFromTupleToPojo() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	List<Tuple4<String, Integer, Double, String>> data = new ArrayList<>();
	data.add(new Tuple4<>("Rofl", 1, 1.0, "Hi"));
	data.add(new Tuple4<>("lol", 2, 1.0, "Hi"));
	data.add(new Tuple4<>("Test me", 4, 3.33, "Hello world"));

	Table table = tableEnv
		.fromDataSet(env.fromCollection(data), "q, w, e, r")
		.select("q as a, w as b, e as c, r as d");

	DataSet<SmallPojo2> ds = tableEnv.toDataSet(table, SmallPojo2.class);
	List<SmallPojo2> results = ds.collect();
	String expected = "Rofl,1,1.0,Hi\n" + "lol,2,1.0,Hi\n" + "Test me,4,3.33,Hello world\n";
	compareResultAsText(results, expected);
}
 
Example 2
Source Project: flink   Source File: SortPartitionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSortPartitionWithKeySelector1() {
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs = env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// should work
	try {
		tupleDs.sortPartition(new KeySelector<Tuple4<Integer, Long, CustomType, Long[]>, Integer>() {
			@Override
			public Integer getKey(Tuple4<Integer, Long, CustomType, Long[]> value) throws Exception {
				return value.f0;
			}
		}, Order.ASCENDING);
	} catch (Exception e) {
		Assert.fail();
	}
}
 
Example 3
Source Project: examples-java   Source File: CustomWindow.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void process(
        String id,
        Context ctx,
        Iterable<SensorReading> readings,
        Collector<Tuple4<String, Long, Long, Integer>> out) throws Exception {
    // count readings
    int cnt = 0;
    for (SensorReading r : readings) {
        cnt++;
    }
    // get current watermark
    long evalTime = ctx.currentWatermark();
    // emit result
    out.collect(Tuple4.of(id, ctx.window().getEnd(), evalTime, cnt));
}
 
Example 4
Source Project: flink   Source File: GroupingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGroupSortByKeyExpression3() {

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs =
			env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// should work
	try {
		tupleDs.groupBy("f0")
				.sortGroup("f2.myString", Order.ASCENDING)
				.sortGroup("f1", Order.DESCENDING);
	} catch (Exception e) {
		Assert.fail();
	}
}
 
Example 5
@SuppressWarnings("unchecked")
protected void testRightSideEmpty() throws Exception {
	CollectionIterator<Tuple2<String, String>> input1 = CollectionIterator.of(
			new Tuple2<>("Jack", "Engineering"),
			new Tuple2<>("Tim", "Sales"),
			new Tuple2<>("Zed", "HR")
	);
	CollectionIterator<Tuple2<String, Integer>> input2 = CollectionIterator.of();

	List<Tuple4<String, String, String, Object>> actualLeft = computeOuterJoin(input1, input2, OuterJoinType.LEFT);
	List<Tuple4<String, String, String, Object>> actualRight = computeOuterJoin(input1, input2, OuterJoinType.RIGHT);
	List<Tuple4<String, String, String, Object>> actualFull = computeOuterJoin(input1, input2, OuterJoinType.FULL);

	List<Tuple4<String, String, String, Object>> expected = Arrays.asList(
			new Tuple4<String, String, String, Object>("Jack", "Engineering", null, null),
			new Tuple4<String, String, String, Object>("Tim", "Sales", null, null),
			new Tuple4<String, String, String, Object>("Zed", "HR", null, null)
	);

	Assert.assertEquals(expected, actualLeft);
	Assert.assertEquals(expected, actualFull);
	Assert.assertEquals(Collections.<Tuple4<String,String,String,Object>>emptyList(), actualRight);
}
 
Example 6
Source Project: flink   Source File: RowComparator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Intermediate constructor for creating auxiliary fields.
 */
private RowComparator(
	int arity,
	int[] keyPositions,
	NullAwareComparator<Object>[] comparators,
	TypeSerializer<Object>[] serializers,
	Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {

	this(
		arity,
		keyPositions,
		comparators,
		serializers,
		auxiliaryFields.f0,
		auxiliaryFields.f1,
		auxiliaryFields.f2,
		auxiliaryFields.f3);
}
 
Example 7
@SuppressWarnings("unchecked")
protected void testLeftSideEmpty() throws Exception {
	CollectionIterator<Tuple2<String, String>> input1 = CollectionIterator.of();
	CollectionIterator<Tuple2<String, Integer>> input2 = CollectionIterator.of(
			new Tuple2<>("Allison", 100),
			new Tuple2<>("Jack", 200),
			new Tuple2<>("Zed", 150),
			new Tuple2<>("Zed", 250)
	);

	List<Tuple4<String, String, String, Object>> actualLeft = computeOuterJoin(input1, input2, OuterJoinType.LEFT);
	List<Tuple4<String, String, String, Object>> actualRight = computeOuterJoin(input1, input2, OuterJoinType.RIGHT);
	List<Tuple4<String, String, String, Object>> actualFull = computeOuterJoin(input1, input2, OuterJoinType.FULL);

	List<Tuple4<String, String, String, Object>> expected = Arrays.asList(
			new Tuple4<String, String, String, Object>(null, null, "Allison", 100),
			new Tuple4<String, String, String, Object>(null, null, "Jack", 200),
			new Tuple4<String, String, String, Object>(null, null, "Zed", 150),
			new Tuple4<String, String, String, Object>(null, null, "Zed", 250)
	);

	Assert.assertEquals(Collections.<Tuple4<String,String,String,Object>>emptyList(), actualLeft);
	Assert.assertEquals(expected, actualRight);
	Assert.assertEquals(expected, actualFull);
}
 
Example 8
Source Project: flink   Source File: GroupingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGroupSortByKeyExpression3() {

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs =
			env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// should work
	try {
		tupleDs.groupBy("f0")
				.sortGroup("f2.myString", Order.ASCENDING)
				.sortGroup("f1", Order.DESCENDING);
	} catch (Exception e) {
		Assert.fail();
	}
}
 
Example 9
Source Project: Flink-CEPplus   Source File: SortPartitionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSortPartitionWithKeySelector4() {
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs = env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// should work
	try {
		tupleDs.sortPartition(new KeySelector<Tuple4<Integer, Long, CustomType, Long[]>, Tuple2<Integer, Long>>() {
			@Override
			public Tuple2<Integer, Long> getKey(Tuple4<Integer, Long, CustomType, Long[]> value) throws Exception {
				return new Tuple2<>(value.f0, value.f1);
			}
		}, Order.ASCENDING);
	} catch (Exception e) {
		Assert.fail();
	}
}
 
Example 10
Source Project: Alink   Source File: ChiSquareTestTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testChiSquare() {

    Crosstab crossTable = new Crosstab();

    crossTable.data = new long[][]{
        {4L, 1L, 3L},
        {2L, 4L, 5L},
        {3L, 4L, 4L}
    };

    Tuple4 tuple4 = ChiSquareTest.test(Tuple2.of(0, crossTable));

    assertEquals(0, tuple4.f0);
    assertEquals(1.0, (double) tuple4.f1, 10e-4);
    assertEquals(0.0, (double) tuple4.f2, 10e-4);

}
 
Example 11
Source Project: Flink-CEPplus   Source File: RowComparator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Intermediate constructor for creating auxiliary fields.
 */
private RowComparator(
	int arity,
	int[] keyPositions,
	NullAwareComparator<Object>[] comparators,
	TypeSerializer<Object>[] serializers,
	Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {

	this(
		arity,
		keyPositions,
		comparators,
		serializers,
		auxiliaryFields.f0,
		auxiliaryFields.f1,
		auxiliaryFields.f2,
		auxiliaryFields.f3);
}
 
Example 12
@SuppressWarnings("unchecked, rawtypes")
private List<Tuple4<String, String, String, Object>> computeOuterJoin(ResettableMutableObjectIterator<Tuple2<String, String>> input1,
																	  ResettableMutableObjectIterator<Tuple2<String, Integer>> input2,
																	  OuterJoinType outerJoinType) throws Exception {
	input1.reset();
	input2.reset();
	AbstractMergeOuterJoinIterator iterator =
			createOuterJoinIterator(
					outerJoinType, input1, input2, serializer1, comparator1, serializer2, comparator2,
					pairComp, this.memoryManager, this.ioManager, PAGES_FOR_BNLJN, this.parentTask
			);

	List<Tuple4<String, String, String, Object>> actual = new ArrayList<>();
	ListCollector<Tuple4<String, String, String, Object>> collector = new ListCollector<>(actual);
	while (iterator.callWithNextKey(new SimpleTupleJoinFunction(), collector)) ;
	iterator.close();

	return actual;
}
 
Example 13
Source Project: flink   Source File: AdamicAdar.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void reduce(Iterable<Tuple3<T, T, FloatValue>> values, Collector<Tuple4<IntValue, T, T, FloatValue>> out)
		throws Exception {
	int groupCount = 0;
	int groupSpans = 1;

	groupSpansValue.setValue(groupSpans);

	for (Tuple3<T, T, FloatValue> edge : values) {
		output.f1 = edge.f0;
		output.f2 = edge.f1;
		output.f3 = edge.f2;

		out.collect(output);

		if (++groupCount == GROUP_SIZE) {
			groupCount = 0;
			groupSpansValue.setValue(++groupSpans);
		}
	}
}
 
Example 14
@Override
public void reduce(Iterable <Tuple4 <Long, Row, Row, Double>> iterable, Collector <Row> collector)
	throws Exception {
	Object id = null;
	List<Tuple2<Double, Row>> list = new ArrayList<>();

	for (Tuple4 <Long, Row, Row, Double> t : iterable) {
		if (null == id) {
			id = t.f1.getField(0);
		}
		list.add(Tuple2.of(t.f3, t.f2));
	}
	list.sort(new Comparator<Tuple2<Double, Row>>() {
		@Override
		public int compare(Tuple2<Double, Row> o1, Tuple2<Double, Row> o2) {
			return o1.f0.compareTo(o2.f0);
		}
	});
	long rank = 1L;
	for(int i = 0; i < Math.min(list.size(), topN); i++){
	    Tuple2<Double, Row> tuple = list.get(i);
              Row row = Row.of(id, tuple.f1.getField(0), tuple.f0, rank++);
              collector.collect(row);
          }
}
 
Example 15
Source Project: flink   Source File: StreamSinkOperatorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(
	T value, Context context) throws Exception {
	Long timestamp = context.timestamp();
	if (timestamp != null) {
		data.add(
			new Tuple4<>(
				context.currentWatermark(),
				context.currentProcessingTime(),
				context.timestamp(),
				value));
	} else {
		data.add(
			new Tuple4<>(
				context.currentWatermark(),
				context.currentProcessingTime(),
				null,
				value));

	}
}
 
Example 16
@SuppressWarnings("unchecked, rawtypes")
private List<Tuple4<String, String, String, Object>> computeOuterJoin(ResettableMutableObjectIterator<Tuple2<String, String>> input1,
																	  ResettableMutableObjectIterator<Tuple2<String, Integer>> input2,
																	  OuterJoinType outerJoinType) throws Exception {
	input1.reset();
	input2.reset();
	AbstractMergeOuterJoinIterator iterator =
			createOuterJoinIterator(
					outerJoinType, input1, input2, serializer1, comparator1, serializer2, comparator2,
					pairComp, this.memoryManager, this.ioManager, PAGES_FOR_BNLJN, this.parentTask
			);

	List<Tuple4<String, String, String, Object>> actual = new ArrayList<>();
	ListCollector<Tuple4<String, String, String, Object>> collector = new ListCollector<>(actual);
	while (iterator.callWithNextKey(new SimpleTupleJoinFunction(), collector)) ;
	iterator.close();

	return actual;
}
 
Example 17
Source Project: flink   Source File: EventTimeWindowCheckpointingITCase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void updateCount(Tuple4<Long, Long, Long, IntType> value, Map<Long, Integer> windowCounts) {

	windowCounts.merge(value.f0, 1, (a, b) -> a + b);

	// verify the contents of that window, the contents should be:
	// (key + num windows so far)
	assertEquals("Window counts don't match for key " + value.f0 + ".", value.f0.intValue() + windowCounts.get(value.f0), value.f3.value);
}
 
Example 18
Source Project: flink   Source File: SortPartitionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSortPartitionExpressionKeys2() {

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs = env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// should work
	try {
		tupleDs
				.sortPartition("f0", Order.ASCENDING)
				.sortPartition("f2.nested.myInt", Order.DESCENDING);
	} catch (Exception e) {
		Assert.fail();
	}
}
 
Example 19
Source Project: Flink-CEPplus   Source File: GroupingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = InvalidProgramException.class)
public void testGroupSortKeyFields4() {

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs =
			env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// should not work
	tupleDs.groupBy(0)
			.sortGroup(2, Order.ASCENDING);
}
 
Example 20
Source Project: Flink-CEPplus   Source File: GroupingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGroupSortByKeyExpression1() {

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs =
			env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// should work
	try {
		tupleDs.groupBy("f0").sortGroup("f1", Order.ASCENDING);
	} catch (Exception e) {
		Assert.fail();
	}
}
 
Example 21
Source Project: flink   Source File: AdamicAdar.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void reduce(Iterable<Tuple4<IntValue, T, T, FloatValue>> values, Collector<Tuple3<T, T, FloatValue>> out)
		throws Exception {
	int visitedCount = 0;

	for (Tuple4<IntValue, T, T, FloatValue> edge : values) {
		output.f1 = edge.f2;
		output.f2 = edge.f3;

		for (int i = 0; i < visitedCount; i++) {
			output.f0 = visited.get(i);
			out.collect(output);
		}

		if (visitedCount < GROUP_SIZE) {
			if (!initialized) {
				initialized = true;

				for (int i = 0; i < GROUP_SIZE; i++) {
					visited.add(edge.f2.copy());
				}
			} else {
				edge.f2.copyTo(visited.get(visitedCount));
			}

			visitedCount += 1;
		}
	}
}
 
Example 22
Source Project: bahir-flink   Source File: RandomTupleSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void run(SourceContext<Tuple4<Integer, String, Double, Long>> ctx) throws Exception {
    while (isRunning) {
        long timestamp = initialTimestamp + 1000 * number.get();
        ctx.collectWithTimestamp(Tuple4.of(number.get(), "test_tuple", random.nextDouble(), timestamp), timestamp);
        if (number.incrementAndGet() >= this.count) {
            cancel();
        }
    }
}
 
Example 23
Source Project: flink   Source File: TriangleListing.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Result<T> join(Tuple4<T, T, T, ByteValue> triplet, Tuple3<T, T, ByteValue> edge)
		throws Exception {
	output.setVertexId0(triplet.f0);
	output.setVertexId1(triplet.f1);
	output.setVertexId2(triplet.f2);
	output.setBitmask((byte) (triplet.f3.getValue() | edge.f2.getValue()));
	return output;
}
 
Example 24
Source Project: Alink   Source File: LinkedData.java    License: Apache License 2.0 5 votes vote down vote up
public Tuple4<Float, Double, Double, Float> getData() {
    int currentIndex = this.iteratorArray.getPoint() * 24;
    return Tuple4.of(buffer.getFloat(currentIndex),
            buffer.getDouble(currentIndex + 4),
            buffer.getDouble(currentIndex + 12),
            buffer.getFloat(currentIndex + 20));
}
 
Example 25
Source Project: bahir-flink   Source File: StreamSchemaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamSchemaWithTuple() {
    TypeInformation<Tuple4<Integer,Long,String,Double>> typeInfo = Types.TUPLE(Types.INT, Types.LONG, Types.STRING, Types.DOUBLE);
    StreamSchema<Tuple4<Integer,Long,String,Double>> schema = new StreamSchema<>(typeInfo, "id", "timestamp", "name", "price");
    assertEquals(Tuple4.class, schema.getTypeInfo().getTypeClass());
    assertEquals(4, schema.getFieldIndexes().length);
    assertEquals(Tuple4.class, schema.getTypeInfo().getTypeClass());
}
 
Example 26
Source Project: Flink-CEPplus   Source File: SortPartitionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = InvalidProgramException.class)
public void testSortPartitionWithPositionKeys4() {

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs = env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// must not work
	tupleDs.sortPartition(3, Order.ASCENDING);
}
 
Example 27
Source Project: Flink-CEPplus   Source File: SortPartitionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = InvalidProgramException.class)
public void testSortPartitionWithExpressionKeys3() {

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs = env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);

	// must not work
	tupleDs.sortPartition("f2.nested", Order.ASCENDING);
}
 
Example 28
Source Project: flink   Source File: EdgeMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void reduce(Iterable<Tuple4<T, T, Degrees, LongValue>> values, Collector<Tuple3<T, Degrees, LongValue>> out)
		throws Exception {
	Tuple4<T, T, Degrees, LongValue> value = values.iterator().next();

	output.f0 = value.f0;
	output.f1 = value.f2;
	output.f2 = value.f3;

	out.collect(output);
}
 
Example 29
Source Project: flink   Source File: EdgeMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void reduce(Iterable<Tuple4<T, T, Degrees, LongValue>> values, Collector<Tuple3<T, Degrees, LongValue>> out)
		throws Exception {
	Tuple4<T, T, Degrees, LongValue> value = values.iterator().next();

	output.f0 = value.f0;
	output.f1 = value.f2;
	output.f2 = value.f3;

	out.collect(output);
}
 
Example 30
Source Project: Flink-CEPplus   Source File: Graph.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void join(Vertex<K, VV> vertex, Edge<K, EV> edge, Collector<Tuple4<K, K, VV, EV>> collector)
		throws Exception {

	collector.collect(new Tuple4<>(edge.getSource(), edge.getTarget(), vertex.getValue(),
			edge.getValue()));
}