Kafka的版本間差別較大,下面是0.8.2.1的操做方法app
首先cd到kafaka的bin目錄下;工具
#step1啓動zookeeper服務命令行
nohup bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper.out &code
#step2 啓動kafka服務cdn
nohup bin/kafka-server-start.sh config/server.properties &server
nohup bin/kafka-server-start.sh config/server.properties > kafka_server.out &blog
#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 test2ip
注: ci
--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增長分區數(原本有20個分區,修改成26個)
kafka-topics.sh --zookeeper $ZK_CONNECT --alter --partitions 26 --topic app_request
# 將一個topic標記爲刪除,刪除的topic默認不可用。
And finally deleting a topic:
> bin/kafka-topics.sh --zookeeper zk_host:port/chroot --delete --topic my_topic_name
Topic 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