源码阅读-Record

源码阅读-Record

Kafka消息格式相关的代码位于clients工程下的/java/org/apache/kafka/common/record包下。

在这里插入图片描述

类之间的关系图如下所示:

在这里插入图片描述

1. DefaultRecord(默认消息)

类中的消息格式定义(org.apache.kafka.common.record.DefaultRecord):

在这里插入图片描述

字段说明
sizeInBytes消息总长度字段
attributes消息属性字段
offset位移增量
timestamp时间戳增量
sequence用于支持消息的幂等性
keykey值
valuevalue值
headers消息头部属性

kafka将record组装成buffer进行输出(org.apache.kafka.common.record.DefaultRecord#writeTo):

/**
 * Write the record to `out` and return its size.
 */
public static int writeTo(DataOutputStream out,
                          int offsetDelta,
                          long timestampDelta,
                          ByteBuffer key,
                          ByteBuffer value,
                          Header[] headers) throws IOException {
    // 消息总数
    int sizeInBytes = sizeOfBodyInBytes(offsetDelta, timestampDelta, key, value, headers);
    ByteUtils.writeVarint(sizeInBytes, out);

    // 属性
    byte attributes = 0; // there are no used record attributes at the moment
    out.write(attributes);

    // 时间增量
    ByteUtils.writeVarlong(timestampDelta, out);
    // 位移增量
    ByteUtils.writeVarint(offsetDelta, out);

    // key
    if (key == null) {
        ByteUtils.writeVarint(-1, out);
    } else {
        // key size
        int keySize = key.remaining();
        ByteUtils.writeVarint(keySize, out);
        Utils.writeTo(out, key, keySize);
    }

    // value
    if (value == null) {
        ByteUtils.writeVarint(-1, out);
    } else {
        // value size
        int valueSize = value.remaining();
        ByteUtils.writeVarint(valueSize, out);
        Utils.writeTo(out, value, valueSize);
    }

    // headers
    if (headers == null)
        throw new IllegalArgumentException("Headers cannot be null");

    // headers个数
    ByteUtils.writeVarint(headers.length, out);
 
    for (Header header : headers) {
        // header key
        String headerKey = header.key();
        if (headerKey == null)
            throw new IllegalArgumentException("Invalid null header key found in headers");

        byte[] utf8Bytes = Utils.utf8(headerKey);
        // header key length
        ByteUtils.writeVarint(utf8Bytes.length, out);
        out.write(utf8Bytes);

        // header value
        byte[] headerValue = header.value();
        if (headerValue == null) {
            ByteUtils.writeVarint(-1, out);
        } else {
            // header value length
            ByteUtils.writeVarint(headerValue.length, out);
            out.write(headerValue);
        }
    }

    return ByteUtils.sizeOfVarint(sizeInBytes) + sizeInBytes;
}

消息格式可以用如下表示:

在这里插入图片描述

2. DefaultRecordBatch(默认消息集合)

类中的消息集合格式的定义(org.apache.kafka.common.record.DefaultRecordBatch):

在这里插入图片描述

消息批次格式可以用如下表示:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yanko24

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

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

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

打赏作者

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

抵扣说明:

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

余额充值