Flink介绍
Flink是一个什么样的框架呢?flink官网是这么介绍的:
Apache Flink is a framework and distributed processing engine for stateful computations over unbounded and bounded data streams.
Apache FIink是一个框架和分布式处理引擎,用于对无界和有界数据流进行状态计算。
Flink的使用场景
1.电商和市场营销
数据报表、广告投放、业务流程需要
2.物联网(IOT)
传感器实时数据采集和显示、实时报警、交通运输业
3.电信业
基站流量调配
4.银行和金融业
实时结算和通知推送,实时检测异常行为
为什么选择Flink
1.流数据可以更正式地反应我们的生活方式。
2.传统的数据架构是基于有限数据集的
3.低延迟、高吞吐、结果的准确性和良好的容错性。
Flink简单示例
首先idea新建一个Maven项目
添加maven依赖
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.12</artifactId>
<version>1.12.2</version>
<scope>provided</scope>
</dependency>
然后写创建一个类,用来统计单词词频统计
public class WordCount {
public static void main(String[] args) throws Exception {
//解析命令行传过来的参数
ParameterTool params = ParameterTool.fromArgs(args);
//获取一个执行环境
final ExecutionEnvironment evn = ExecutionEnvironment.getExecutionEnvironment();
//读取输入的数据
DataSet<String> dataSet = null;
if (params.has("input")) {
dataSet = evn.readTextFile(params.get("input"));
} else {
dataSet = WordCountData.getDefaultTextLineDataSet(evn);
}
//单词词频统计
DataSet<Tuple2<String, Integer>> counts = dataSet.flatMap(new Tokenizer()).groupBy(0).sum(1);
if (params.has("output")) {
// 数据输出为csv 格式
counts.writeAsCsv(params.get("output"), "\n", "");
// 提交执行flink应用
evn.execute("wordcount example");
} else {
//数据打印到控制台
counts.print();
}
}
public static final class Tokenizer implements FlatMapFunction<String , Tuple2<String, Integer>> {
@Override
public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
String[] tokens = s.toLowerCase().split("\\W+");
for (String token : tokens) {
collector.collect(new Tuple2<String, Integer>(token,1));
}
}
}
}
当然还有单词词频文件,这里我们用的flink-example项目的WordCountData文件
public class WordCountData {
public static final String[] WORDS =
new String[] {
"To be, or not to be,--that is the question:--",
"Whether 'tis nobler in the mind to suffer",
"The slings and arrows of outrageous fortune",
"Or to take arms against a sea of troubles,",
"And by opposing end them?--To die,--to sleep,--",
"No more; and by a sleep to say we end",
"The heartache, and the thousand natural shocks",
"That flesh is heir to,--'tis a consummation",
"Devoutly to be wish'd. To die,--to sleep;--",
"To sleep! perchance to dream:--ay, there's the rub;",
"For in that sleep of death what dreams may come,",
"When we have shuffled off this mortal coil,",
"Must give us pause: there's the respect",
"That makes calamity of so long life;",
"For who would bear the whips and scorns of time,",
"The oppressor's wrong, the proud man's contumely,",
"The pangs of despis'd love, the law's delay,",
"The insolence of office, and the spurns",
"That patient merit of the unworthy takes,",
"When he himself might his quietus make",
"With a bare bodkin? who would these fardels bear,",
"To grunt and sweat under a weary life,",
"But that the dread of something after death,--",
"The undiscover'd country, from whose bourn",
"No traveller returns,--puzzles the will,",
"And makes us rather bear those ills we have",
"Than fly to others that we know not of?",
"Thus conscience does make cowards of us all;",
"And thus the native hue of resolution",
"Is sicklied o'er with the pale cast of thought;",
"And enterprises of great pith and moment,",
"With this regard, their currents turn awry,",
"And lose the name of action.--Soft you now!",
"The fair Ophelia!--Nymph, in thy orisons",
"Be all my sins remember'd."
};
public static DataSet<String> getDefaultTextLineDataSet(ExecutionEnvironment env) {
return env.fromElements(WORDS);
}
}
然后执行main方法,启动后你会发现,控制台报错了:
接下来,我们根据报错添加依赖:
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_2.12</artifactId>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
然后重新执行代码
从控制台的执行结果来看,我们成功的运行了flink的一个小案例。
源码:flink流处理demo