Flink的背压现象

通过构建A->B->C三个算子,为方便测试断开算子间的链接·env.disableOperatorChaining(),在B算子中增加Thread.sleep降低其数据处理能力。观察现象如下:
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
案例代码

package com.dayue;

import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
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.source.RichParallelSourceFunction;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 观察反压情况
 * A->B->C
 * 假设B点计算压力大
 * 则B点显示为红色,busy 100%
 * A点背压点,backpressure比较高
 *
 */

public class back_pressure_demo1 {

    @SneakyThrows
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        env.disableOperatorChaining();

        DataStreamSource<String> source = env.addSource(new demo2());

        SingleOutputStreamOperator<JSONObject> map = source.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value;
            }
        }).map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                Thread.sleep(1000);
                return value + "," + System.currentTimeMillis();
            }
        }).map(new RichMapFunction<String, JSONObject>() {
            @Override
            public JSONObject map(String value) throws Exception {
                JSONObject json = new JSONObject();
                String[] split = value.split(",");
                json.put("province", split[0]);
                json.put("id", split[1]);
                json.put("ord", split[2]);
                json.put("ts", split[3]);
                return json;
            }
        });

        map.print();

        env.execute(back_pressure_demo1.class.getSimpleName());

    }


    public static class demo2 extends RichParallelSourceFunction<String> {

        @Override
        public void run(SourceContext<String> sourceContext) throws Exception {

            AtomicInteger i = new AtomicInteger(0);
            String[] province = {"河南省", "广东省", "江苏省", "北京市", "重庆市"};

            while (true) {
                sourceContext.collect(province[RandomUtils.nextInt(0, 5)] + "," + RandomStringUtils.randomAlphabetic(5) + "," + i.getAndIncrement());
                Thread.sleep(10);
            }

        }

        @Override
        public void cancel() {

        }
    }



}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值