org.apache.flink.api.common.typeinfo.BasicTypeInfo Java Examples

The following examples show how to use org.apache.flink.api.common.typeinfo.BasicTypeInfo. 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: RowTypeInfoTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSchemaEquals() {
	final RowTypeInfo row1 = new RowTypeInfo(
		new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO},
		new String[] {"field1", "field2"});
	final RowTypeInfo row2 = new RowTypeInfo(
		new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO},
		new String[] {"field1", "field2"});
	assertTrue(row1.schemaEquals(row2));

	final RowTypeInfo other1 = new RowTypeInfo(
		new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO},
		new String[] {"otherField", "field2"});
	final RowTypeInfo other2 = new RowTypeInfo(
		new TypeInformation[]{BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO},
		new String[] {"field1", "field2"});
	assertFalse(row1.schemaEquals(other1));
	assertFalse(row1.schemaEquals(other2));
}
 
Example #2
Source File: StreamSourceOperatorWatermarksTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoMaxWatermarkOnImmediateCancel() throws Exception {
	StreamSource<String, ?> sourceOperator = new StreamSource<>(new InfiniteSource<>());
	StreamTaskTestHarness<String> testHarness = setupSourceStreamTask(
		sourceOperator, BasicTypeInfo.STRING_TYPE_INFO, true);

	testHarness.invoke();
	try {
		testHarness.waitForTaskCompletion();
		fail("should throw an exception");
	} catch (Throwable t) {
		if (!ExceptionUtils.findThrowable(t, CancelTaskException.class).isPresent()) {
			throw t;
		}
	}
	assertTrue(testHarness.getOutput().isEmpty());
}
 
Example #3
Source File: StreamTaskTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that checkpoints are declined if operators are (partially) closed.
 *
 * <p>See FLINK-16383.
 */
@Test
public void testCheckpointDeclinedOnClosedOperator() throws Throwable {
	ClosingOperator operator = new ClosingOperator();
	MultipleInputStreamTaskTestHarnessBuilder<Integer> builder =
		new MultipleInputStreamTaskTestHarnessBuilder<>(OneInputStreamTask::new, BasicTypeInfo.INT_TYPE_INFO)
				.addInput(BasicTypeInfo.INT_TYPE_INFO);
	StreamTaskMailboxTestHarness<Integer> harness = builder
		.setupOutputForSingletonOperatorChain(operator)
		.build();
	// keeps the mailbox from suspending
	harness.setAutoProcess(false);
	harness.processElement(new StreamRecord<>(1));

	harness.streamTask.operatorChain.closeOperators(harness.streamTask.getActionExecutor());
	assertEquals(true, operator.closed.get());

	harness.streamTask.triggerCheckpointOnBarrier(new CheckpointMetaData(1, 0), CheckpointOptions.forCheckpointWithDefaultLocation(), new CheckpointMetrics());
	assertEquals(1, harness.getCheckpointResponder().getDeclineReports().size());
}
 
