/** * Creates a data stream from the given non-empty collection. * * <p>Note that this operation will result in a non-parallel data stream source, * i.e., a data stream source with parallelism one. * * @param data * The collection of elements to create the data stream from * @param typeInfo * The TypeInformation for the produced data stream * @param <OUT> * The type of the returned data stream * @return The data stream representing the given collection */ public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) { Preconditions.checkNotNull(data, "Collection must not be null"); // must not have null elements and mixed elements FromElementsFunction.checkCollection(data, typeInfo.getTypeClass()); SourceFunction<OUT> function; try { function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return addSource(function, "Collection Source", typeInfo).setParallelism(1); }
@Test public void fromCollectionTest() throws Exception { List<Integer> expectedList = Arrays.asList(1, 2, 3); List<Integer> actualList = SourceFunctionUtil.runSourceFunction( CommonTestUtils.createCopySerializable(new FromElementsFunction<Integer>( IntSerializer.INSTANCE, Arrays.asList(1, 2, 3)))); assertEquals(expectedList, actualList); } }
@Test public void fromElementsTest() throws Exception { List<Integer> expectedList = Arrays.asList(1, 2, 3); List<Integer> actualList = SourceFunctionUtil.runSourceFunction(CommonTestUtils.createCopySerializable( new FromElementsFunction<Integer>( IntSerializer.INSTANCE, 1, 2, 3))); assertEquals(expectedList, actualList); }
@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()); } }
@Test public void testNonJavaSerializableType() { try { MyPojo[] data = { new MyPojo(1, 2), new MyPojo(3, 4), new MyPojo(5, 6) }; FromElementsFunction<MyPojo> source = new FromElementsFunction<MyPojo>( TypeExtractor.getForClass(MyPojo.class).createSerializer(new ExecutionConfig()), data); List<MyPojo> result = new ArrayList<MyPojo>(); source.run(new ListSourceContext<MyPojo>(result)); assertEquals(Arrays.asList(data), result); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testDeSerializationError() { try { TypeInformation<DeserializeTooMuchType> info = new ValueTypeInfo<DeserializeTooMuchType>(DeserializeTooMuchType.class); FromElementsFunction<DeserializeTooMuchType> source = new FromElementsFunction<DeserializeTooMuchType>( info.createSerializer(new ExecutionConfig()), new DeserializeTooMuchType()); try { source.run(new ListSourceContext<DeserializeTooMuchType>(new ArrayList<DeserializeTooMuchType>())); fail("should fail with an exception"); } catch (IOException e) { assertTrue(ExceptionUtils.stringifyException(e).contains("user-defined serialization")); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testSerializationError() { try { TypeInformation<SerializationErrorType> info = new ValueTypeInfo<SerializationErrorType>(SerializationErrorType.class); try { new FromElementsFunction<SerializationErrorType>( info.createSerializer(new ExecutionConfig()), new SerializationErrorType()); fail("should fail with an exception"); } catch (IOException e) { assertTrue(ExceptionUtils.stringifyException(e).contains("test exception")); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
final FromElementsFunction<Integer> source = new FromElementsFunction<>(IntSerializer.INSTANCE, data); StreamSource<Integer, FromElementsFunction<Integer>> src = new StreamSource<>(source); AbstractStreamOperatorTestHarness<Integer> testHarness = final FromElementsFunction<Integer> sourceCopy = new FromElementsFunction<>(IntSerializer.INSTANCE, data); StreamSource<Integer, FromElementsFunction<Integer>> srcCopy = new StreamSource<>(sourceCopy); AbstractStreamOperatorTestHarness<Integer> testHarnessCopy =
/** * Creates a data stream from the given non-empty collection. * * <p>Note that this operation will result in a non-parallel data stream source, * i.e., a data stream source with parallelism one. * * @param data * The collection of elements to create the data stream from * @param typeInfo * The TypeInformation for the produced data stream * @param <OUT> * The type of the returned data stream * @return The data stream representing the given collection */ public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) { Preconditions.checkNotNull(data, "Collection must not be null"); // must not have null elements and mixed elements FromElementsFunction.checkCollection(data, typeInfo.getTypeClass()); SourceFunction<OUT> function; try { function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return addSource(function, "Collection Source", typeInfo).setParallelism(1); }
/** * Creates a data stream from the given non-empty collection. * * <p>Note that this operation will result in a non-parallel data stream source, * i.e., a data stream source with parallelism one. * * @param data * The collection of elements to create the data stream from * @param typeInfo * The TypeInformation for the produced data stream * @param <OUT> * The type of the returned data stream * @return The data stream representing the given collection */ public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) { Preconditions.checkNotNull(data, "Collection must not be null"); // must not have null elements and mixed elements FromElementsFunction.checkCollection(data, typeInfo.getTypeClass()); SourceFunction<OUT> function; try { function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return addSource(function, "Collection Source", typeInfo).setParallelism(1); }
/** * Creates a data stream from the given non-empty collection. * * <p>Note that this operation will result in a non-parallel data stream source, * i.e., a data stream source with a parallelism one. * * @param data * The collection of elements to create the data stream from * @param typeInfo * The TypeInformation for the produced data stream * @param <OUT> * The type of the returned data stream * @return The data stream representing the given collection */ public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) { Preconditions.checkNotNull(data, "Collection must not be null"); // must not have null elements and mixed elements FromElementsFunction.checkCollection(data, typeInfo.getTypeClass()); SourceFunction<OUT> function; try { function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return addSource(function, "Collection Source", typeInfo).setParallelism(1); }
/** * Creates a data stream from the given non-empty collection. * * <p>Note that this operation will result in a non-parallel data stream source, * i.e., a data stream source with parallelism one. * * @param data * The collection of elements to create the data stream from * @param typeInfo * The TypeInformation for the produced data stream * @param <OUT> * The type of the returned data stream * @return The data stream representing the given collection */ public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) { Preconditions.checkNotNull(data, "Collection must not be null"); // must not have null elements and mixed elements FromElementsFunction.checkCollection(data, typeInfo.getTypeClass()); SourceFunction<OUT> function; try { function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return addSource(function, "Collection Source", typeInfo).setParallelism(1); }