Flinkチュートリアル(16)最大値を見つけるためのキー付き状態状態管理のReducingStateユースケース

一連の記事

Flinkチュートリアル(13)温度差アラームを使用したKeyed State状態管理ValueState
Flinkチュートリアル(14)Keyed State状態管理MapStateユースケース
Flinkチュートリアル(15)ValueStateを使用して
Flinkチュートリアルを実装するKeyed State状態管理ListState (16)Keyed State State The ReducingState最大値を見つけるための管理のユースケース
Flinkチュートリアル(17)平均値を見つけるためのKeyedState状態管理のAggregatingStateユースケース

1つは、ReducingStateのメソッドです。

  • ReducingStateはReduceFunctionと組み合わせて使用​​されます
  • get()状態の値を取得します
  • add(IN value)メソッドは要素を追加し、reduceFunctionをトリガーして1回計算します
    ここに画像の説明を挿入

第二に、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番目のパラメーターには、新しいSensorRecordUtils.MyMaxTemp()が渡されます。
将来的には、データが入るたびに、このMyMaxTempのreduce()メソッドが実行されます。

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

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

3、プログラムの本体

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

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

演算結果:
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/winterking3/article/details/115125313