org.apache.flink.api.common.functions.RichFlatJoinFunction Java Examples

The following examples show how to use org.apache.flink.api.common.functions.RichFlatJoinFunction. 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: TypeExtractorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSubclassOfTuple() {
	// use getJoinReturnTypes()
	RichFlatJoinFunction<?, ?, ?> function = new RichFlatJoinFunction<CustomTuple, String, CustomTuple>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void join(CustomTuple first, String second, Collector<CustomTuple> out) throws Exception {
			out.collect(null);
		}			
	};

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

	Assert.assertTrue(ti.isTupleType());
	Assert.assertEquals(2, ti.getArity());
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(0));
	Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(1));
	Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti).getTypeClass());

	// use getForObject()
	CustomTuple t = new CustomTuple("hello", 1);
	TypeInformation<?> ti2 = TypeExtractor.getForObject(t);

	Assert.assertTrue(ti2.isTupleType());
	Assert.assertEquals(2, ti2.getArity());
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(0));
	Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(1));
	Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti2).getTypeClass());
}
 
Example #2
Source File: TypeExtractorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSubclassOfTuple() {
	// use getJoinReturnTypes()
	RichFlatJoinFunction<?, ?, ?> function = new RichFlatJoinFunction<CustomTuple, String, CustomTuple>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void join(CustomTuple first, String second, Collector<CustomTuple> out) throws Exception {
			out.collect(null);
		}			
	};

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

	Assert.assertTrue(ti.isTupleType());
	Assert.assertEquals(2, ti.getArity());
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(0));
	Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(1));
	Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti).getTypeClass());

	// use getForObject()
	CustomTuple t = new CustomTuple("hello", 1);
	TypeInformation<?> ti2 = TypeExtractor.getForObject(t);

	Assert.assertTrue(ti2.isTupleType());
	Assert.assertEquals(2, ti2.getArity());
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(0));
	Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(1));
	Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti2).getTypeClass());
}
 
Example #3
Source File: TypeExtractorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSubclassOfTuple() {
	// use getJoinReturnTypes()
	RichFlatJoinFunction<?, ?, ?> function = new RichFlatJoinFunction<CustomTuple, String, CustomTuple>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void join(CustomTuple first, String second, Collector<CustomTuple> out) throws Exception {
			out.collect(null);
		}			
	};

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

	Assert.assertTrue(ti.isTupleType());
	Assert.assertEquals(2, ti.getArity());
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(0));
	Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti).getTypeAt(1));
	Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti).getTypeClass());

	// use getForObject()
	CustomTuple t = new CustomTuple("hello", 1);
	TypeInformation<?> ti2 = TypeExtractor.getForObject(t);

	Assert.assertTrue(ti2.isTupleType());
	Assert.assertEquals(2, ti2.getArity());
	Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(0));
	Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) ti2).getTypeAt(1));
	Assert.assertEquals(CustomTuple.class, ((TupleTypeInfo<?>) ti2).getTypeClass());
}
 
Example #4
Source File: InnerJoinOperatorBaseTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testJoinRich(){
	final AtomicBoolean opened = new AtomicBoolean(false);
	final AtomicBoolean closed = new AtomicBoolean(false);
	final String taskName = "Test rich join function";

	final RichFlatJoinFunction<String, String, Integer> joiner = new RichFlatJoinFunction<String, String, Integer>() {
		@Override
		public void open(Configuration parameters) throws Exception {
			opened.compareAndSet(false, true);
			assertEquals(0, getRuntimeContext().getIndexOfThisSubtask());
			assertEquals(1, getRuntimeContext().getNumberOfParallelSubtasks());
		}

		@Override
		public void close() throws Exception{
			closed.compareAndSet(false, true);
		}

		@Override
		public void join(String first, String second, Collector<Integer> out) throws Exception {
			out.collect(first.length());
			out.collect(second.length());
		}
	};

	InnerJoinOperatorBase<String, String, Integer,
					RichFlatJoinFunction<String, String, Integer>> base = new InnerJoinOperatorBase<String, String, Integer,
									RichFlatJoinFunction<String, String, Integer>>(joiner, new BinaryOperatorInformation<String, String,
			Integer>(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO,
			BasicTypeInfo.INT_TYPE_INFO), new int[0], new int[0], taskName);

	final List<String> inputData1 = new ArrayList<String>(Arrays.asList("foo", "bar", "foobar"));
	final List<String> inputData2 = new ArrayList<String>(Arrays.asList("foobar", "foo"));
	final List<Integer> expected = new ArrayList<Integer>(Arrays.asList(3, 3, 6, 6));


	try {
		final TaskInfo taskInfo = new TaskInfo(taskName, 1, 0, 1, 0);
		final HashMap<String, Accumulator<?, ?>> accumulatorMap = new HashMap<String, Accumulator<?, ?>>();
		final HashMap<String, Future<Path>> cpTasks = new HashMap<>();

		ExecutionConfig executionConfig = new ExecutionConfig();
		
		executionConfig.disableObjectReuse();
		List<Integer> resultSafe = base.executeOnCollections(inputData1, inputData2,
				new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks,
						accumulatorMap, new UnregisteredMetricsGroup()),
				executionConfig);
		
		executionConfig.enableObjectReuse();
		List<Integer> resultRegular = base.executeOnCollections(inputData1, inputData2,
				new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks,
						accumulatorMap, new UnregisteredMetricsGroup()),
				executionConfig);

		assertEquals(expected, resultSafe);
		assertEquals(expected, resultRegular);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}

	assertTrue(opened.get());
	assertTrue(closed.get());
}
 
