Flink ReducingState 实例

ReducingState介绍

  • ReducingState是和ReduceFunction配合使用
  • get() 获取状态的值
  • add(IN value)方法添加一个元素,触发reduceFunction计算一次

需求:输出各设备10s内最大温度

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Random;

public class ReducingStateTest {
    public static void main(String[] args) throws Exception {
        // 需求:取10秒内最高的温度进行输出
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.getConfig().setAutoWatermarkInterval(100l);
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        DataStreamSource<Tuple3<String, Integer, Long>> tuple3DataStreamSource = env.addSource(new SourceFunction<Tuple3<String, Integer, Long>>() {
            boolean flag = true;

            @Override
            public void run(SourceContext<Tuple3<String, Integer, Long>> ctx) throws Exception {
                String[] str = {"水阀1", "水阀2", "水阀3"};
                while (flag) {
                    int i = new Random().nextInt(3);
                    // 温度
                    int temperature = new Random().nextInt(100);
                    Thread.sleep(1000l);
                    // 设备号、温度、事件时间
                    ctx.collect(new Tuple3<String, Integer, Long>(str[i], temperature, System.currentTimeMillis()));
                }
            }

            @Override
            public void cancel() {
                flag = false;
            }
        });

        tuple3DataStreamSource.assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple3<String, Integer, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(2))
        .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, Integer, Long>>() {
            @Override
            public long extractTimestamp(Tuple3<String, Integer, Long> stringIntegerLongTuple3, long l) {
                return stringIntegerLongTuple3.f2;
            }
        })).keyBy(new KeySelector<Tuple3<String, Integer, Long>, String>() {
            @Override
            public String getKey(Tuple3<String, Integer, Long> stringIntegerLongTuple3) throws Exception {
                return stringIntegerLongTuple3.f0;
            }
        }).process(new KeyedProcessFunction<String, Tuple3<String, Integer, Long>, String>() {
            Long interval = 10 * 1000l;
            ReducingState<Integer> reducingState = null;
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                ReducingStateDescriptor<Integer> reducingStateDescriptor = new ReducingStateDescriptor<Integer>("reducingState",new Max(),Integer.class);
                reducingState = getRuntimeContext().getReducingState(reducingStateDescriptor);
            }

            @Override
            public void processElement(Tuple3<String, Integer, Long> value, Context ctx, Collector<String> out) throws Exception {
                // 注册定时器10s触发一次,相同定时器重复注册会忽略
                Long statrTimestamp = ctx.timestamp() - (ctx.timestamp() % interval);
                Long timerTimestamp = statrTimestamp + interval;
                ctx.timerService().registerEventTimeTimer(timerTimestamp);

                // 加入新元素
                reducingState.add(value.f1);
            }

            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                super.onTimer(timestamp, ctx, out);
                out.collect("[" + ctx.getCurrentKey() + "] " + "10s内最大温度是" + reducingState.get());
                reducingState.clear();
            }

        }).print();

        env.execute("reduceState");


    }

    private static class Max implements ReduceFunction<Integer> {

        @Override
        public Integer reduce(Integer integer, Integer t1) throws Exception {
            return Math.max(integer,t1);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值