Example #4
Source File: LegacyKeyedProcessOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNullOutputTagRefusal() throws Exception {
	LegacyKeyedProcessOperator<Integer, Integer, String> operator =
		new LegacyKeyedProcessOperator<>(new NullOutputTagEmittingProcessFunction());

	OneInputStreamOperatorTestHarness<Integer, String> testHarness =
		new KeyedOneInputStreamOperatorTestHarness<>(
			operator, new IdentityKeySelector<>(), BasicTypeInfo.INT_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	testHarness.setProcessingTime(17);
	try {
		expectedException.expect(IllegalArgumentException.class);
		testHarness.processElement(new StreamRecord<>(5));
	} finally {
		testHarness.close();
	}
}
 
Example #5
Source File: StreamTaskTimerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private StreamTaskTestHarness<?> startTestHarness() throws Exception {
	final OneInputStreamTaskTestHarness<String, String> testHarness = new OneInputStreamTaskTestHarness<>(
			OneInputStreamTask::new,
			BasicTypeInfo.STRING_TYPE_INFO,
			BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setupOutputForSingletonOperatorChain();

	StreamConfig streamConfig = testHarness.getStreamConfig();
	streamConfig.setChainIndex(0);
	streamConfig.setStreamOperator(new StreamMap<String, String>(new DummyMapFunction<>()));

	testHarness.invoke();
	testHarness.waitForTaskRunning();

	return testHarness;
}
 
Example #6
Source File: TypeHintITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testCombineGroupWithTypeInformationTypeHint() throws Exception {
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	env.getConfig().disableSysoutLogging();

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.getSmall3TupleDataSet(env);
	DataSet<Integer> resultDs = ds
		.groupBy(0)
		.combineGroup(new GroupCombiner<Tuple3<Integer, Long, String>, Integer>())
		.returns(BasicTypeInfo.INT_TYPE_INFO);
	List<Integer> result = resultDs.collect();

	String expectedResult = "2\n" +
		"3\n" +
		"1\n";

	compareResultAsText(result, expectedResult);
}
 
Example #7
Source File: RowTypeInfoTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetFlatFields() {
	RowTypeInfo typeInfo1 = new RowTypeInfo(typeList, new String[]{"int", "row", "string"});
	List<FlatFieldDescriptor> result = new ArrayList<>();
	typeInfo1.getFlatFields("row.*", 0, result);
	assertEquals(2, result.size());
	assertEquals(
		new FlatFieldDescriptor(1, BasicTypeInfo.SHORT_TYPE_INFO).toString(),
		result.get(0).toString());
	assertEquals(
		new FlatFieldDescriptor(2, BasicTypeInfo.BIG_DEC_TYPE_INFO).toString(),
		result.get(1).toString());

	result.clear();
	typeInfo1.getFlatFields("string", 0, result);
	assertEquals(1, result.size());
	assertEquals(
		new FlatFieldDescriptor(3, BasicTypeInfo.STRING_TYPE_INFO).toString(),
		result.get(0).toString());
}
 
Example #8
Source File: MultipleInputStreamTaskTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testLatencyMarker() throws Exception {
	final Map<String, Metric> metrics = new ConcurrentHashMap<>();
	final TaskMetricGroup taskMetricGroup = new StreamTaskTestHarness.TestTaskMetricGroup(metrics);

	try (StreamTaskMailboxTestHarness<String> testHarness =
			new MultipleInputStreamTaskTestHarnessBuilder<>(MultipleInputStreamTask::new, BasicTypeInfo.STRING_TYPE_INFO)
				.addInput(BasicTypeInfo.STRING_TYPE_INFO)
				.addInput(BasicTypeInfo.INT_TYPE_INFO)
				.addInput(BasicTypeInfo.DOUBLE_TYPE_INFO)
				.setupOutputForSingletonOperatorChain(new MapToStringMultipleInputOperatorFactory())
				.setTaskMetricGroup(taskMetricGroup)
				.build()) {
		ArrayDeque<Object> expectedOutput = new ArrayDeque<>();

		OperatorID sourceId = new OperatorID();
		LatencyMarker latencyMarker = new LatencyMarker(42L, sourceId, 0);
		testHarness.processElement(latencyMarker);
		expectedOutput.add(latencyMarker);

		assertThat(testHarness.getOutput(), contains(expectedOutput.toArray()));

		testHarness.endInput();
		testHarness.waitForTaskCompletion();
	}
}
 
Example #9
Source File: TypeHintITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsortedGroupReduceWithTypeInformationTypeHint() throws Exception {
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.getSmall3TupleDataSet(env);
	DataSet<Integer> resultDs = ds
		.groupBy(0)
		.reduceGroup(new GroupReducer<Tuple3<Integer, Long, String>, Integer>())
		.returns(BasicTypeInfo.INT_TYPE_INFO);
	List<Integer> result = resultDs.collect();

	String expectedResult = "2\n" +
		"3\n" +
		"1\n";

	compareResultAsText(result, expectedResult);
}
 
Example #10
Source File: RowSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testRowSerializer() {
	TypeInformation<Row> typeInfo = new RowTypeInfo(
		BasicTypeInfo.INT_TYPE_INFO,
		BasicTypeInfo.STRING_TYPE_INFO);
	Row row1 = new Row(2);
	row1.setKind(RowKind.UPDATE_BEFORE);
	row1.setField(0, 1);
	row1.setField(1, "a");

	Row row2 = new Row(2);
	row2.setKind(RowKind.INSERT);
	row2.setField(0, 2);
	row2.setField(1, null);

	TypeSerializer<Row> serializer = typeInfo.createSerializer(new ExecutionConfig());
	RowSerializerTestInstance instance = new RowSerializerTestInstance(serializer, row1, row2);
	instance.testAll();
}
 
Example #11
Source File: PojoTypeExtractionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericPojoTypeInference1() {
	MyMapper<String> function = new MyMapper<>();

	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(
			function,
			TypeInformation.of(new TypeHint<PojoWithGenerics<Long, String>>(){}));
	
	Assert.assertTrue(ti instanceof PojoTypeInfo<?>);
	PojoTypeInfo<?> pti = (PojoTypeInfo<?>) ti;
	for(int i = 0; i < pti.getArity(); i++) {
		PojoField field = pti.getPojoFieldAt(i);
		String name = field.getField().getName();
		if(name.equals("field1")) {
			Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, field.getTypeInformation());
		} else if (name.equals("field2")) {
			Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, field.getTypeInformation());
		} else if (name.equals("key")) {
			Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, field.getTypeInformation());
		} else {
			Assert.fail("Unexpected field "+field);
		}
	}
}
 
