Flink状态应用测试程序示例
文章来源地址https://www.toymoban.com/news/detail-824976.html
1. 创建执行环境
// 1. 创建执行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
2. 创建数据流
// 2. 创建数据流
DataStream<Tuple2<String, Integer>> inputStream = env.fromElements(
Tuple2.of("a", 1),
Tuple2.of("b", 2),
Tuple2.of("c", 3),
Tuple2.of("a", 4),
Tuple2.of("b", 5),
Tuple2.of("c", 6)
).uid("source").name("source");
3. 对数据流进行keyBy()操作
// 3. 对数据流进行keyBy()操作
DataStream<Tuple2<String, Integer>> keyedStream = inputStream.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
@Override
public String getKey(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
return stringIntegerTuple2.f0;
}
});
4. 使用RichFlatMapFunction来实现状态的维护
// 4. 使用RichFlatMapFunction来实现状态的维护
keyedStream.flatMap(new RichFlatMapFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() {
// 定义状态变量
private ValueState<Integer> countState;
@Override
public void open(Configuration parameters) throws Exception {
// 初始化状态变量
ValueStateDescriptor<Integer> descriptor = new ValueStateDescriptor<>("count", Integer.class);
countState = getRuntimeContext().getState(descriptor);
}
@Override
public void flatMap(Tuple2<String, Integer> input, Collector<Tuple2<String, Integer>> out) throws Exception {
// 获取状态变量的值
Integer currentCount = countState.value();
// 更新状态变量的值
if (currentCount == null) {
currentCount = 0;
}
currentCount++;
countState.update(currentCount);
// 输出结果
out.collect(Tuple2.of(input.f0, currentCount));
}
}).print();
5.执行任务
// 5.执行任务
env.execute("Flink State Test");
6.执行结果
(a,1)
(b,1)
(c,1)
(a,2)
(b,2)
(c,2)
Process finished with exit code 0
文章来源:https://www.toymoban.com/news/detail-824976.html
到了这里,关于Flink状态应用测试程序示例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!