kafka系列10——第4章1——主题

 🌈hello,你好鸭,我是Ethan,西安电子科技大学大三在读,很高兴你能来阅读。

✔️目前博客主要更新Java系列、项目案例、计算机必学四件套等。
🏃人生之义,在于追求,不在成败,勤通大道。加油呀!

🔥个人主页:Ethan Yankang
🔥推荐:史上最强八股文||一分钟看完我的几百篇博客

🔥温馨提示:划到文末发现专栏彩蛋   点击这里直接传送

🔥本篇概览:详细讲解了kafka系列10——第4章1——主题。🌈⭕🔥


【计算机领域一切迷惑的源头都是基本概念的模糊,算法除外】


🔥   微服务全集

🔥   kafka全集

🔥   前一篇章


🌈引出

Apache的kafka是一个分布式的消息发布订阅中间件。具有高吞吐、可扩展和容错性等特点。主要用于处理大规模的流式数据

本博客从各个方面详细讲解了kafka的机制,并实际上手使用之,好好学完定会习得大功。(bushi,上一次面试就噶在kafka上了,好好对待之。)


4章 主题

tips 学完这一章你可以
深入学习 Kafka 主题的管理
KafkaAdminClient 应用

4.1 管理主题

4.1.1 创建主题

命令
bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic heima --partitions 2 --replication-
factor 1
localhost:2181 zookeeper 所在的 ip zookeeper 必传参数,多个 zookeeper  ‘,’ 分开。
partitions 用于设置主题分区数,每个线程处理一个分区数据
replication-factor 用于设置主题副本数每个副本分布在不同节点,不能超过总结点数。如你只有一个节点,但是创建时指定副本数为2 ,就会报错。

[WARNING]
        "每个副本分布在不同节点,不能超过总结点数" 这句话是正确的。如果你有 3 个 Kafka broker(节点),你可以设置一个分区的副本数为 3,即 1 个主副本和 2 个从副本。每个副本会分布在不同的节点上。这个机制保证了即使某个节点宕机,Kafka 依然能从其他节点上的副本中读取数据。
        一主多从的意思是,其中一个副本会被选为主副本,负责处理所有的读写请求,而其他的副本作为从副本,主要用于同步数据,以备主副本出现故障时能接替它的工作。如果你设置的副本数超过了节点的总数,Kafka 就无法为所有副本找到独立的节点,因此会报错。

查看topic元数据信息的方法
topic 元数据信细保存在z ookeeper 节点中[zookeeper是核心配置中心]
//连接zkclient
itcast@Server-node:/mnt/d/zookeeper-3.4.14$bin/zkCli.sh-serverlocalhost:2181 Connectingtolocalhost:2181
...........................................
[zk:localhost:2181(CONNECTED)2]get/brokers/topics/heima
{"version":1,"partitions":{"1":[0],"0":[0]}}
cZxid=0x618
ctime=WedAug2805:51:35GMT2019
mZxid=0x618
mtime=WedAug2805:51:35GMT2019
pZxid=0x619
cversion=1
dataVersion=0
aclVersion=0
ephemeralOwner=0x0
dataLength=44
numChildren=1
[zk:localhost:2181(CONNECTED)3]

4.1.2 查看主题

// 查看所有主题
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$ bin/kafka-topics.sh --list --
zookeeper localhost:2181
__consumer_offsets
__transaction_state
_schemas
heima
topic0701
topic0703
topic0703kafka_source
topic0828
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$
// 查看某个特定主题信息,不指定topic则查询所有 通过 --describe
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$ bin/kafka-topics.sh --describe --
zookeeper localhost:2181 --topic heima
Topic:heima PartitionCount:2 ReplicationFactor:1 Configs:
Topic: heima Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: heima Partition: 1 Leader: 0 Replicas: 0 Isr: 0
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$
//查看正在同步的主题
// 通过 --describe 和 under-replicated-partitions命令组合查看 under-replacation状态

4.1.3 修改主题