Example #12
Source File: ExpressionKeysTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testStandardTupleKeys() {
	TupleTypeInfo<Tuple7<String, String, String, String, String, String, String>> typeInfo = new TupleTypeInfo<>(
			BasicTypeInfo.STRING_TYPE_INFO,BasicTypeInfo.STRING_TYPE_INFO,BasicTypeInfo.STRING_TYPE_INFO,BasicTypeInfo.STRING_TYPE_INFO,BasicTypeInfo.STRING_TYPE_INFO,
			BasicTypeInfo.STRING_TYPE_INFO,BasicTypeInfo.STRING_TYPE_INFO);
	
	ExpressionKeys<Tuple7<String, String, String, String, String, String, String>> ek;
	
	for( int i = 1; i < 8; i++) {
		int[] ints = new int[i];
		for( int j = 0; j < i; j++) {
			ints[j] = j;
		}
		int[] inInts = Arrays.copyOf(ints, ints.length); // copy, just to make sure that the code is not cheating by changing the ints.
		ek = new ExpressionKeys<>(inInts, typeInfo);
		Assert.assertArrayEquals(ints, ek.computeLogicalKeyPositions());
		Assert.assertEquals(ints.length, ek.computeLogicalKeyPositions().length);
		
		ArrayUtils.reverse(ints);
		inInts = Arrays.copyOf(ints, ints.length);
		ek = new ExpressionKeys<>(inInts, typeInfo);
		Assert.assertArrayEquals(ints, ek.computeLogicalKeyPositions());
		Assert.assertEquals(ints.length, ek.computeLogicalKeyPositions().length);
	}
}
 
Example #13
Source File: ImmutableMapStateTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	if (!mapStateDesc.isSerializerInitialized()) {
		mapStateDesc.initializeSerializerUnlessSet(new ExecutionConfig());
	}

	Map<Long, Long> initMap = new HashMap<>();
	initMap.put(1L, 5L);
	initMap.put(2L, 5L);

	byte[] initSer = KvStateSerializer.serializeMap(
			initMap.entrySet(),
			BasicTypeInfo.LONG_TYPE_INFO.createSerializer(new ExecutionConfig()),
			BasicTypeInfo.LONG_TYPE_INFO.createSerializer(new ExecutionConfig()));

	mapState = ImmutableMapState.createState(mapStateDesc, initSer);
}
 
Example #14
Source File: SelectorFunctionKeysTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAreCompatible4() throws Keys.IncompatibleKeysException {
	TypeInformation<Tuple3<String, Long, Integer>> t1 = new TupleTypeInfo<>(
		BasicTypeInfo.STRING_TYPE_INFO,
		BasicTypeInfo.LONG_TYPE_INFO,
		BasicTypeInfo.INT_TYPE_INFO
	);
	TypeInformation<PojoWithMultiplePojos> t2 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);

	Keys.ExpressionKeys<Tuple3<String, Long, Integer>> ek1 = new Keys.ExpressionKeys<>(new int[]{2,0}, t1);
	Keys<PojoWithMultiplePojos> sk2 = new Keys.SelectorFunctionKeys<>(
		new KeySelector3(),
		t2,
		new TupleTypeInfo<Tuple2<Integer, String>>(BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO)
	);

	Assert.assertTrue(sk2.areCompatible(ek1));
}
 