Example #5
Source File: InnerJoinOperatorBaseTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testJoinRich(){
	final AtomicBoolean opened = new AtomicBoolean(false);
	final AtomicBoolean closed = new AtomicBoolean(false);
	final String taskName = "Test rich join function";

	final RichFlatJoinFunction<String, String, Integer> joiner = new RichFlatJoinFunction<String, String, Integer>() {
		@Override
		public void open(Configuration parameters) throws Exception {
			opened.compareAndSet(false, true);
			assertEquals(0, getRuntimeContext().getIndexOfThisSubtask());
			assertEquals(1, getRuntimeContext().getNumberOfParallelSubtasks());
		}

		@Override
		public void close() throws Exception{
			closed.compareAndSet(false, true);
		}

		@Override
		public void join(String first, String second, Collector<Integer> out) throws Exception {
			out.collect(first.length());
			out.collect(second.length());
		}
	};

	InnerJoinOperatorBase<String, String, Integer,
					RichFlatJoinFunction<String, String, Integer>> base = new InnerJoinOperatorBase<String, String, Integer,
									RichFlatJoinFunction<String, String, Integer>>(joiner, new BinaryOperatorInformation<String, String,
			Integer>(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO,
			BasicTypeInfo.INT_TYPE_INFO), new int[0], new int[0], taskName);

	final List<String> inputData1 = new ArrayList<String>(Arrays.asList("foo", "bar", "foobar"));
	final List<String> inputData2 = new ArrayList<String>(Arrays.asList("foobar", "foo"));
	final List<Integer> expected = new ArrayList<Integer>(Arrays.asList(3, 3, 6, 6));


	try {
		final TaskInfo taskInfo = new TaskInfo(taskName, 1, 0, 1, 0);
		final HashMap<String, Accumulator<?, ?>> accumulatorMap = new HashMap<String, Accumulator<?, ?>>();
		final HashMap<String, Future<Path>> cpTasks = new HashMap<>();

		ExecutionConfig executionConfig = new ExecutionConfig();
		
		executionConfig.disableObjectReuse();
		List<Integer> resultSafe = base.executeOnCollections(inputData1, inputData2,
				new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks,
						accumulatorMap, new UnregisteredMetricsGroup()),
				executionConfig);
		
		executionConfig.enableObjectReuse();
		List<Integer> resultRegular = base.executeOnCollections(inputData1, inputData2,
				new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks,
						accumulatorMap, new UnregisteredMetricsGroup()),
				executionConfig);

		assertEquals(expected, resultSafe);
		assertEquals(expected, resultRegular);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}

	assertTrue(opened.get());
	assertTrue(closed.get());
}
 
Example #6
Source File: InnerJoinOperatorBaseTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testJoinRich(){
	final AtomicBoolean opened = new AtomicBoolean(false);
	final AtomicBoolean closed = new AtomicBoolean(false);
	final String taskName = "Test rich join function";

	final RichFlatJoinFunction<String, String, Integer> joiner = new RichFlatJoinFunction<String, String, Integer>() {
		@Override
		public void open(Configuration parameters) throws Exception {
			opened.compareAndSet(false, true);
			assertEquals(0, getRuntimeContext().getIndexOfThisSubtask());
			assertEquals(1, getRuntimeContext().getNumberOfParallelSubtasks());
		}

		@Override
		public void close() throws Exception{
			closed.compareAndSet(false, true);
		}

		@Override
		public void join(String first, String second, Collector<Integer> out) throws Exception {
			out.collect(first.length());
			out.collect(second.length());
		}
	};

	InnerJoinOperatorBase<String, String, Integer,
					RichFlatJoinFunction<String, String, Integer>> base = new InnerJoinOperatorBase<String, String, Integer,
									RichFlatJoinFunction<String, String, Integer>>(joiner, new BinaryOperatorInformation<String, String,
			Integer>(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO,
			BasicTypeInfo.INT_TYPE_INFO), new int[0], new int[0], taskName);

	final List<String> inputData1 = new ArrayList<String>(Arrays.asList("foo", "bar", "foobar"));
	final List<String> inputData2 = new ArrayList<String>(Arrays.asList("foobar", "foo"));
	final List<Integer> expected = new ArrayList<Integer>(Arrays.asList(3, 3, 6, 6));


	try {
		final TaskInfo taskInfo = new TaskInfo(taskName, 1, 0, 1, 0);
		final HashMap<String, Accumulator<?, ?>> accumulatorMap = new HashMap<String, Accumulator<?, ?>>();
		final HashMap<String, Future<Path>> cpTasks = new HashMap<>();

		ExecutionConfig executionConfig = new ExecutionConfig();
		
		executionConfig.disableObjectReuse();
		List<Integer> resultSafe = base.executeOnCollections(inputData1, inputData2,
				new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks,
						accumulatorMap, new UnregisteredMetricsGroup()),
				executionConfig);
		
		executionConfig.enableObjectReuse();
		List<Integer> resultRegular = base.executeOnCollections(inputData1, inputData2,
				new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks,
						accumulatorMap, new UnregisteredMetricsGroup()),
				executionConfig);

		assertEquals(expected, resultSafe);
		assertEquals(expected, resultRegular);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}

	assertTrue(opened.get());
	assertTrue(closed.get());
}