Flink实现物联网流数据处理的一个demo

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();
        };
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值