Example #15
Source File: PojoTypeExtractionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Test if the TypeExtractor is accepting untyped generics,
 * making them GenericTypes
 */
@Test
public void testPojoWithGenericsSomeFieldsGeneric() {
	TypeInformation<?> typeForClass = TypeExtractor.createTypeInfo(PojoWithGenerics.class);
	Assert.assertTrue(typeForClass instanceof PojoTypeInfo<?>);
	PojoTypeInfo<?> pojoTypeForClass = (PojoTypeInfo<?>) typeForClass;
	for(int i = 0; i < pojoTypeForClass.getArity(); i++) {
		PojoField field = pojoTypeForClass.getPojoFieldAt(i);
		String name = field.getField().getName();
		if(name.equals("field1")) {
			Assert.assertEquals(new GenericTypeInfo<Object>(Object.class), field.getTypeInformation());
		} else if (name.equals("field2")) {
			Assert.assertEquals(new GenericTypeInfo<Object>(Object.class), field.getTypeInformation());
		} else if (name.equals("key")) {
			Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, field.getTypeInformation());
		} else {
			Assert.fail("Unexpected field "+field);
		}
	}
}
 
Example #16
Source File: WindowTranslationTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testReduceProcessingTime() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);

	DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));

	DataStream<Tuple2<String, Integer>> window1 = source
			.keyBy(new TupleKeySelector())
			.window(SlidingProcessingTimeWindows.of(Time.of(1, TimeUnit.SECONDS), Time.of(100, TimeUnit.MILLISECONDS)))
			.reduce(new DummyReducer());

	OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window1.getTransformation();
	OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator = transform.getOperator();
	Assert.assertTrue(operator instanceof WindowOperator);
	WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
	Assert.assertTrue(winOperator.getTrigger() instanceof ProcessingTimeTrigger);
	Assert.assertTrue(winOperator.getWindowAssigner() instanceof SlidingProcessingTimeWindows);
	Assert.assertTrue(winOperator.getStateDescriptor() instanceof ReducingStateDescriptor);

	processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
 
Example #17
Source File: SourceStreamTaskTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * This test verifies that open() and close() are correctly called by the StreamTask.
 */
@Test
@SuppressWarnings("unchecked")
public void testOpenClose() throws Exception {
	final StreamTaskTestHarness<String> testHarness = new StreamTaskTestHarness<>(
			SourceStreamTask::new, BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setupOutputForSingletonOperatorChain();

	StreamConfig streamConfig = testHarness.getStreamConfig();
	StreamSource<String, ?> sourceOperator = new StreamSource<>(new OpenCloseTestSource());
	streamConfig.setStreamOperator(sourceOperator);
	streamConfig.setOperatorID(new OperatorID());

	testHarness.invoke();
	testHarness.waitForTaskCompletion();

	assertTrue("RichFunction methods where not called.", OpenCloseTestSource.closeCalled);

	List<String> resultElements = TestHarnessUtil.getRawElementsFromOutput(testHarness.getOutput());
	Assert.assertEquals(10, resultElements.size());
}
 
Example #18
Source File: SelectorFunctionKeysTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testAreCompatible2() throws Keys.IncompatibleKeysException {
	TypeInformation<PojoWithMultiplePojos> t1 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);
	TypeInformation<Tuple3<Long, Pojo1, Integer>> t2 = new TupleTypeInfo<>(
		BasicTypeInfo.LONG_TYPE_INFO,
		TypeExtractor.getForClass(Pojo1.class),
		BasicTypeInfo.INT_TYPE_INFO);
	TypeInformation<Tuple2<Integer, String>> kt = new TupleTypeInfo<>(
		BasicTypeInfo.INT_TYPE_INFO,
		BasicTypeInfo.STRING_TYPE_INFO
	);

	Keys<PojoWithMultiplePojos> k1 = new Keys.SelectorFunctionKeys<>(
		new KeySelector3(),
		t1,
		kt
	);
	Keys<Tuple3<Long, Pojo1, Integer>> k2 = new Keys.SelectorFunctionKeys<>(
		new KeySelector4(),
		t2,
		kt
	);

	Assert.assertTrue(k1.areCompatible(k2));
	Assert.assertTrue(k2.areCompatible(k1));
}
 
