flink 应用 keyby算子,增加数据迟延

Flink 添加 keyby 算子,增加数据

  		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        //env.setBufferTimeout(0);
      	env.addSource(new SourceFunction<ABean>() {

            @Override
            public void run(SourceContext<ABean> ctx) throws Exception {
                while(true){
                	ctx.collect(ABean.builder().key("aaa").value(System.currentTimeMillis()).build());
                    Thread.sleep(100);
                }

            }

            @Override
            public void cancel() {

            }
        }).keyBy(new KeySelector<ABean, String>() {
            @Override
            public String getKey(ABean value) throws Exception {
                return value.getKey();
            }
        }).map(new RichMapFunction<ABean, ABean>() {
            @Override
            public ABean map(ABean value) throws Exception {

                log.warn("延迟时间:" + (System.currentTimeMillis() - value.getValue()));

                return value;
            }
        });
        env.execute("keyby 时间延迟");

在这里插入图片描述

The reason for this delay is that by adding that keyBy you are forcing a network shuffle along with serialization/deserialization. The reason the delay is so variable is because of the network buffering.

https://stackoverflow.com/questions/56819799/flink-keyby-adding-delay-how-can-i-reduce-this-latency

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
//
env.setBufferTimeout(0);
/**
     * Sets the maximum time frequency (milliseconds) for the flushing of the output buffers. By
     * default the output buffers flush frequently to provide low latency and to aid smooth
     * developer experience. Setting the parameter can result in three logical modes:
     *
     * <ul>
     *   <li>A positive integer triggers flushing periodically by that integer
     *   <li>0 triggers flushing after every record thus minimizing latency
     *   <li>-1 triggers flushing only when the output buffer is full thus maximizing throughput
     * </ul>
     *
     * @param timeoutMillis The maximum time between two output flushes.
     */
    public StreamExecutionEnvironment setBufferTimeout(long timeoutMillis) {
        if (timeoutMillis < -1) {
            throw new IllegalArgumentException("Timeout of buffer must be non-negative or -1");
        }

        this.bufferTimeout = timeoutMillis;
        return this;
    }

在这里插入图片描述

参考:
https://www.modb.pro/db/116828
https://stackoverflow.com/questions/56819799/flink-keyby-adding-delay-how-can-i-reduce-this-latency
https://flink.apache.org/2019/06/05/flink-network-stack.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值