两种api的使用
1、NoKeyedProcessingTime滑动窗口
不分组,按照ProcessingTime划分为滑动窗口,然后调用reduce对窗口内的数据进行聚合
新api【TumblingAlignedProcessingTimeWindows】
直接代码
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.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
/**
* 滑动窗口--【无界流】
* 新api【TumblingAlignedProcessingTimeWindows】
* 案例需求:当时间触发后,才会生成新的窗口,这里的需求是当5秒钟过去后会生成一个新的窗口进行聚合
*/
public class ProcessingTimeSlidingWindowAllDemo {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
// env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime); //老api
DataStreamSource<String> lines = env.socketTextStream("Master", 8888);
//将字符串转为数字【lamber表达式】
//本地执行,执行并行度为4,所以调用map返回的DataStream的并行度为4
SingleOutputStreamOperator<Integer> nums = lines.map(Integer::parseInt);
// AllWindowedStream<Integer, TimeWindow> windowed = nums.timeWindowAll(Time.seconds(30), Time.seconds(10));
AllWindowedStream<Integer, TimeWindow> windowed = nums.windowAll(SlidingProcessingTimeWindows.of(Time.seconds(30), Time.seconds(10)));
windowed.sum(0).print();
env.execute();
}
}
打印输出,在8888窗口输入10个1,让其滑动测试如下:
查看job: http://localhost:8081/#/job/cbb14bd6e7fe7f13febaa057f3807e5d/overview
2、KeyedProcessingTime滑动窗口
先keybey,再划分ProcessingTime的滑动窗口
直接代码
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
/**
* KeyedProcessingTime滑动窗口--【无界流】
*/
public class ProcessingTimeSlidingWindowDemo {
public static void main(String [] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
DataStreamSource<String> lines = env.socketTextStream("Master", 8888);
SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndCount = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
@Override
public Tuple2<String, Integer> map(String value) throws Exception {
String[] fields = value.split(",");
return Tuple2.of(fields[0], Integer.parseInt(fields[1]));
}
});
//调用keyBy
KeyedStream<Tuple2<String, Integer>, String> keyed = wordAndCount.keyBy(t -> t.f0);
//NoKeyed window:不调用keyBy,然后调用windowAll方法,传入windowAssinger
//Keyed window:先调用keyBy,然后调用windowAll方法,传入windowAssinger
/**
* 第一个参数:窗口的长度时间
* 第二个参数:滑动时间
* 说明:
* (1)滑动的间隔时间与参数滑动的步长可以设置一样,如:Time.seconds(30),Time.seconds(30).--->变成滚动窗口
* (2)滑动的间隔时间<参数滑动的步长,如:Time.seconds(30),Time.seconds(60).也是可以,但是会少算一些数据
*/
WindowedStream<Tuple2<String, Integer>, String, TimeWindow> windowed = keyed.window(SlidingProcessingTimeWindows.of(Time.seconds(30),Time.seconds(10)));
windowed.sum(1).print();
env.execute();
}
}
在nc -lk的的命令下,输入的数字信息测试:
查看job:http://localhost:8081/#/job/24a41ae174fa92c5872f40b49ff35f02/overview