Example #19
Source File: FromElementsFunctionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrings() {
	try {
		String[] data = { "Oh", "boy", "what", "a", "show", "!"};

		FromElementsFunction<String> source = new FromElementsFunction<String>(
				BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), data);

		List<String> result = new ArrayList<String>();
		source.run(new ListSourceContext<String>(result));

		assertEquals(Arrays.asList(data), result);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #20
Source File: CollectionDataSets.java    From flink with Apache License 2.0 6 votes vote down vote up
public static DataSet<Tuple5<Integer, Long, Integer, String, Long>> getSmall5TupleDataSet(ExecutionEnvironment env) {

		List<Tuple5<Integer, Long, Integer, String, Long>> data = new ArrayList<>();
		data.add(new Tuple5<>(1, 1L, 0, "Hallo", 1L));
		data.add(new Tuple5<>(2, 2L, 1, "Hallo Welt", 2L));
		data.add(new Tuple5<>(2, 3L, 2, "Hallo Welt wie", 1L));

		Collections.shuffle(data);

		TupleTypeInfo<Tuple5<Integer, Long, Integer, String, Long>> type = new TupleTypeInfo<>(
				BasicTypeInfo.INT_TYPE_INFO,
				BasicTypeInfo.LONG_TYPE_INFO,
				BasicTypeInfo.INT_TYPE_INFO,
				BasicTypeInfo.STRING_TYPE_INFO,
				BasicTypeInfo.LONG_TYPE_INFO
		);

		return env.fromCollection(data, type);
	}
 
Example #21
Source File: TypeExtractorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSameGenericVariable() {
	RichMapFunction<?, ?> function = new RichMapFunction<SameTypeVariable<String>, SameTypeVariable<String>>() {
		private static final long serialVersionUID = 1L;

		@Override
		public SameTypeVariable<String> map(SameTypeVariable<String> value) throws Exception {
			return null;
		}
	};

	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<String, String>>(){}));

	Assert.assertTrue(ti.isTupleType());
	Assert.assertEquals(2, ti.getArity());
	
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;
	Assert.assertEquals(SameTypeVariable.class, tti.getTypeClass());
	
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0));
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(1));
}
 
Example #22
Source File: EitherSerializerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testEitherWithTuple() {

Either<Tuple2<Long, Long>, Double>[] testData = new Either[] {
		Either.Left(new Tuple2<>(2L, 9L)),
		new Left<>(new Tuple2<>(Long.MIN_VALUE, Long.MAX_VALUE)),
		new Right<>(32.0),
		Right(Double.MIN_VALUE),
		Right(Double.MAX_VALUE)};

EitherTypeInfo<Tuple2<Long, Long>, Double> eitherTypeInfo = (EitherTypeInfo<Tuple2<Long, Long>, Double>)
		new EitherTypeInfo<Tuple2<Long, Long>, Double>(
		new TupleTypeInfo<Tuple2<Long, Long>>(BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO),
		BasicTypeInfo.DOUBLE_TYPE_INFO);
EitherSerializer<Tuple2<Long, Long>, Double> eitherSerializer =
		(EitherSerializer<Tuple2<Long, Long>, Double>) eitherTypeInfo.createSerializer(new ExecutionConfig());
SerializerTestInstance<Either<Tuple2<Long, Long>, Double>> testInstance =
		new EitherSerializerTestInstance<Either<Tuple2<Long, Long>, Double>>(
				eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
testInstance.testAll();
}
 
Example #23
Source File: AllWindowTranslationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testApplyProcessingTimeTime() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);

	DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));

	DataStream<Tuple2<String, Integer>> window1 = source
			.windowAll(TumblingProcessingTimeWindows.of(Time.of(1, TimeUnit.SECONDS)))
			.apply(new AllWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, TimeWindow>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void apply(
						TimeWindow window,
						Iterable<Tuple2<String, Integer>> values,
						Collector<Tuple2<String, Integer>> out) throws Exception {
					for (Tuple2<String, Integer> in : values) {
						out.collect(in);
					}
				}
			});

	OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window1.getTransformation();
	OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator = transform.getOperator();
	Assert.assertTrue(operator instanceof WindowOperator);
	WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
	Assert.assertTrue(winOperator.getTrigger() instanceof ProcessingTimeTrigger);
	Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingProcessingTimeWindows);
	Assert.assertTrue(winOperator.getStateDescriptor() instanceof ListStateDescriptor);

	processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));

}
 
