kafka 常用命令

https://blog.xiaoxiaomo.com/2016/05/13/Kafka-%E5%88%86%E5%B8%83%E5%BC%8F%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/

https://www.zhangluya.com/?p=360

以下命令中localhost 都建议改成ip,否则查问题查半天啊...

消费者组理解:很多consumer可以组成一个组,一个消息在组中只能被一个consumer消费,可以被不同的组消费。
消息持久化:Kafka中会把消息持久化到本地文件系统中,并且保持极高的效率。
消息状态:在Kafka中,消息的状态被保存在consumer中,broker不会关心哪个消息被消费了被谁消费了,只记录一个offset值(指向partition中下一个要被消费的消息位置)。
消息有效期:Kafka会长久(默认七天,一般公司生产环境中会设置很长)保留其中的消息,以便consumer可以多次消费。
批量发送:Kafka支持以消息集合为单位进行批量发送,以提高push效率
同步异步Producer采用异步push方式,极大提高Kafka系统的吞吐率(可以通过参数控制)。
分区机制partition:Kafka的broker端支持消息分区,Producer可以决定把消息发到哪个分区,在一个分区中消息的顺序就是Producer发送消息的顺序,一个主题中可以有多个分区,具体分区的数量是可配置的。分区的意义很重大,后面的内容会逐渐体现。
离线数据装载:Kafka由于对可拓展的数据持久化的支持,它也非常适合向Hadoop或者数据仓库中进行数据装载。
插件支持:现在不少活跃的社区已经开发出不少插件来拓展Kafka的功能,如用来配合Storm、Hadoop、flume相关的插件。

在执行下面命令时,如果提示,  就是命令之间空格不能多个,只能1个,否则报错

例如: 

sh kafka-consumer-groups.sh --bootstrap-server ip:9092  --list
报错

sh kafka-consumer-groups.sh --bootstrap-server ip:9092 --list
成功

--list 前面多了一个空格, 去掉,保留1个空格,才可以

创建topic


kafka-topics.sh --create --zookeeper 10.10.3.11:2181 --replication-factor 3  --partitions 1 --topic test1

Created topic "test1".


#查看所有topic

kafka-topics.sh --list --zookeeper 10.10.3.12:2181

test1


#查看每个topic详细信息

[root@bj-6-dqy-msg-queue01 kafka]# /usr/local/kafka_2.12-0.10.2.0/bin/kafka-topics.sh --describe --zookeeper 10.10.3.13:2181

Topic:test1    PartitionCount:1    ReplicationFactor:3    Configs:

    Topic: test1    Partition: 0    Leader: 1    Replicas: 1,3,0    Isr: 1,3,0
    
###############PartitionCount: 当前topic分区数
###############ReplicationFactor: 当前topic副本数
###############Configs: 当前topic自定义配置信息
###############Partition: patition序号
###############Leader: Leader的brokerID
###############Replicas: 所有副本的brokerId
###############Isr: 所有存活(可用)的brokerId


#查看指定topic 详细信息


kafka-topics.sh --describe --zookeeper 10.10.3.13:2181 --topic test1
kafka-topics.sh --describe --zookeeper localhost:2181 --topic zcy111


#删除topic

kafka-topics.sh --delete --zookeeper 10.10.3.12:2181 --topic test1

Topic test1 is marked for deletion.

Note: This will have no impact if delete.topic.enable is not set to true.
#######################删除world topic ############################
[root@xxo07 bin]# kafka-topics.sh --delete --zookeeper localhost:2181 --topic world
Topic world is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
[root@xxo07 bin]# kafka-topics.sh --list --zookeeper localhost:2181
hello
world - marked for deletion   ##可以看见还在,如果重启可以删除

###是否开启topic的删除功能:默认为false 修改delete.topic.enable=true可以不用重启
[root@xxo07 bin]# vim ../config/server.properties  ###修改配置如下图
[root@xxo07 bin]# kafka-server-stop.sh             ###关闭kafka

[root@xxo07 bin]# kafka-topics.sh --list --zookeeper localhost:2181  ########查看,已经被删除了
hello


