Kafka的版本间差异较大,下面是0.8.1的操作方法
首先cd到kafaka的bin目录下;
#step1启动zookeeper服务
nohup bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper.out &
#step2 启动kafka服务
nohup bin/kafka-server-start.sh config/server.properties &
nohup bin/kafka-server-start.sh config/server.properties > kafka_server.out &
#step3创建一个kafka消息队列
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test2
注:
--partitions 1 为分区数,为分区数,0.8.1默认就是2
--replication-factor 3 设置是topic消息的备份份数为3份,即副本数
#创建消息队列,并指定topic的配置参数(消息的最大值参数max.message.bytes )
kafka-topics.sh --create --zookeeper $ZK_CONNECT --replication-factor 2 --partitions 200 --topic cdn_gz_data --config max.message. bytes=10485760 --config flush.message=1
#step4列出现有的topic列表
bin/kafka-topics.sh --list --zookeeper localhost:2181
#step5发送消息
Kafka提供了命令行工具从文件获取输入或者标准输入 ,作为消息发送到kafka集群,默认一行一个消息。
标准输入:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
#step6启动consumer,接收消息
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
#step7查看一个topic的明细 即describe topics,
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic page_visits
#step8 删除topic
kafka-topics.sh --topic test_kafka1 --zookeeper $ZK_CONNECT --delete test_kafka1
# 给一个topic 添加配置项
To add configs:
> bin/kafka-topics.sh --zookeeper zk_host:port/chroot --alter --topic my_topic_name --config x=y
# 给一个topic 添加配置项
To remove a config:
> bin/kafka-topics.sh --zookeeper zk_host:port/chroot --alter --topic my_topic_name --deleteConfig x
# 将一个topic标记为删除,删除的topic默认不可用。
And finally deleting a topic:
> bin/kafka-topics.sh --zookeeper zk_host:port/chroot --delete --topic my_topic_nameTopic deletion option is disabled by default. To enable it set the server config
delete.topic.enable=true
kafka-run-class.sh kafka.admin.DeleteTopicCommand --topic tvstore --zookeeper $ZK_CONNECT
#查看不可用分区
./kafka-topics.sh -topic tvstore -describe -unavailable-partitions -zookeeper $ZK_CONNECT
#kafka查看topic各个分区的消息的信息
kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --group cdn_log_group --topic cdn_gzip_data --zookeeper $ZK_CONNECT
-----------------------------------------
####kafka系统工具################
Displays the: Consumer Group, Topic, Partitions, Offset, logSize, Lag, Owner for the specified set of Topics and Consumer Group
bin/kafka-run-
class
.sh kafka.tools.ConsumerOffsetChecker
kafka-run-
class
.sh kafka.tools.DumpLogSegments
Dump Log Segment
This can print the messages directly from the log files or just verify the indexes correct for the logs
bin/kafka-run-
class
.sh kafka.tools.DumpLogSegments