考察点:DataStream API 基本用法、keyBy、时间窗口与聚合算子。
要点:
env.addSource 接入点击流(假设为 (userId, timestamp) 元组或 POJO),设置 EventTime 时间特性并分配 Timestamp/Watermark。keyBy(event -> event.userId) 按用户分区,再用 window(TumblingEventTimeWindows.of(Time.minutes(1))) 开 1 分钟滚动窗口。aggregate 或 process 统计次数(PV)。下面给出骨架:StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
DataStream<ClickEvent> clicks = env.addSource(new ClickSource())
.assignTimestampsAndWatermarks(
WatermarkStrategy.<ClickEvent>forBoundedOutOfOrderness(Duration.ofSeconds(5))
.withTimestampAssigner((e, ts) -> e.timestamp));
clicks.keyBy(e -> e.userId)
.window(TumblingEventTimeWindows.of(Time.minutes(1)))
.aggregate(new CountAgg(), new WindowResultFn())
.print();
env.execute("User PV per minute");
forBoundedOutOfOrderness 容忍 5 秒乱序;CountAgg 做增量计数避免存全量;窗口触发后由 WindowResultFn 输出 (userId, windowEnd, pv)。生产需配置 checkpoint 与并行度,并把结果 sink 到下游存储。题目与答案就在本页;收藏、批注和错题重练在应用内