Flink 延迟流用法

public class DelayKeyStreamProcess extends KeyedProcessFunction<Integer, Iterable<HashMap<String, Object>>, Iterable<HashMap<String, Object>>> {
    private transient MapState<Long, List<Iterable<HashMap<String, Object>>>> mapState;

    /**
     * 延迟十分钟,十分钟应该是够了
     */
    private final Long delayTime = 1000 * 60 * 10L;

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        // 设置生命有效期为两天
        StateTtlConfig ttlConfig = StateTtlConfig
                .newBuilder(Time.hours(12))
                .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite)
                .setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
                .build();
        MapStateDescriptor<Long, List<Iterable<HashMap<String, Object>>>> cacheStorage = new MapStateDescriptor<Long, List<Iterable<HashMap<String, Object>>>>("CacheStorage", TypeInformation.of(new TypeHint<Long>() {
            @Override
            public TypeInformation<Long> getTypeInfo() {
                return super.getTypeInfo();
            }
        }), TypeInformation.of(new TypeHint<List<Iterable<HashMap<String, Object>>>>() {
            @Override
            public TypeInformation<List<Iterable<HashMap<String, Object>>>> getTypeInfo() {
                return super.getTypeInfo();
            }
        }));
        cacheStorage.enableTimeToLive(ttlConfig);
        mapState = getRuntimeContext().getMapState(cacheStorage);
    }

    @Override
    public void processElement(Iterable<HashMap<String, Object>> value, Context ctx, Collector<Iterable<HashMap<String, Object>>> out) throws Exception {
        long cacheTime = System.currentTimeMillis() + delayTime;
        ctx.timerService().registerProcessingTimeTimer(cacheTime);
        List<Iterable<HashMap<String, Object>>> cacheIterables = mapState.get(cacheTime);
        if (cacheIterables == null) {
            cacheIterables = new ArrayList<>();
        }
        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        Iterator<HashMap<String, Object>> iterator = value.iterator();

        while (iterator.hasNext()) {
            HashMap<String, Object> next = iterator.next();
            hashMaps.add(next);
        }
        cacheIterables.add(hashMaps);
        mapState.put(cacheTime, cacheIterables);
    }

    @Override
    public void onTimer(long timestamp, OnTimerContext ctx, Collector<Iterable<HashMap<String, Object>>> out) throws Exception {
        super.onTimer(timestamp, ctx, out);
        List<Iterable<HashMap<String, Object>>> cacheIterables = mapState.get(timestamp);
        for (Iterable<HashMap<String, Object>> cacheIterable : cacheIterables) {
            out.collect(cacheIterable);
        }
        mapState.remove(timestamp);
    }
}

整体思路:存到状态中去,同时注册定时器,用定时器捞出来。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值