目的:读取集合中的数据
环境:jdk8 工具:idea
创建maven项目
引入jar包
<dependency><groupId>org.apache.flink</groupId><artifactId>flink-java</artifactId><version>1.10.1</version></dependency><dependency><groupId>org.apache.flink</groupId><artifactId>flink-streaming-java_2.12</artifactId><version>1.10.1</version></dependency>
代码演示
DataStream用于实时数据统计
//创建流处理执行环境StreamExecutionEnvironment env=StreamExecutionEnvironment.getExecutionEnvironment();List<Test> testData = Arrays.asList(new Test(1, "测试1", "测试1"),new Test(2, "测试2", "测试2"),new Test(3, "测试3", "测试3"));DataStream<Test> dataStream = env.fromCollection(testData);dataStream.print();env.execute(); //如果要保持顺序可以设置全局并行度为1
Test类
package com.hello.flink;public class Test {private Integer id;private String name;private String content;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Test(Integer id, String name, String content) {this.id = id;this.name = name;this.content = content;}}
结果
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.4> Test{id=1, name='测试1', content='测试1'}2> Test{id=3, name='测试3', content='测试3'}1> Test{id=2, name='测试2', content='测试2'}
