Flink状态之MapState

1、主类

package com.example.demo.flink;

import com.example.demo.flink.impl.CountAverageWithMapState;
import com.example.demo.flink.impl.CountAverageWithValueState;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


/**
 * @program: demo
 * @description: valuestate
 * @author: yang
 * @create: 2020-12-28 15:46
 */
public class TestKeyedMapStateMain {
    public static void main(String[] args) throws  Exception{
        //获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        //StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度
        env.setParallelism(16);
        //获取数据源
        DataStreamSource<Tuple2<Long, Long>> dataStreamSource =
                env.fromElements(
                        Tuple2.of(1L, 3L),
                        Tuple2.of(1L, 7L),
                        Tuple2.of(2L, 4L),
                        Tuple2.of(1L, 5L),
                        Tuple2.of(2L, 2L),
                        Tuple2.of(2L, 6L));


        // 输出:
        //(1,5.0)
        //(2,4.0)
        dataStreamSource
                .keyBy(0)
                .flatMap(new CountAverageWithMapState())
                .print();


        env.execute("TestStatefulApi");
    }

}

2、处理实现类

package com.example.demo.flink.impl;

/**
 * @program: demo
 * @description: valuestate
 * @author: yang
 * @create: 2020-12-28 16:26
 */

import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.shaded.guava18.com.google.common.collect.Lists;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 *  ValueState<T> :这个状态为每一个 key 保存一个值
 *      value() 获取状态值
 *      update() 更新状态值
 *      clear() 清除状态
 *
 *      IN,输入的数据类型
 *      OUT:数据出的数据类型
 */
public class CountAverageWithMapState
        extends RichFlatMapFunction<Tuple2<Long, Long>, Tuple2<Long, Double>> {

    private MapState<String,Long> mapState;

    /***状态初始化*/
    @Override
    public void open(Configuration parameters) throws Exception {

        MapStateDescriptor descriptor = new MapStateDescriptor("MapDescriptor",String.class,String.class);
        mapState = getRuntimeContext().getMapState(descriptor);

    }

    @Override
    public void flatMap(Tuple2<Long, Long> element, Collector<Tuple2<Long, Double>> collector) throws Exception {

        //获取状态
        mapState.put(UUID.randomUUID().toString(),element.f1);
        List<Long> allEles = Lists.newArrayList(mapState.values());

        if(allEles.size() >=3){
            long count = 0;
            long sum = 0;
            for (Long ele:allEles) {
                count ++;
                sum += ele;
            }
            double avg = (double) sum/count;
            collector.collect(Tuple2.of(element.f0,avg));
            mapState.clear();
        }
    }


}

 

posted @ 2021-01-04 10:41  小白啊小白,Fighting  阅读(6792)  评论(0编辑  收藏  举报