1 主题 - Topic
主题是存储消息的一个逻辑概念,可以简单理解为一类消息的集合,由使用方创建。Kafka中的主题一般会有多个订阅者去消费对应主题的消息,也可以存在多个生产者往主题中写入消息,如下图所示:
此外,每个主题可以划分成多个分区,每个分区存储不同的消息。当消息添加至分区时,会为其分配一个位移offset(从0开始递增),并保证分区上唯一,消息在分区上的顺序由offset保证,即同一个分区内的消息是有序的,如下图所示:
同一个主题的不同分区会分配在不同的节点上(broker),分区是保证Kafka集群具有水平扩展的基础。
2 日志 - Log
以主题nginx_access_log
为例,分区数为3,如下图所示:
分区在逻辑上对应一个日志(Log),物理上对应的是一个文件夹。
drwxr-xr-x 2 root root 4096 10月 11 20:07 nginx_access_log-0/
drwxr-xr-x 2 root root 4096 10月 11 20:07 nginx_access_log-1/
drwxr-xr-x 2 root root 4096 10月 11 20:07 nginx_access_log-2/
消息写入分区时,实际上是将消息写入分区所在的文件夹中。
3 分片 - Segment
日志分成多个分片(Segment),每个分片由日志文件与索引文件组成,每个分片大小是有上限的(在kafka集群的配置文件log.segment.bytes
配置,默认为1073741824byte,即1GB),当分片大小超过限制则会重新创建一个新的分片,外部消息的写入只会写入到最新的一个分片中(顺序IO)。
-rw-r--r-- 1 root root 1835920 10月 11 19:18 00000000000000000000.index
-rw-r--r-- 1 root root 1073741684 10月 11 19:18 00000000000000000000.log
-rw-r--r-- 1 root root 2737884 10月 11 19:18 00000000000000000000.timeindex
-rw-r--r-- 1 root root 1828296 10月 11 19:30 00000000000003257573.index
-rw-r--r-- 1 root root 1073741513 10月 11 19:30 00000000000003257573.log
-rw-r--r-- 1 root root 2725512 10月 11 19:30 00000000000003257573.timeindex
-rw-r--r-- 1 root root 1834744 10月 11 19:42 00000000000006506251.index
-rw-r--r-- 1 root root 1073741771 10月 11 19:42 00000000000006506251.log
-rw-r--r-- 1 root root 2736072 10月 11 19:42 00000000000006506251.timeindex
-rw-r--r-- 1 root root 1832152 10月 11 19:54 00000000000009751854.index
-rw-r--r-- 1 root root 1073740984 10月 11 19:54 00000000000009751854.log
-rw-r--r-- 1 root root 2731572 10月 11 19:54 00000000000009751854.timeindex
-rw-r--r-- 1 root root 1808792 10月 11 20:06 00000000000012999310.index
-rw-r--r-- 1 root root 1073741584 10月 11 20:06 00000000000012999310.log
-rw-r--r-- 1 root root 10 10月 11 19:54 00000000000012999310.snapshot
-rw-r--r-- 1 root root 2694564 10月 11 20:06 00000000000012999310.timeindex
-rw-r--r-- 1 root root 10485760 10月 11 20:09 00000000000016260431.index
-rw-r--r-- 1 root root 278255892 10月 11 20:09 00000000000016260431.