Flink ListState 实例

ListState 介绍

  • get()方法获取值
  • add(IN value),addAll(List values)方法更新值
  • update(List values) 用新List 替换 原来的List
  • clear() 清空List,List还存在,但是没有元素

需求:每10s输出一次用户行为信息

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
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.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.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Iterator;
import java.util.Random;

public class ListStateTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.getConfig().setAutoWatermarkInterval(100l);
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        DataStreamSource<Tuple3<String, String, Long>> tuple3DataStreamSource = env.addSource(new SourceFunction<Tuple3<String, String, Long>>() {

            boolean flag = true;

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

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

        tuple3DataStreamSource.assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple3<String, String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(3))
        .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Long>>() {
            @Override
            public long extractTimestamp(Tuple3<String, String, Long> stringStringLongTuple3, long l) {
                return stringStringLongTuple3.f2;
            }
        })).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>() {
            ListState<String> listState = null;

            // 10s触发一次
            private final long interval = 10 * 1000l;

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                ListStateDescriptor<String> listStateDescriptor = new ListStateDescriptor<String>("listState",String.class);
                listState = getRuntimeContext().getListState(listStateDescriptor);
            }

            @Override
            public void processElement(Tuple3<String, String, Long> value, Context ctx, Collector<String> out) throws Exception {
                SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
                String str = format.format(value.f2) + " " + value.f1;
                listState.add(str);

                Long start = ctx.timestamp() - (ctx.timestamp() % interval);
                Long timer = start + interval;
                ctx.timerService().registerEventTimeTimer(timer);
            }

            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                super.onTimer(timestamp, ctx, out);
                StringBuilder stringBuilder = new StringBuilder();
                String str = null;
                Iterable<String> it = listState.get();
                if(it != null){
                    Iterator<String> iterator = it.iterator();
                    while(iterator.hasNext()){
                        stringBuilder.append(iterator.next()).append(",");
                    }

                    str = ctx.getCurrentKey() + "[" + stringBuilder.substring(0,stringBuilder.length() - 1) + "]";
                }
                out.collect(str);
            }
        }).print();
        env.execute("listState");

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值