[root@xxo07 bin]# kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 2 --topic world
Created topic "world".   ######################重新创建了一个
[root@xxo07 bin]# kafka-topics.sh --delete --zookeeper localhost:2181 --topic world ###再次删除
Topic world is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.  ######这个为提示信息
[root@xxo07 bin]# kafka-topics.sh --list --zookeeper localhost:2181  ########查看,已经被删除了
hello

#一个broker关了,重新加入,怎么重新均匀分配?


##########2.1、Broker配置中的自动均衡策略######################################
#####Auto.leader.rebalance.enable=true######################################
#####leader.imbalance.check.interval.seconds 默认值:300#####################

##########2.2、手动配置执行###################################################
##########下面我们手动配置一下#################################################
[root@xxo10 kafka]# bin/kafka-preferred-replica-election.sh --zookeeper xxo08:2181,xxo09:2181,xxo10:2181
Successfully started preferred replica election for partitions Set([world,0], [world,2], [world,3], [world,1])

#修改 topic 增加分区, 不可以减少


#########################修改hello为2个分区#########################   
[root@xxo07 bin]# kafka-topics.sh --alter --zookeeper localhost:2181 -partitions 3 --topic hello
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数(只能增加)#################
[root@xxo07 bin]# kafka-topics.sh --alter --zookeeper localhost:2181 -partitions 2 --topic hello
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
kafka.admin.AdminOperationException: The number of partitions for a topic can only be increased
    at kafka.admin.AdminUtils$.addPartitions(AdminUtils.scala:114)
    at kafka.admin.TopicCommand$$anonfun$alterTopic$1.apply(TopicCommand.scala:119)
    
    
#生产数据


############broker-list:必须的参数,kafka的服务地址[多个用逗号隔开]#############
./kafka-console-producer.sh --broker-list 10.40.66.19x:9092,10.40.66.19x:9092,10.40.66.19x:9092 --topic zcy222

生产带key数据

sh kafka-console-producer.sh --topic mytestzc --broker-list ip:9092 --property parse.key=true --property key.separator=:

或者
sh kafka-console-producer.sh --topic mytestzc --broker-list ip:9092 --property parse.key=true
默认消息键与消息值间使用“Tab键”进行分隔



#消费数据

1.  从头消费
sh bin/kafka-console-consumer.sh --topic spurs --bootstrap-server localhost:9092  --from-beginning

2. 从最新的消费
sh bin/kafka-console-consumer.sh --topic spurs  --bootstrap-server localhost:9092  

语法: 
# 可以增加 --partition 选项 从指定的分区消费消息
# 可以增加 --offset 选项 从指定的偏移位置消费消息
# 可以增加 --group 选项 以指定消费者组的形式消费消息  (我的版本不支持)
# 可以增加 --max-messages 选项 指定消费消息的最大个数

# 例如 创建hncscwc消费者组, 并从 2号分区 偏移量为1的位置开始消费 2条消息
sh bin/kafka-console-consumer.sh --topic spurs --group hncscwc --partition 2 --offset 1 --max-messages 2 --bootstrap-server localhost:9092
# 结果如下
Mills
Ginobili
Processed a total of 2 messages

或者 
sh bin/kafka-console-consumer.sh --topic spurs --group hncscwc --partition 2  --max-messages 2 --bootstrap-server localhost:9092  --from-beginning
从最原早有效的offset开始 

消费显示key

sh kafka-console-consumer.sh --bootstrap-server ip:9092 --property print.key=true --topic mytestzc

#指定组消费

./kafka-console-consumer.sh -bootstrap-server ip:9092 --consumer.config ../config/consumer1.properties --from-beginning --topic mytestzcy

./kafka-console-consumer.sh -bootstrap-server ip:9092 --consumer.config ../config/consumer1.properties --topic mytestzcy


或者

sh kafka-console-consumer.sh --bootstrap-server ip:9092 
--topic mytestzc --from-beginning --consumer-property group.id=mytestzc

#查看topic在分区上的偏移

./kafka-run-class.sh kafka.tools.GetOffsetShell 

    
./kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 10.40.66.19x:9092,10.40.66.19x:9092,10.40.66.19x:9092 --topic  zcy111

zcy111:0:6
有1个分区, 分区总共有6条数据

sh kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic spurs --time -1 

# 显示结果如下 
spurs:2:0 
spurs:1:0 
spurs:0:0 

