卡夫卡的藏书阁【Book6】- Kafka 实作新增 Topic

IT 这行真的学无止境,理想上可以主动追技术,比被技术追上好
“I am free and that is why I am lost.”
― Franz Kafka

我是自由的,那就是我迷失的原因。


今天会实际带大家创建一个 topic,接着用 producer 去新增资料,再用 consumer 去取得资料,最後是将这个 topic 删除。

Step 1: 新增人生中第一个 Topic

首先,第一步永远是先打开一个终端机

  • 移动到 Kafka 的资料夹 cd /usr/local/Cellar/kafka/2.8.0/libexec
  • topic 在这里就像是档案系统里面的资料夹,你要存放档案前,必须得先创一个资料夹(topic)
  • 使用 Kafka 的 kafka-topic.sh 新增 topic
$ kafka-topics --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic first_topic

Created topic first_topic

topic 命名需要注意:
first_topicfirst.topic 视为相同的,官方建议是选择其中一种用法统一使用,不要同时并用
如果你混用底线跟点,还是可以成功创建 topic 但会跳出警报:

$ kafka-topics --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic first_topic.123

WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic first_topic.123.

Kafka的每个指令都有很多可选选项(Option),输入指令即可查看
可以看到 topic 相关的指令很多,接下来几天会尽量为大家用实际例子下去介绍/images/emoticon/emoticon30.gif

$ kafka-topics
Create, delete, describe, or change a topic.
Option                                   Description
------                                   -----------
--alter                                  Alter the number of partitions,
                                           replica assignment, and/or
                                           configuration for the topic.
--at-min-isr-partitions                  if set when describing topics, only
                                           show partitions whose isr count is
                                           equal to the configured minimum. Not
                                           supported with the --zookeeper
                                           option.
--bootstrap-server <String: server to    REQUIRED: The Kafka server to connect
  connect to>                              to. In case of providing this, a
                                           direct Zookeeper connection won't be
                                           required.
--command-config <String: command        Property file containing configs to be
  config property file>                    passed to Admin Client. This is used
                                           only with --bootstrap-server option
                                           for describing and altering broker
                                           configs.
--config <String: name=value>            A topic configuration override for the
                                           topic being created or altered. The
                                           following is a list of valid
                                           configurations:
                                         	cleanup.policy
                                         	compression.type
                                         	delete.retention.ms
                                         	file.delete.delay.ms
                                         	flush.messages
                                         	flush.ms
                                         	follower.replication.throttled.
                                           replicas
                                         	index.interval.bytes
                                         	leader.replication.throttled.replicas
                                         	max.compaction.lag.ms
                                         	max.message.bytes
                                         	message.downconversion.enable
                                         	message.format.version
                                         	message.timestamp.difference.max.ms
                                         	message.timestamp.type
                                         	min.cleanable.dirty.ratio
                                         	min.compaction.lag.ms
                                         	min.insync.replicas
                                         	preallocate
                                         	retention.bytes
                                         	retention.ms
                                         	segment.bytes
                                         	segment.index.bytes
                                         	segment.jitter.ms
                                         	segment.ms
                                         	unclean.leader.election.enable
                                         See the Kafka documentation for full
                                           details on the topic configs. It is
                                           supported only in combination with --
                                           create if --bootstrap-server option
                                           is used (the kafka-configs CLI
                                           supports altering topic configs with
                                           a --bootstrap-server option).
--create                                 Create a new topic.
--delete                                 Delete a topic
--delete-config <String: name>           A topic configuration override to be
                                           removed for an existing topic (see
                                           the list of configurations under the
                                           --config option). Not supported with
                                           the --bootstrap-server option.
--describe                               List details for the given topics.
--disable-rack-aware                     Disable rack aware replica assignment
--exclude-internal                       exclude internal topics when running
                                           list or describe command. The
                                           internal topics will be listed by
                                           default
--force                                  Suppress console prompts
--help                                   Print usage information.
--if-exists                              if set when altering or deleting or
                                           describing topics, the action will
                                           only execute if the topic exists.
