- 本文作者: lemon
- 本文链接: https://lemon2013.github.io/2019/10/20/kafka入门之主题与日志/
- 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
主题
主题是存储消息的一个逻辑概念,可以简单理解为一类消息的集合,有使用方去创建。Kafka中的主题一般会有多个订阅者去消费对应主题的消息,也可以存在多个生产者往主题中写入消息。
每个主题又可以划分成多个分区,每个分区存储不同的消息。当消息添加至分区时,会为其分配一个位移offset(从0开始递增),并保证分区上唯一,消息在分区上的顺序由offset保证,即同一个分区内的消息是有序的,如下图所示
同一个主题的不同分区会分配在不同的节点上(broker),分区时保证Kafka集群具有水平扩展的基础。
以主题nginx_access_log
为例,分区数为3,如上图所示。分区在逻辑上对应一个日志(Log),物理上对应的是一个文件夹。
1 2 3 |
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/ |
消息写入分区时,实际上是将消息写入分区所在的文件夹中。日志又分成多个分片(Segment),每个分片由日志文件与索引文件组成,每个分区大小是有限的(在kafka集群的配置文件log.segment.bytes
配置,默认为1073741824byte,即1GB),当分片大小超过限制则会重新创建一个新的分片,外界消息的写入只会写入最新的一个分片(顺序IO)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
-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 |