package examples;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import java.time.Duration;
public class SensorExample {
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env =
StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<SensorExample.SensorReading> input = env.fromElements(
new SensorExample.SensorReading("machine_01", 35, 1610704407000L),
new SensorExample.SensorReading("machine_01", 35, 1610704417000L),
new SensorExample.SensorReading("machine_01", 2, 1610704427000L),
new SensorExample.SensorReading("machine_01", 2, 1610704437000L),
new SensorExample.SensorReading("machine_01", 2, 1610704447000L),
new SensorExample.SensorReading("machine_01", 3, 1610704457000L),
new SensorExample.SensorReading("machine_01", 4, 1610704567000L),
new SensorExample.SensorReading("machine_01", 5, 1610704667000L),
new SensorExample.SensorReading("machine_01", 6, 1610704767000L)
);
WatermarkStrategy<SensorReading> strategy = WatermarkStrategy
.<SensorReading>forBoundedOutOfOrderness(Duration.ofSeconds(1))
.withTimestampAssigner((event, timestamp) -> event.timestamp);
DataStream<SensorExample.SensorReading> inputWithTimeStamp = input.assignTimestampsAndWatermarks(strategy);
DataStream<Tuple3<String, Long, Integer>> output = inputWithTimeStamp.keyBy(x -> x.key)
.window(TumblingEventTimeWindows.of(Time.minutes(1)))
.process(new MyWastefulMax());
output.print();
env.execute();
}
public static class MyWastefulMax extends ProcessWindowFunction<
SensorReading, // 输入类型
Tuple3<String, Long, Integer>, // 输出类型
String, // 键类型
TimeWindow> { // 窗口类型
@Override
public void process(
String key,
Context context,
Iterable<SensorReading> events,
Collector<Tuple3<String, Long, Integer>> out) {
int max = 0;
for (SensorReading event : events) {
max = Math.max(event.value, max);
}
out.collect(Tuple3.of(key, context.window().getEnd(), max));
}
}
public static class SensorReading {
public String key;
public Integer value;
public Long timestamp;
public SensorReading() {};
public SensorReading(String key, Integer value, Long timestamp) {
this.key = key;
this.value = value;
this.timestamp = timestamp;
};
public String toString() {
return this.key.toString() + ": value " + this.value.toString() + ": timestamp " + this.timestamp.toString();
};
}
}
Flink实现物联网流数据处理的一个demo
最新推荐文章于 2024-06-26 08:24:39 发布