iT邦幫忙

2021 iThome 鐵人賽

DAY 6
0
Modern Web

『卡夫卡的藏書閣』- 程序猿必須懂的Kafka開發與實作系列 第 6

卡夫卡的藏書閣【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
[]

上一篇
卡夫卡的藏書閣【Book5】- Kafka 安裝與基本設定
下一篇
卡夫卡的藏書閣【Book7】- Kafka 實作新增 Topic
系列文
『卡夫卡的藏書閣』- 程序猿必須懂的Kafka開發與實作30

尚未有邦友留言

立即登入留言