# 注1 结果格式为: topic名称:partition分区号:分区的offset 
# 注2 --time 为 -1时用来请求分区最新的offset  --> 每个分区最大的offset
# --time 为 -2时用来请求分区最早有效的offset  --> 有些offset 在7(设置)天删除数据, 最早的0,但是最早有效的不一定是0 


#查看消费组 


./kafka-consumer-groups.sh --bootstrap-server 10.40.66.19x:9092,10.40.66.19x:9092,10.40.66.19x:9092 --list

sh kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list 
我这把 必须localhost 才可以

#查看具体的消费者信息

./kafka-consumer-groups.sh --bootstrap-server 10.40.66.19x:9092,10.40.66.19x:9092,10.40.66.19x:9092 --describe --group z

TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID                                     HOST            CLIENT-ID
zcy111          0          7               7               0               consumer-1-e88b0cc6-1323-47c3-aedb-d77df3ee43b0 /10.40.66.19x   consumer-1

简单对每列进行说明:

  • TOPIC:   消费者的topic名称  
  • PARTITION:   分区数的名称  

  • CURRENT-OFFSET:   consumer group最后一次提交的offset

  • LOG-END-OFFSET:   最后提交的生产消息offset
  • LAG   消费offset与生产offset之间的差值
  • CONSUMER-ID   消费者的ID编号,我们知道消费者组里面可以有最少要有一个消费者,当然也可以有多个消费者。
  • HOST   消费者的主机IP地址。
  • CLIENT-ID   链接的ID编号。

关于offset补充一些知识点。

kafka有个常用的设置是 auto.offset.reset,它的意义是,

该属性指定了消费者在读取一个没有偏移量的分区或者偏移量无效的情况下(因消费者长 时间失效,包含偏移量的记录已经过时井被删除)该作何处理。它的默认值是 latest , 意 思是说,在偏移量无效的情况下,消费者将从最新的记录开始读取数据(在消费者启动之 后生成的记录)。另一个值是 earliest ,意思是说,在偏移量无效的情况下,消费者将从 起始位置读取分区的记录。

这个属性有以下几个值,

  • earliest: 当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,从头开始消费
  • latest: 当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,消费新产生的该分区下的数据
  • none: topic各分区都存在已提交的offset时,从offset后开始消费;只要有一个分区不存在已提交的offset,则抛出异常

我要强调的是,这个设置只有在我们的消费者(或者消费者群组)在分区内找不到有效的offset时才会生效。

举个例子,

我们使用java kafka客户端来操作kafak。

比如我们在消费组group1有个消费者,消费了5条消息然后节点挂了。

然后我们重启这个消费节点,那么我来问你,这个消费者会从哪里开始消费?

如果你回答根据auto.offset.reset的配置来决定那就说明你没理解我上面所说的。

正确的答案是,消费者会继续从上次挂掉的offset(kafka broker保存)那里继续消费,根本不理会auto.offset.reset

再举个例子,

生产者在某个topic生产了一些消息,然后我们启动一个消费组group2,里面有一个消费者。

如果这个时候kafka没有这个topic消息的offset信息,那么auto.offset.reset的值就决定从哪里消费。

#kafka手动修改消费者偏移量

(我的版本低,无法用,需要升级版本后才可以)

kafka手动修改消费者偏移量_代码运输员的博客-CSDN博客_kafka修改偏移量offset

1.设置为最初偏移量:

[root@snn bin]#./kafka-consumer-groups.sh --bootstrap-server snn:6667 --group offsettest --topic offset-test --reset-offsets --to-earliest –execute

2.设置任意偏移量:

[root@snn bin]# ./kafka-consumer-groups.sh --bootstrap-server snn:6667 --group offsettest --topic offset-test --reset-offsets --to-offset 3 –execute

3.设置最近偏移量

[root@snn bin]# ./kafka-consumer-groups.sh --bootstrap-server snn:6667 --group offsettest --topic offset-test --reset-offsets --to-latest --execute

基于上面设置后,消费的获取的结果就一样
重新读取数据:
 ./kafka-console-consumer.sh -bootstrap-server ip:9092 --consumer.config ../config/consumer1.properties --topic mytestzcy

查看消费者消费偏移量:
./kafka-consumer-groups.sh --bootstrap-server ip:9092 --group mytestzcy --describe

#删除broker
jps
kill -9 进程id

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值