• Flink状态之KeyedVauleState


    1、主类

    package com.example.demo.flink;
    
    import com.example.demo.flink.impl.CountAverageWithValueState;
    import org.apache.flink.api.common.functions.FlatMapFunction;
    import org.apache.flink.api.common.functions.MapFunction;
    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 TestKeyedValueStateMain {
        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 CountAverageWithValueState())
                    .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.ValueState;
    import org.apache.flink.api.common.state.ValueStateDescriptor;
    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.util.Collector;
    
    /**
     *  ValueState<T> :这个状态为每一个 key 保存一个值
     *      value() 获取状态值
     *      update() 更新状态值
     *      clear() 清除状态
     *
     *      IN,输入的数据类型
     *      OUT:数据出的数据类型
     */
    public class CountAverageWithValueState
            extends RichFlatMapFunction<Tuple2<Long, Long>, Tuple2<Long, Double>> {
    
        private ValueState<Tuple2<Long,Long>> countAndSum;
    
        /**注册状态,并初始化*/
        @Override
        public void open(Configuration parameters) throws Exception {
            ValueStateDescriptor descriptor = new ValueStateDescriptor<Tuple2<Long, Long>>("valueDescriptor",Types.TUPLE(Types.LONG,Types.LONG));
            countAndSum = getRuntimeContext().getState(descriptor);
    
        }
    
        @Override
        public void flatMap(Tuple2<Long, Long> element, Collector<Tuple2<Long, Double>> collector) throws Exception {
            //拿取当前key的状态值
            Tuple2<Long, Long> currentState = countAndSum.value();
            //如果是空,则初始化
            if(currentState == null){
                currentState = Tuple2.of(0L,0L);
            }
            //不为空,则计算,f0为key出现的次数 , f1为key对应的value叠加值
            currentState.f0 +=1;
            currentState.f1 += element.f1;
    
            countAndSum.update(currentState);
    
            if(currentState.f0 >=3){
                double avg = (double)currentState.f1 / currentState.f0;
                collector.collect(Tuple2.of(element.f0,avg));
                countAndSum.clear();
            }
        }
    }
  • 相关阅读:
    爬虫必看,每日JS逆向之爱奇艺密码加密,今天你练了吗?
    每日JS逆向练习之斗鱼登录密码加密,今天你练了吗?
    兄弟们,我打算抠100个网站JS加密代码召唤,一个也跑不掉,这次轮到小虎牙
    这个爬虫JS逆向加密任务,你还不来试试?逆向入门级,适合一定爬虫基础的人
    兄弟,你爬虫基础这么好,需要研究js逆向了,一起吧(有完整JS代码)
    四十一:快速排序(递归)
    第四十次发博不知道用什么标题好
    第三十九次发博不知道发什么好
    第三十八次发博不知道用什么标题好
    第三十七次发博不知道用什么标题好
  • 原文地址:https://www.cnblogs.com/ywjfx/p/14228549.html
Copyright © 2020-2023  润新知