Flink——KeyedProcessFunction

package process;

import kb11.beans.SensorReading;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
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.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author Xulihua
 * @Date2021/7/7
 * @Description
 */
public class testProcess_KeyProcessFunction2 {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> inputStream = env.socketTextStream("192.168.107.103", 7777);

        SingleOutputStreamOperator<SensorReading> mapStream = inputStream.map(x -> {
            String[] split = x.split(",");
            return new SensorReading(split[0], Long.parseLong(split[1]), Double.parseDouble(split[2]));
        });

        mapStream.keyBy("id")
                .process(new MyKeyProcessFunctions2(10)).print("process");

        env.execute("testProcess_KeyProcessFunction2");
    }


    private static class MyKeyProcessFunctions2 extends KeyedProcessFunction<Tuple, SensorReading, String> {
        ValueState<Long> isTimeState = null;
        ValueState<Double> isLastTemp = null;
        private Integer time;

        public MyKeyProcessFunctions2(Integer second) {
            this.time=second;
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
            String warningStr="warning:温度连续上升";
            System.out.println(warningStr);
            isTimeState.clear();  // 触发成功也要clear
            out.collect(warningStr);
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            isTimeState = getRuntimeContext().getState(
                    new ValueStateDescriptor<Long>("is-timer", Long.class));
            isLastTemp = getRuntimeContext().getState(
                    new ValueStateDescriptor<Double>("is-temp", Double.class, Double.MIN_VALUE));
        }

        @Override
        public void close() throws Exception {
            isTimeState.clear();
            isLastTemp.clear();
        }

        // 当 10 内 连续输入的温度大于37 触发定时器 且只触发一次 ,如果10秒内温度下降则删除触发器
        @Override
        public void processElement(SensorReading value, Context ctx, Collector<String> out) throws Exception {
            Tuple currentKey = ctx.getCurrentKey();
            Long timestamp = ctx.timestamp();
            long l = ctx.timerService().currentProcessingTime();
            System.out.println(currentKey + "," + timestamp + "," + l);

            // 这边 isLastTemp 要更新 用来给下次输入的值 进行判断
            //isTimeState 在没有把定时器删除的时候 也要update  一旦删除之后就直接 clear 因为要重新开始判断了
            if (isTimeState.value() == null && value.getTemperature() > isLastTemp.value()) {
                ctx.timerService().registerProcessingTimeTimer(l + time*1000);
                isTimeState.update(l + time*1000);
                System.out.println("设置成功");
            } else if (isTimeState.value() != null && value.getTemperature() < isLastTemp.value()) {
                ctx.timerService().deleteProcessingTimeTimer(isTimeState.value());
                isTimeState.clear();   // 删除成功要clear
                System.out.println("删除成功");
            }
            isLastTemp.update(value.getTemperature());

        }
    }
}

在这里插入图片描述
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值