// 增加配置
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$ bin/kafka-topics.sh --alter --
zookeeper localhost:2181 --topic heima
--config flush.messages=1
WARNING: Altering topic configuration from this script has been deprecated and
may be removed in future releases.
Going forward, please use kafka-configs.sh for this functionality
Updated config for topic heima. 
//删除配置
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$bin/kafka-topics.sh--alter--
zookeeperlocalhost:2181--topicheima--delete-configflush.messages
WARNING:Alteringtopicconfigurationfromthisscripthasbeendeprecatedand mayberemovedinfuturereleases.
    Goingforward,pleaseusekafka-configs.shforthisfunctionality Updatedconfigfortopicheima.

4.1.4 删除主题

        若 delete.topic.enable=true 直接彻底删除该 Topic delete.topic.enable=false 如果当前
Topic 没有使用过即没有传输过信息:可以彻底删除。 如果当前 Topic 有使用过即有过传输过信息: 并没有真正删除 Topic 只是把这个 Topic 标记为删除 (marked for deletion) ,重启 Kafka Server 后删除。
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$ bin/kafka-topics.sh --delete --
zookeeper localhost:2181 --topic heima
Topic heima is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
// 标记为 marked for deletion
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$ bin/kafka-topics.sh --list --
zookeeper localhost:2181
__consumer_offsets
topic0701
heima - marked for deletion


4.2 增加分区

// 增加分区数
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$ bin/kafka-topics.sh --alter --
zookeeper localhost:2181 --topic heima --partitions 3
WARNING: If partitions are increased for a topic that has a key, the partition
logic or ordering of the messages will be affected
Adding partitions succeeded!
//修改分区数时,仅能增加分区个数。若是用其减少 partition 个数,则会报如下错误信息:
itcast@Server-node:/mnt/d/kafka_2.12-2.2.1$ bin/kafka-topics.sh --alter --
zookeeper localhost:2181 --topic heima --partitions 2
WARNING: If partitions are increased for a topic that has a key, the partition
logic or ordering of the messages will be affected
Error while executing topic command : The number of partitions for a topic can
only be increased. Topic heima currently has 3 partitions, 2 would not be an
increase.
[2019-08-28 08:43:41,478] ERROR
org.apache.kafka.common.errors.InvalidPartitionsException: The number of
partitions for a topic can only be increased. Topic heima currently has 3
partitions, 2 would not be an increase.
(kafka.admin.TopicCommand$)


4.3 KafkaAdminClient应用

        我们都习惯使用Kafka bin 目录下的脚本工具来管理查看 Kafka 但是有些时候需要将某些管理查看的功能集成到系统,直接使用桌面管理,(比如Kafka Manager )中,那么就需要调用一些 API 来直接操作 Kafka 了。
        见代码库:com.heima.kafka.chapter4.KafkaAdminConfigOperation.


💖💖💖💖💖💖💖💖💖💖💖💖💖💖💖💖💖💖

热门专栏推荐

🌈🌈计算机科学入门系列                     关注走一波💕💕

🌈🌈CSAPP深入理解计算机原理        关注走一波💕💕

🌈🌈微服务项目之黑马头条                 关注走一波💕💕

🌈🌈redis深度项目之黑马点评            关注走一波💕💕

🌈🌈JAVA面试八股文系列专栏           关注走一波💕💕

🌈🌈JAVA基础试题集精讲                  关注走一波💕💕   

🌈🌈代码随想录精讲200题                  关注走一波💕💕


总栏

🌈🌈JAVA基础要夯牢                         关注走一波💕💕  

🌈🌈​​​​​​JAVA后端技术栈                          关注走一波💕💕  

🌈🌈JAVA面试八股文​​​​​​                          关注走一波💕💕  

🌈🌈JAVA项目(含源码深度剖析)    关注走一波💕💕  

🌈🌈计算机四件套                               关注走一波💕💕  

🌈🌈数据结构与算法                           ​关注走一波💕💕  

🌈🌈必知必会工具集                           关注走一波💕💕

🌈🌈书籍网课笔记汇总                       关注走一波💕💕         



📣非常感谢你阅读到这里,如果这篇文章对你有帮助,希望能留下你的点赞👍 关注❤收藏✅ 评论💬,大佬三连必回哦!thanks!!!
📚愿大家都能学有所得,功不唐捐!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值