flink初级编程实践
时间: 2023-08-31 13:12:10 浏览: 251
Flink 是一个分布式流处理和批处理框架,可以用于大规模数据的实时处理和分析。下面是一个简单的 Flink 编程实践示例,用于计算输入流中每个单词的出现次数:
```java
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
public class WordCount {
public static void main(String[] args) throws Exception {
// 创建执行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// 定义输入流
DataStream<String> input = env.addSource(new SimpleSource());
// 数据转换和处理
DataStream<Tuple2<String, Integer>> counts = input
.flatMap((String value, Collector<Tuple2<String, Integer>> out) -> {
// 按空格拆分每行,并输出每个单词的次数
for (String word : value.split(" ")) {
out.collect(new Tuple2<>(word, 1));
}
})
.keyBy(0) // 按单词分组
.sum(1); // 求和
// 输出结果
counts.print();
// 执行程序
env.execute();
}
// 自定义数据源,用于提供输入数据
public static class SimpleSource implements SourceFunction<String> {
private volatile boolean isRunning = true;
@Override
public void run(SourceContext<String> ctx) throws Exception {
while (isRunning) {
ctx.collect("hello world");
Thread.sleep(1000);
}
}
@Override
public void cancel() {
isRunning = f
阅读全文