--if-not-exists                          if set when creating topics, the
                                           action will only execute if the
                                           topic does not already exist.
--list                                   List all available topics.
--partitions <Integer: # of partitions>  The number of partitions for the topic
                                           being created or altered (WARNING:
                                           If partitions are increased for a
                                           topic that has a key, the partition
                                           logic or ordering of the messages
                                           will be affected). If not supplied
                                           for create, defaults to the cluster
                                           default.
--replica-assignment <String:            A list of manual partition-to-broker
  broker_id_for_part1_replica1 :           assignments for the topic being
  broker_id_for_part1_replica2 ,           created or altered.
  broker_id_for_part2_replica1 :
  broker_id_for_part2_replica2 , ...>
--replication-factor <Integer:           The replication factor for each
  replication factor>                      partition in the topic being
                                           created. If not supplied, defaults
                                           to the cluster default.
--topic <String: topic>                  The topic to create, alter, describe
                                           or delete. It also accepts a regular
                                           expression, except for --create
                                           option. Put topic name in double
                                           quotes and use the '\' prefix to
                                           escape regular expression symbols; e.
                                           g. "test\.topic".
--topics-with-overrides                  if set when describing topics, only
                                           show topics that have overridden
                                           configs
--unavailable-partitions                 if set when describing topics, only
                                           show partitions whose leader is not
                                           available
--under-min-isr-partitions               if set when describing topics, only
                                           show partitions whose isr count is
                                           less than the configured minimum.
                                           Not supported with the --zookeeper
                                           option.
--under-replicated-partitions            if set when describing topics, only
                                           show under replicated partitions
--version                                Display Kafka version.
--zookeeper <String: hosts>              DEPRECATED, The connection string for
                                           the zookeeper connection in the form
                                           host:port. Multiple hosts can be
                                           given to allow fail-over.

查看刚刚创建 topic 的详细资讯

$ kafka-topics --describe --topic first_topic --bootstrap-server localhost:9092
Topic: first_topic	TopicId: Wvh3sCCoQ8mnfkKtDAiUIQ	PartitionCount: 1	ReplicationFactor: 1	Configs: segment.bytes=1073741824
	Topic: first_topic	Partition: 0	Leader: 1	Replicas: 1	Isr: 1

这里可以看到只有一行,是因为创建只有设定生成一个partition,明天会介绍创建多个 partition 的状况

  • Partition: 0 => 代表该 Topic 编号0的partition
  • Leader: 0 => 表示 partition 0的 leader 位於哪个 broker,因为只有一个 broker,所以就是在 Broker 0
  • Replicas: 0 => 表示有几个备份
  • Isr: 1 => 表示在哪几个 broker 可以找到这个 partition

列出所有已经创建的 topics
指令:kafka-topics --list --bootstrap-server localhost:9092

$ kafka-topics --list --bootstrap-server localhost:9092
__consumer_offsets
connect-configs
connect-offsets
connect-status
first_topic
first_topic.123

Step 2: Producer 塞资料到 topic 中

Producer:在topic中输入资料,利用 kafka-console-producer 指令会进入互动模式,可以手动输入资料给 topic,如果要离开输入 ctrl+c 即可

生产者这边很单纯只有两个参数说明如下:

  • --topic:指定在哪个 topic 新增资料
  • --bootstrap-server:指定要将资料塞到哪个 broker server
$ kafka-console-producer --topic first_topic --bootstrap-server localhost:9092
>test123
>test456
>

Step 3: Consumer 从 topic 中取得资料

Consumer:读取指定 broker 的 topic 中的资料,一样 ctrl+c 可离开

消费者这边有三个参数介绍如下:

  • --topic:指定要消费的 topic
  • --from-beginning:从该 topic 第一笔开始消费
  • --bootstrap-server:指定要从哪个 broker server 消费,通常都会有多个 broker server
$ kafka-console-consumer --topic first_topic --from-beginning --bootstrap-server localhost:9092
test123
test456
^CProcessed a total of 2 messages

Step 4: 删除 Topic

