概述
下面介绍一些 Kafka 经常使用到的命令。
服务端启动命令
| 1
2
3
 | # -daemon 表示后台进行
# config/server.properties 指定配置文件
kafka-server-start.sh -daemon config/server.properties
 | 
 
创建topic
| 1
2
3
 | # replication-factor 2 表示复制两份
# partitions 3 topic 的分区三个
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 3 --topic kafka-action
 | 
 
修改topic分区
| 1
 | kafka-topics.sh --alter --zookeeper localhost:2181 --topic kafka-action --partitions 5
 | 
 
启动Kafka的console生产者
| 1
2
 | # property key.separator=' ' 属性配置
kafka-console-producer.sh --broker-list localhost:9092 --topic kafka-action --property parse.key=true --property key.separator=' '
 | 
 
查看topic某分区偏移量最值
| 1
2
 | # -time -1 最大的偏移量 
kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic kafka-action -time -1
 | 
 
读取日志文件
DumpLogSegment 这个类是可以读取 Kafka Log 的二进制的内容。
| 1
 | kafka-run-class.sh kafka.tools.DumpLogSegments --files /tmp/kafka-logs/kafka-action-2/00000000000000000000.log
 | 
 
| 1
2
3
 | # 结果
Dumping /tmp/kafka-logs/kafka-action-2/00000000000000000000.log
Starting offset: 6
 | 
 
性能测试
| 1
2
3
4
 | # num-records 消息记录数
# record-size 消息大小
# throughput 每秒消息数
kafka-producer-perf-test.sh --num-records 100000 --record-size 1000 --topic kafka-action --throughput 100000 --producer-props bootstrap.servers=localhost:9092 acks=all
 | 
 
| 1
2
 | # 结果
100000 records sent, 37243.947858 records/sec (35.52 MB/sec), 527.35 ms avg latency, 914.00 ms max latency, 465 ms 50th, 896 ms 95th, 904 ms 99th, 913 ms 99.9th.
 | 
 
开启Kafka的console消费者
| 1
 | kafka-console-consumer.sh --bootstrap-server localhost:9092 --new-consumer --consumer-property group.id=new-consumer-test --consumer-property client.id=new-consumer-c1 --topic kafka-action
 | 
 
			
				警告
		    
			
				
					本文最后更新于 2017年2月1日,文中内容可能已过时,请谨慎参考。