CC00049.flink——|Hadoop&Flink.V04|——|Flink.v04|Flink Connector|kafka|源码理解|

本文主要探讨Flink连接器与Kafka的源码实现,详细解析了Flink在处理Kafka数据流时的内部工作机制。
摘要由CSDN通过智能技术生成
一、源码提取说明
### --- 直接启动consumer

~~~     # 源码提取说明:FlinkKafkaConsumerBase.java
~~~     # 第160~161行
    /** The startup mode for the consumer (default is {@link StartupMode#GROUP_OFFSETS}). */
    private StartupMode startupMode = StartupMode.GROUP_OFFSETS;
### --- 该枚举类型有5个值:

~~~     GROUP_OFFSETS:从保存在zookeeper或者是Kafka broker的对应消费者组提交的offset开始消费,
~~~     这个是默认的配置
~~~     EARLIEST:尽可能从最早的offset开始消费
~~~     LATEST:从最近的offset开始消费
~~~     TIMESTAMP:从用户提供的timestamp处开始消费
~~~     SPECIFIC_OFFSETS:从用户提供的offset处开始消费
### --- 根据startup mode,获取从哪个地方开始消费。然后,partition discoverer就会拉取初始分区的数据

~~~     # 源码提取说明:FlinkKafkaConsumerBase.java
~~~     # 第583~605行
            LOG.info("Consumer subtask {} will start reading {} partitions with offsets in restored state: {}",
                getRuntimeContext().getIndexOfThisSubtask(), subscribedPartitionsToStartOffsets.size(), subscribedPartitionsToStartOffsets);
        } else {
            // use the partition discoverer to fetch the initial seed partitions,
            // and set their initial offsets depending on the startup mode.
            // for SPECIFIC_OFFSETS and TIMESTAMP modes, we set the specific offsets now;
            // for other modes (EARLIEST, LATEST, and GROUP_OFFSETS), the offset is lazily determined
            // when the partition is actually read.
            switch (startupMode) {
                case SPECIFIC_OFFSETS:
                    if (specificStartupOffsets == null) {
                        throw new IllegalStateException(
                            "Startup mode for the consumer set to " + StartupMode.SPECIFIC_OFFSETS +
                                ", but no specific offsets were specified.");
                    }

                    for (KafkaTopicPartition seedPartition : allPartitions) {
                        Long specificOffset = specificStartupOffsets.get(seedPartition);
                        if (specificOffset != null) {
                            // since the specified offsets represent the next record to read, we subtract
                            // it by one so that the initial state of the consumer will be correct
                            subscribedPartitionsToStartOffsets.put(seedPartition, specificOffset - 1);
                        } else {
    
~~~     如果startup模式为SPECIFIC_OFFSETS:
~~~     异常情况:如果没有配置具体从哪个offset开始消费
### --- 正常情况:获取每个分区指定的消费起始offset

Long specificOffset = specificStartupOffsets.get(seedPartition);
~~~     # 源码提取说明:FlinkKafkaConsumerBa
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值