建立完 topic,如果需要删除 topic,删除动作是由 TopicDeletionManager 负责,删除的前提是设定档的 delete.topic.enable 设定为 true

4.1 新增一个 id 为100的 broker

$ kafka-server-start /usr/local/etc/kafka/server-1.properties \
--override delete.topic.enable=true \
--override broker.id=100 \
--override log.dirs=/tmp/kafka-logs-100 \
--override port=9195 \
--override listeners=PLAINTEXT://localhost:9195

4.2 新增 topic wait-for-remove

$ kafka-topics --bootstrap-server localhost:9195 \
--create \
--topic wait-for-remove \
--partitions 1 \
--replication-factor 1
Created topic “wait-for-remove”.

4.3 用指令 kafka-topics.sh --list 列出所有的 topic

$ kafka-topics --zookeeper localhost:2181 --list
__consumer_offsets
wait-for-remove

4.4 用指令 kafka-topics.sh --describe 查询 topic wait-for-remove 的资讯

$ kafka-topics --zookeeper localhost:2181 --describe --topic wait-for-remove

Topic:wait-for-remove	PartitionCount:1	ReplicationFactor:1	Configs:
	Topic: wait-for-remove	Partition: 0	Leader: 100	Replicas: 100	Isr: 100

=> 这边可以看到 broker 100 是 topic wait-for-remove 唯一一个 partition 0 的 partition leader

4.5 将 broker 100 停止,接着启动 broker 200

$ kafka-server-start /usr/local/etc/kafka/server-1.properties \
--override delete.topic.enable=true \
--override broker.id=200 \
--override log.dirs=/tmp/kafka-logs-200 \
--override port=9196 \
--override listeners=PLAINTEXT://localhost:9196

4.6 用 kafka-topics.sh --delete把 topic wait-for-remove 删掉

$ kafka-topics --zookeeper localhost:2181 --delete --topic wait-for-remove

Topic wait-for-remove is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

4.7 再次用指令 kafka-topics.sh --list 列出所有的 topic

$ kafka-topics --zookeeper localhost:2181 --list
__consumer_offsets
wait-for-remove - marked for deletion

从这里你可以看到kafka-topics.sh —delete只有在 topic 的 leader broker还活着的时候、而且delete.topic.enable是开启的情况下才会执行,但是我们刚刚已经将 broker 100 关闭,这个等待被记录的 topic 会被记录在 zookeeper 中。

4.8 利用 Zookeeper CLI 查看待删除的 topics

zkCli -server localhost:2181 
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /admin/delete_topics
[wait-for-remove]

只要 broker 100 不启动,topic wait-for-remove 就会维持这个标注的状态

4.9 重新启动 broker 100

$ kafka-server-start /usr/local/etc/kafka/server-1.properties \
--override delete.topic.enable=true \
--override broker.id=100 \
--override log.dirs=/tmp/kafka-logs-100 \
--override port=9195 \
--override listeners=PLAINTEXT://localhost:9195

可以看到 log info 逐步地将 topic wait-for-remove 删除掉

