上篇:基于flink的WindowFunction算子增量聚合使用
直接代码
package cn._51doit.flink.day04;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.util.Collector;
import java.util.ArrayList;
import java.util.Comparator;
/**
* CountWindow的使用[无界流]
* 不分组划分窗口,然后调用apply对窗口内的数据处理
* 特点:就是将窗口内的数据存起来(window state)中,当满足触发条件后,在状态中的数据取出来进行计算
* 需求:输入的数据存起来,当满足触发条件后,在状态中的数据取出来进行计算
*/
public class CountWindowAllApplyDemo {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
//1
//2
//3
DataStreamSource<String> lines = env.socketTextStream("Master", 8888);
//将字符串转为数字【lamber表达式】
//本地执行,执行并行度为4,所以调用map返回的DataStream的并行度为4
SingleOutputStreamOperator<Integer> nums = lines.map(Integer::parseInt);
//划分window
//GlobalWindow有几个并行度?并行度:1,只有一个分区(在这个窗口内只有一个subTask)
AllWindowedStream<Integer, GlobalWindow> windowed = nums.countWindowAll(5);
windowed.apply(new AllWindowFunction<Integer, Integer, GlobalWindow>() {
/**
*
* @param window
* @param values 输入的数据【持久化】
* @param out 输出的数据
* @throws Exception
*/
@Override
public void apply(GlobalWindow window, Iterable<Integer> values, Collector<Integer> out) throws Exception {
Integer sum =0;
ArrayList<Integer>list=new ArrayList<>();
for (Integer value: values) {
list.add(value);
}
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(01,o2);
}
});
//输出
for (Integer i: list) {
out.collect(i);
}
}
}).print().setParallelism(1);
env.execute();
}
}
打印输出:输入5个,输出5个,按一个分区当作一个批次输入输出打印
查看job:http://localhost:8081/#/job/6b381e3a25fc0fe4b356f99764bbf960/overview