|NO.Z.00062|——————————|BigDataEnd|——|Hadoop&kafka.V47|——|kafka.v47|日志存储索引|偏移量索引|时间戳索引.v02|
一、偏移量索引
### --- 位置索引保存在index文件中
~~~ log日志默认每写入4K(log.index.interval.bytes设定的),会写入一条索引信息到index文件中,
~~~ 因此索引文件是稀疏索引,它不会为每条日志都建立索引信息。
~~~ log文件中的日志,是顺序写入的,由message+实际offset+position组成
~~~ 索引文件的数据结构则是由相对offset(4byte)+position(4byte)组成,
~~~ 由于保存的是相对第一个消息的相对offset,只需要4byte就可以了,可以节省空间,
~~~ 在实际查找后还需要计算回实际的offset,这对用户是透明的。
~~~ 稀疏索引,索引密度不高,但是offset有序,二分查找的时间复杂度为O(lgN),
~~~ 如果从头遍历时间复杂度是O(N)。
### --- 示意图如下:
~~~ 偏移量索引由相对偏移量和物理地址组成。


### --- 可以通过如下命令解析.index 文件
[root@hadoop01 ~]# cd /opt/yanqi/servers/kafka/kafka-logs/tp_demo_05-0
[root@hadoop01 tp_demo_05-0]# kafka-run-class.sh kafka.tools.DumpLogSegments \
--files 00000000000000000000.index --print-data-log | head
~~~输出参数
Dumping 00000000000000000000.index
offset: 705 position: 16374
offset: 1385 position: 32740
offset: 2055 position: 49111
offset: 2737 position: 65476
offset: 3419 position: 81841
offset: 4101 position: 98206
offset: 4783 position: 114571
offset: 5465 position: 130936
offset: 6147 position: 147301
### --- 注意:offset 与 position 没有直接关系,因为会删除数据和清理日志。
[root@hadoop01 ~]# cd /opt/yanqi/servers/kafka/kafka-logs/tp_demo_05-0
[root@hadoop01 tp_demo_05-0]# kafka-run-class.sh kafka.tools.DumpLogSegments \
--files 00000000000007809212.log --print-data-log | head
~~~输出参数
Dumping 00000000000007809212.log
Starting offset: 7809212
baseOffset: 7809212 lastOffset: 7809817 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 0 CreateTime: 1632423527756 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 770421718
baseOffset: 7809818 lastOffset: 7810423 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 16359 CreateTime: 1632423527765 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 4083354828
baseOffset: 7810424 lastOffset: 7811029 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 32718 CreateTime: 1632423527773 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 70646303
baseOffset: 7811030 lastOffset: 7811635 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 49077 CreateTime: 1632423527782 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 2692280678
baseOffset: 7811636 lastOffset: 7812241 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 65436 CreateTime: 1632423527789 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 2658885119
baseOffset: 7812242 lastOffset: 7812847 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 81795 CreateTime: 1632423527797 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 1457480712
baseOffset: 7812848 lastOffset: 7813453 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 98154 CreateTime: 1632423527806 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 1539370327
baseOffset: 7813454 lastOffset: 7814059 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 0 isTransactional: false position: 114513 CreateTime: 1632423527817 isvalid: true size: 16359 magic: 2 compresscodec: NONE crc: 1834052643
### --- 在偏移量索引文件中,索引数据都是顺序记录 offset ,
~~~ 但时间戳索引文件中每个追加的索引时间戳必须大于之前追加的索引项,否则不予追加。
~~~ 在 Kafka 0.11.0.0 以后,消息元数据中存在若干的时间戳信息。
~~~ 如果 broker 端参数 log.message.timestamp.type 设置为 LogAppendTIme ,
~~~ 那么时间戳必定能保持单调增长。反之如果是 CreateTime 则无法保证顺序。
~~~ 注意:timestamp文件中的 offset 与 index 文件中的 relativeOffset 不是一一对应的。
~~~ 因为数据的写入是各自追加。
### --- 思考:如何查看偏移量为23的消息?
~~~ Kafka 中存在一个 ConcurrentSkipListMap 来保存在每个日志分段,通过跳跃表方式,
~~~ 定位到在00000000000000000000.index ,
~~~ 通过二分法在偏移量索引文件中找到不大于 23 的最大索引项,
~~~ 即offset 20 那栏,然后从日志分段文件中的物理位置为320 开始顺序查找偏移量为 23 的消息。
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
bdv013-kafka
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