[2021-09-01 15:45:06,554] INFO [Partition wait-for-remove-0 broker=100] Log loaded for partition wait-for-remove-0 with initial high watermark 0 (kafka.cluster.Partition)
[2021-09-01 15:45:06,565] INFO [GroupCoordinator 100]: Removed 0 offsets associated with deleted partitions: wait-for-remove-0. (kafka.coordinator.group.GroupCoordinator)
[2021-09-01 15:45:06,567] INFO [GroupCoordinator 100]: Removed 0 offsets associated with deleted partitions: wait-for-remove-0. (kafka.coordinator.group.GroupCoordinator)
[2021-09-01 15:45:06,570] INFO [ReplicaFetcherManager on broker 100] Removed fetcher for partitions Set(wait-for-remove-0) (kafka.server.ReplicaFetcherManager)
[2021-09-01 15:45:06,570] INFO [ReplicaAlterLogDirsManager on broker 100] Removed fetcher for partitions Set(wait-for-remove-0) (kafka.server.ReplicaAlterLogDirsManager)
[2021-09-01 15:45:06,580] INFO [ReplicaFetcherManager on broker 100] Removed fetcher for partitions Set(wait-for-remove-0) (kafka.server.ReplicaFetcherManager)
[2021-09-01 15:45:06,580] INFO [ReplicaAlterLogDirsManager on broker 100] Removed fetcher for partitions Set(wait-for-remove-0) (kafka.server.ReplicaAlterLogDirsManager)
[2021-09-01 15:45:06,587] INFO Log for partition wait-for-remove-0 is renamed to /tmp/kafka-logs-100/wait-for-remove-0.bf17e83681d84b9bb50a4413bb0c461e-delete and is scheduled for deletion (kafka.log.LogManager)
[2021-09-01 15:45:06,588] INFO [broker-100-to-controller-send-thread]: Recorded new controller, from now on will use broker localhost:9092 (id: 0 rack: null) (kafka.server.BrokerToControllerRequestThread)
[2021-09-01 15:46:06,595] INFO [Log partition=wait-for-remove-0, dir=/tmp/kafka-logs-100] Deleting segments as the log has been deleted: LogSegment(baseOffset=0, size=0, lastModifiedTime=1630481740242, largestRecordTimestamp=None) (kafka.log.Log)
[2021-09-01 15:46:06,600] INFO [Log partition=rwait-for-remove-0, dir=/tmp/kafka-logs-100] Deleting segment files LogSegment(baseOffset=0, size=0, lastModifiedTime=1630481740242, largestRecordTimestamp=None) (kafka.log.Log)
[2021-09-01 15:46:06,604] INFO Deleted log /tmp/kafka-logs-100/wait-for-remove-0.bf17e83681d84b9bb50a4413bb0c461e-delete/00000000000000000000.log.deleted. (kafka.log.LogSegment)
[2021-09-01 15:46:06,610] INFO Deleted offset index /tmp/kafka-logs-100/wait-for-remove-0.bf17e83681d84b9bb50a4413bb0c461e-delete/00000000000000000000.index.deleted. (kafka.log.LogSegment)
[2021-09-01 15:46:06,611] INFO Deleted time index /tmp/kafka-logs-100/wait-for-remove-0.bf17e83681d84b9bb50a4413bb0c461e-delete/00000000000000000000.timeindex.deleted. (kafka.log.LogSegment)
[2021-09-01 15:46:06,617] INFO Deleted log for partition wait-for-remove-0 in /tmp/kafka-logs-100/wait-for-remove-0.bf17e83681d84b9bb50a4413bb0c461e-delete. (kafka.log.LogManager)

到此为止 topic 已经被删除了

4.10 再次用 Zookeeper CLI 确认

可以看到因为 topic wait-for-remove 已经被删除了,所以ls出来的是空阵列

$ zkCli -server localhost:2181

Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /admin/delete_topics
[]

<<:  Day20 测试写起乃 - $CHILD_STATUS

>>:  [Day18] 跟我一起从头学 React 吧!Let's start learning React from Codecademy! ~ Intro to JSX 篇

第11天 - PHP修改MySQL资料表内容

今天来做从网页修改MySQL资料表内容(修改会员的名称) 首先要做出一个修改按钮(虽然说是按钮,但我...

[04] [Flask 快速上手笔记] 03. 路由

基本路由 使用 route() 装饰器来把函数绑定到 url,就可以定义不同的路径执行的内容 @ap...

Day30 - 第一次铁人赛心得

今天是铁人赛完赛日,写一写心得吧! 老实讲自己能够完赛是真的蛮意外的,虽然一开始说是临时起意,但那也...

【HTML】dl dd dt 清单型的网页标签

网页呈现若有内文有「标题+内文的清单」会怎麽排版呢? 可以使用 description list 哦...

Day7 - 程序设计报价 (二) - 重新定义甲乙关系

从传统的接案甲乙方关系我们发现,因为利益的冲突,甲方也不可能得到乙方 100% 的专业协助,因为乙方...