Example #24
Source File: TypeExtractorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicArray2() {
	RichMapFunction<Boolean[], ?> function = new IdentityMapper<Boolean[]>();

	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicArrayTypeInfo.BOOLEAN_ARRAY_TYPE_INFO);

	Assert.assertTrue(ti instanceof BasicArrayTypeInfo<?, ?>);
	BasicArrayTypeInfo<?, ?> bati = (BasicArrayTypeInfo<?, ?>) ti;
	Assert.assertTrue(bati.getComponentInfo().isBasicType());
	Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, bati.getComponentInfo());
}
 
Example #25
Source File: RowTypeInfoTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTypeAt() {
	RowTypeInfo typeInfo = new RowTypeInfo(typeList);


	assertArrayEquals(new String[]{"f0", "f1", "f2"}, typeInfo.getFieldNames());

	assertEquals(BasicTypeInfo.STRING_TYPE_INFO, typeInfo.getTypeAt("f2"));
	assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, typeInfo.getTypeAt("f1.f0"));
	assertEquals(BasicTypeInfo.BIG_DEC_TYPE_INFO, typeInfo.getTypeAt("f1.1"));
}
 
Example #26
Source File: LambdaExtractionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapPartitionLambda() {
	MapPartitionFunction<Tuple2<Tuple1<Integer>, Boolean>, Tuple2<Tuple1<Integer>, String>> f = (i, o) -> {};

	TypeInformation<?> ti = TypeExtractor.getMapPartitionReturnTypes(f, NESTED_TUPLE_BOOLEAN_TYPE, null, true);
	if (!(ti instanceof MissingTypeInfo)) {
		assertTrue(ti.isTupleType());
		assertEquals(2, ti.getArity());
		assertTrue(((TupleTypeInfo<?>) ti).getTypeAt(0).isTupleType());
		assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO);
	}
}
 
Example #27
Source File: TypeExtractorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testBasicArray() {
	// use getCoGroupReturnTypes()
	RichCoGroupFunction<?, ?, ?> function = new RichCoGroupFunction<String[], String[], String[]>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void coGroup(Iterable<String[]> first, Iterable<String[]> second, Collector<String[]> out) throws Exception {
			// nothing to do
		}
	};

	TypeInformation<?> ti = TypeExtractor.getCoGroupReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<String[]>(){}), (TypeInformation) TypeInformation.of(new TypeHint<String[]>(){}));

	Assert.assertFalse(ti.isBasicType());
	Assert.assertFalse(ti.isTupleType());
	
	// Due to a Java 6 bug the classification can be slightly wrong
	Assert.assertTrue(ti instanceof BasicArrayTypeInfo<?,?> || ti instanceof ObjectArrayTypeInfo<?,?>);
	
	if(ti instanceof BasicArrayTypeInfo<?,?>) {
		Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, ti);
	}
	else {
		Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((ObjectArrayTypeInfo<?,?>) ti).getComponentInfo());
	}		
}
 
Example #28
Source File: WindowTranslationTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testProcessWithCustomTrigger() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);

	DataStream<Tuple2<String, Integer>> source = env.fromElements(Tuple2.of("hello", 1), Tuple2.of("hello", 2));

	DataStream<Tuple2<String, Integer>> window1 = source
			.keyBy(new TupleKeySelector())
			.window(TumblingEventTimeWindows.of(Time.of(1, TimeUnit.SECONDS)))
			.trigger(CountTrigger.of(1))
			.process(new ProcessWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void process(String key,
						Context ctx,
						Iterable<Tuple2<String, Integer>> values,
						Collector<Tuple2<String, Integer>> out) throws Exception {
					for (Tuple2<String, Integer> in : values) {
						out.collect(in);
					}
				}
			});

	OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>> transform = (OneInputTransformation<Tuple2<String, Integer>, Tuple2<String, Integer>>) window1.getTransformation();
	OneInputStreamOperator<Tuple2<String, Integer>, Tuple2<String, Integer>> operator = transform.getOperator();
	Assert.assertTrue(operator instanceof WindowOperator);
	WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?> winOperator = (WindowOperator<String, Tuple2<String, Integer>, ?, ?, ?>) operator;
	Assert.assertTrue(winOperator.getTrigger() instanceof CountTrigger);
	Assert.assertTrue(winOperator.getWindowAssigner() instanceof TumblingEventTimeWindows);
	Assert.assertTrue(winOperator.getStateDescriptor() instanceof ListStateDescriptor);

	processElementAndEnsureOutput(winOperator, winOperator.getKeySelector(), BasicTypeInfo.STRING_TYPE_INFO, new Tuple2<>("hello", 1));
}
 
