其实我挺排斥用java写大数据代码的,但是没办法公司用java写。不过Java写也是有道理的嘛,所以就从Scala转到了Java。
依赖
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
<!-- <scope>provided</scope>-->
</dependency>
<!--没有会报错:No ExecutorFactory found to execute the application.-->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
<!-- <scope>runtime</scope>-->
</dependency>
demo
很简单的一个读取元素然后对每个元素乘以3的程序。重在走通java写flink的流程。
public class FirstDemo {
public static void main(String[] args) {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.fromElements(1,2,3,4,5)
.map(re -> re * 3)
.print();
try {
env.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}