Flink系列文章 java实现增量文件WordCount,任务部署到yarn
Apache Flink® - 数据流上的有状态计算
Apache Flink 是一个框架和分布式处理引擎,用于在无边界和有边界数据流上进行有状态的计算。Flink
能在所有常见集群环境中运行,并能以内存速度和任意规模进行计算。接下来,我们来介绍一下 Flink 架构中的重要方面。
处理无界和有界数据
任何类型的数据都可以形成一种事件流。信用卡交易、传感器测量、机器日志、网站或移动应用程序上的用户交互记录,所有这些数据都形成一种流。
数据可以被作为 无界 或者 有界 流来处理。
无界流 有定义流的开始,但没有定义流的结束。它们会无休止地产生数据。无界流的数据必须持续处理,即数据被摄取后需要立刻处理。我们不能等到所有数据都到达再处理,因为输入是无限的,在任何时候输入都不会完成。处理无界数据通常要求以特定顺序摄取事件,例如事件发生的顺序,以便能够推断结果的完整性。
有界流 有定义流的开始,也有定义流的结束。有界流可以在摄取所有数据后再进行计算。有界流所有数据可以被排序,所以并不需要有序摄取。有界流处理通常被称为批处理
Apache Flink 擅长处理无界和有界数据集 精确的时间控制和状态化使得 Flink 的运行时(runtime)能够运行任何处理无界流的应用。有界流则由一些专为固定大小数据集特殊设计的算法和数据结构进行内部处理,产生了出色的性能。
环境准备
组件 | 版本 |
---|---|
Apache Hadoop | 3.2.1 |
Idea | 2018.3 |
CentOS | 7 |
JDK | 1.8 |
Apache Flink | 1.10.0 |
我们的目标
用java和Flink实现一个程序可以对一个文件进行单词计数,并且当我们往文件里写入新的内容并保存时,程序会自动的对增量的内容进行追加计数。
然后我们会将这个任务提交到yarn上面运行。
FileWindowWordCount
引入依赖
Gradle
// 下面四个依赖是java开发Flink应用必要的依赖
// https://mvnrepository.com/artifact/org.apache.flink/flink-core
compile group: 'org.apache.flink', name: 'flink-core', version: '1.10.0'
// https://mvnrepository.com/artifact/org.apache.flink/flink-java
compile group: 'org.apache.flink', name: 'flink-java', version: '1.10.0'
// https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-java
compile group: 'org.apache.flink', name: 'flink-streaming-java_2.12', version: '1.10.0'
// https://mvnrepository.com/artifact/org.apache.flink/flink-clients
compile group: 'org.apache.flink', name: 'flink-clients_2.12', version: '1.10.0'
码代码
package cn.flink;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.io.TextInputFormat;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.FileProcessingMode;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
/**
* 增量读取文件中的内容并对单词计数
* 改编自Apache Flink官网上的SocketStreamWordCount
*/
public class LocalFileWindowWordCount {
public static void main(String[] args) throws Exception {
// 待处理的文件由 --input 参数指定
String input;
try {
final ParameterTool params = ParameterTool.fromArgs(args);
input = params.get("input");
} catch (Exception e) {
System.err.println("No port specified. Please run 'LocalFileWindowWordCount --input <input>'");
return;
}
// get the execution environment
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
Path path = new org.apache.flink.core.fs.Path(input);
TextInputFormat textInputFormat = new TextInputFormat(path);
// get input data by connecting to the socket
DataStream<String> text = env.readFile(textInputFormat, input, FileProcessingMode.PROCESS_CONTINUOUSLY,2000);
// parse the data, group it, window it, and aggregate the counts
DataStream<WordWithCount> windowCounts = text
.flatMap(new FlatMapFunction<String, WordWithCount>() {
@Override
public void flatMap(String value, Collector<WordWithCount> out) {
for (String word : value.split("\\s")) {
out.collect(new WordWithCount(word, 1L));
}
}
})
.keyBy("word")
.timeWindow(Time.seconds(1), Time.seconds(1))
.reduce(new ReduceFunction<WordWithCount>() {
@Override
public WordWithCount reduce(WordWithCount a, WordWithCount b) {
return new WordWithCount(a.word, a.count + b.count);
}
});
// print the results with a single thread, rather than in parallel
windowCounts.print().setParallelism(2);
env.execute("Local File Window WordCount");
}
// Data type for words with count
public static class WordWithCount {
public String word;
public long count;
public WordWithCount() {}
public WordWithCount(String word, long count) {
this.word = word;
this.count = count;
}
@Override
public String toString() {
return word + " : " + count;
}
}
}
在IDE里运行看下效果
先要新建一个空文本文件
我这里是D:\tmp\input.txt
运行项目
记事本打开input.txt文件
然后写入一行内容
OK , 结果正常 。
可以上传到服务器运行了。
bin/flink run -m yarn-cluster -p 2 -yjm 700m -ytm 1024m -c cn.flink.LocalFileWindowWordCount ~/ApacheFlink-0.0.3-alpha.jar --input /root/wordcount/input.txt