Example #29
Source File: KeyedProcessOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that we don't have leakage between different keys.
 */
@Test
public void testEventTimeTimerWithState() throws Exception {

	KeyedProcessOperator<Integer, Integer, String> operator =
			new KeyedProcessOperator<>(new TriggeringStatefulFlatMapFunction(TimeDomain.EVENT_TIME));

	OneInputStreamOperatorTestHarness<Integer, String> testHarness =
			new KeyedOneInputStreamOperatorTestHarness<>(operator, new IdentityKeySelector<Integer>(), BasicTypeInfo.INT_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	testHarness.processWatermark(new Watermark(1));
	testHarness.processElement(new StreamRecord<>(17, 0L)); // should set timer for 6
	testHarness.processElement(new StreamRecord<>(13, 0L)); // should set timer for 6

	testHarness.processWatermark(new Watermark(2));
	testHarness.processElement(new StreamRecord<>(42, 1L)); // should set timer for 7
	testHarness.processElement(new StreamRecord<>(13, 1L)); // should delete timer

	testHarness.processWatermark(new Watermark(6));
	testHarness.processWatermark(new Watermark(7));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();

	expectedOutput.add(new Watermark(1L));
	expectedOutput.add(new StreamRecord<>("INPUT:17", 0L));
	expectedOutput.add(new StreamRecord<>("INPUT:13", 0L));
	expectedOutput.add(new Watermark(2L));
	expectedOutput.add(new StreamRecord<>("INPUT:42", 1L));
	expectedOutput.add(new StreamRecord<>("STATE:17", 6L));
	expectedOutput.add(new Watermark(6L));
	expectedOutput.add(new StreamRecord<>("STATE:42", 7L));
	expectedOutput.add(new Watermark(7L));

	TestHarnessUtil.assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());

	testHarness.close();
}
 
Example #30
Source File: WindowOperatorMigrationTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Manually run this to write binary snapshot data.
 */
@Ignore
@Test
public void writeSessionWindowsWithCountTriggerInMintConditionSnapshot() throws Exception {

	final int sessionSize = 3;

	ListStateDescriptor<Tuple2<String, Integer>> stateDesc = new ListStateDescriptor<>("window-contents",
			STRING_INT_TUPLE.createSerializer(new ExecutionConfig()));

	WindowOperator<String, Tuple2<String, Integer>, Iterable<Tuple2<String, Integer>>, Tuple3<String, Long, Long>, TimeWindow> operator = new WindowOperator<>(
			EventTimeSessionWindows.withGap(Time.seconds(sessionSize)),
			new TimeWindow.Serializer(),
			new TupleKeySelector<String>(),
			BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()),
			stateDesc,
			new InternalIterableWindowFunction<>(new SessionWindowFunction()),
			PurgingTrigger.of(CountTrigger.of(4)),
			0,
			null /* late data output tag */);

	OneInputStreamOperatorTestHarness<Tuple2<String, Integer>, Tuple3<String, Long, Long>> testHarness =
			new KeyedOneInputStreamOperatorTestHarness<>(operator, new TupleKeySelector<>(), BasicTypeInfo.STRING_TYPE_INFO);

	testHarness.setup();
	testHarness.open();

	// do snapshot and save to file
	OperatorSubtaskState snapshot = testHarness.snapshot(0, 0);
	OperatorSnapshotUtil.writeStateHandle(
		snapshot,
		"src/test/resources/win-op-migration-test-session-with-stateful-trigger-mint-flink" + flinkGenerateSavepointVersion + "-snapshot");

	testHarness.close();
}