Flink教程(16) Keyed State状态管理之ReducingState使用案例 求最大值

系列文章

Flink教程(13) Keyed State状态管理之ValueState的使用 温差报警
Flink教程(14) Keyed State状态管理之MapState使用案例
Flink教程(15) Keyed State状态管理之ListState使用 ValueState实现
Flink教程(16) Keyed State状态管理之ReducingState使用案例 求最大值
Flink教程(17) Keyed State状态管理之AggregatingState使用案例 求平均值

一、ReducingState的方法

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

二、ReducingState的描述器

ReducingState的描述器和之前ValueState、ListState不同,它得和一个ReduceFunction配合使用。

所以得先实现一个ReduceFunction,例如下面这个计算最高温度的。

public static class MyMaxTemp implements ReduceFunction<SensorRecord> {

    @Override
    public SensorRecord reduce(SensorRecord value1, SensorRecord value2) throws Exception {
        return value1.getRecord() >= value2.getRecord() ? value1 : value2;
    }
}

ReducingStateDescriptor第2个参数传new SensorRecordUtils.MyMaxTemp()。
以后每次来一个数据,都会执行这个MyMaxTemp的reduce()方法。

//用ReducingStateDescriptor定义描述器
    ReducingStateDescriptor reducingStateDescriptor = new ReducingStateDescriptor(
            "max-temp-state",//id
            new SensorRecordUtils.MyMaxTemp(),//ReduceFunction
            SensorRecord.class);//状态里的值的类型

//获取ReducingState
reducingState = getRuntimeContext().getReducingState(reducingStateDescriptor);

三、程序主体

public class Test05_ReduceState {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //方便测试,设置为1
        env.setParallelism(1);

        DataStreamSource<String> source = env.socketTextStream(BaseConstant.URL, BaseConstant.PORT);

        /*
        设置watermark和指定时间属性
         */
        SingleOutputStreamOperator<SensorRecord> dataStream = source
                .map(new SensorRecordUtils.BeanMap());

        dataStream
                .keyBy(SensorRecord::getId)
                .process(new MyKeyedProcessFunction())
                .print();

        env.execute();
    }
}

四、KeyedProcessFunction处理类

public static class MyKeyedProcessFunction extends KeyedProcessFunction<String, SensorRecord, SensorRecord> {

    private transient ReducingState reducingState;

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);

        //用ReducingStateDescriptor定义描述器
        ReducingStateDescriptor reducingStateDescriptor = new ReducingStateDescriptor(
                "max-temp-state",//id
                new SensorRecordUtils.MyMaxTemp(),//ReduceFunction
                SensorRecord.class);//状态里的值的类型

        //获取ReducingState
        reducingState = getRuntimeContext().getReducingState(reducingStateDescriptor);
    }

    @Override
    public void processElement(SensorRecord value, Context ctx, Collector<SensorRecord> out) throws Exception {

        reducingState.add(value);

        out.collect((SensorRecord) reducingState.get());
    }
}

运行结果:
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑟 王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值