Flink MapState实例

MapState的方法和Java的Map的方法极为相似,所以上手相对容易。
常用的有如下:

  • get()方法获取值
  • put(),putAll()方法更新值
  • remove()删除某个key
  • contains()判断是否存在某个key
  • isEmpty() 判断是否为空

需求:统计每个用户的行为次数

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple3;
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.source.SourceFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Random;

public class MapStateTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.getConfig().setAutoWatermarkInterval(100);
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        DataStreamSource<Tuple3<String, String, Long>> tuple2DataStreamSource = env.addSource(new SourceFunction<Tuple3<String, String, Long>>() {
            boolean flag = true;

            @Override
            public void run(SourceContext<Tuple3<String, String, Long>> sourceContext) throws Exception {
                String[] s = {"张三", "王五", "李四", "秋英"};
                String[] s1 = {"登录", "退出", "加购", "够买"};
                while (flag) {
                    Thread.sleep(1000);
                    int i = new Random().nextInt(4);
                    sourceContext.collect(new Tuple3<String, String, Long>(s[i], s1[i], System.currentTimeMillis()));
                }
            }

            @Override
            public void cancel() {
                flag = false;
            }
        });

        SingleOutputStreamOperator<Tuple3<String, String, Long>> tuple3SingleOutputStreamOperator = tuple2DataStreamSource.assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple3<String, String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Long>>() {
                    @Override
                    public long extractTimestamp(Tuple3<String, String, Long> stringLongTuple2, long l) {
                        return stringLongTuple2.f2;
                    }
                }));

        tuple3SingleOutputStreamOperator.keyBy(new KeySelector<Tuple3<String, String, Long>, String>() {
            @Override
            public String getKey(Tuple3<String, String, Long> stringStringLongTuple3) throws Exception {
                return stringStringLongTuple3.f0;
            }
        }).process(new KeyedProcessFunction<String, Tuple3<String, String, Long>, String>() {

            MapState<String,Integer> mapState = null;

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                MapStateDescriptor<String,Integer> mapStateDescriptor = new MapStateDescriptor<String, Integer>("mapstate",String.class,Integer.class);
                mapState = getRuntimeContext().getMapState(mapStateDescriptor);
            }

            @Override
            public void processElement(Tuple3<String, String, Long> value, Context ctx, Collector<String> out) throws Exception {
                // 初始化
                if(!mapState.contains(value.f1)){
                    mapState.put(value.f1,1);
                }

                // "登录", "退出", "加购", "够买"
                mapState.put(value.f1,mapState.get(value.f1) + 1);

                out.collect(value.f0 + "[登录次数:" + mapState.get("登录") + ",退出次数:" + mapState.get("退出") + ",加购次数:" + mapState.get("加购") + ",够买次数:" + mapState.get("够买") + "]");

            }

            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                super.onTimer(timestamp, ctx, out);
            }

        }).print();

        env.execute("mapState");

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值