Apache Flink 是一个框架和分布式处理引擎,用于对无界和有界数据流进行有状态计算。Flink 旨在运行在所有常见的集群环境中,以内存速度和任意规模执行计算。
了解更多关于 Flink 的信息,请访问 https://flink.apache.org/
流式优先的运行时,同时支持批处理和数据流程序
优雅流畅的 Java API
同时支持极高吞吐量和低事件延迟的运行时
基于 Dataflow 模型,在 DataStream API 中支持 事件时间 和 乱序 处理
跨不同时间语义(事件时间、处理时间)的灵活窗口(时间、计数、会话、自定义触发器)
具有 恰好一次 处理保证的容错能力
流式程序中的自然背压
用于图处理(批处理)、机器学习(批处理)和复杂事件处理(流式)的库
自定义内存管理,可在内存和核外数据处理算法之间高效稳健地切换
Apache Hadoop MapReduce 的兼容层
与 YARN、HDFS、HBase 以及 Apache Hadoop 生态系统的其他组件集成
// pojo class WordWithCount
public class WordWithCount {
public String word;
public int count;
public WordWithCount() {}
public WordWithCount(String word, int count) {
this.word = word;
this.count = count;
}
}
// main method
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<String> text = env.socketTextStream(host, port);
DataStream<WordWithCount> windowCounts = text
.flatMap(
(FlatMapFunction<String, String>) (line, collector)
-> Arrays.stream(line.split("\\s")).forEach(collector::collect)
).returns(String.class)
.map(word -> new WordWithCount(word, 1)).returns(TypeInformation.of(WordWithCount.class))
.keyBy(wordWithCnt -> wordWithCnt.word)
.window(TumblingProcessingTimeWindows.of(Duration.ofSeconds(5)))
.sum("count").returns(TypeInformation.of(WordWithCount.class));
windowCounts.print();
env.execute();
}
// pojo class WordWithCount
public class WordWithCount {
public String word;
public int count;
public WordWithCount() {}
public WordWithCount(String word, int count) {
this.word = word;
this.count = count;
}
}
// main method
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setRuntimeMode(RuntimeExecutionMode.BATCH);
FileSource<String> source = FileSource.forRecordStreamFormat(new TextLineInputFormat(), new Path("MyInput.txt")).build();
DataStreamSource<String> text = env.fromSource(source, WatermarkStrategy.noWatermarks(), "MySource");
DataStream<WordWithCount> windowCounts = text
.flatMap((FlatMapFunction<String, String>) (line, collector) -> Arrays
.stream(line.split("\\s"))
.forEach(collector::collect)).returns(String.class)
.map(word -> new WordWithCount(word, 1)).returns(TypeInformation.of(WordWithCount.class))
.keyBy(wordWithCount -> wordWithCount.word)
.sum("count").returns(TypeInformation.of(WordWithCount.class));
windowCounts.print();
env.execute();
构建 Flink 的前提条件:
首先,克隆代码库:
git clone https://github.com/apache/flink.git
cd flink
然后,根据您的 Java 版本选择以下命令之一:
对于 Java 11
./mvnw clean package -DskipTests -Djdk11 -Pjava11-target
对于 Java 17(默认)
./mvnw clean package -DskipTests -Djdk17 -Pjava17-target
对于Java 21
./mvnw clean package -DskipTests -Djdk21 -Pjava21-target
构建过程大约需要 10 分钟。Flink 将安装在“build-target”中。
Flink 提交者使用 IntelliJ IDEA 开发 Flink 代码库。
我们推荐使用 IntelliJ IDEA 开发涉及 Scala 代码的项目。
IDE 的最低要求如下:
IntelliJ IDE 开箱即用地支持 Maven,并提供 Scala 开发插件。
查看我们的IntelliJ 设置 指南,了解更多详情。
**注意:**根据我们的经验,此设置不适用于 Flink,
原因是 Scala IDE 3.0.3 捆绑的旧 Eclipse 版本存在缺陷,
或者由于 Scala IDE 4.4.1 中捆绑的 Scala 版本不兼容。
我们建议使用 IntelliJ(见上文)
欢迎随时提问!
如果您需要任何帮助,请通过邮件列表联系开发者和社区。
如果您发现 Flink 中的错误,请提交问题。
Apache Flink 的文档位于网站:https://flink.apache.org 或源代码的“docs/”目录中。
这是一个活跃的开源项目。我们始终欢迎所有想要使用该系统或为其做出贡献的人。
如果您正在寻找适合您技能的实施任务,请联系我们。本文介绍了如何为 Apache Flink 做出贡献。
大多数 Flink 连接器已外部化到 Apache 软件基金会 下的独立代码库中:
Apache Flink 是 Apache 软件基金会 (ASF) 的一个开源项目。Apache Flink 项目源自 Stratosphere 研究项目。