$ mkdir kafka
$ wget http://apache.dataguru.cn/kafka/0.8.0/kafka_2.8.0-0.8.0.tar.gz
$ tar -zxvf kafka_2.8.0-0.8.0.tar.gz
$ mv kafka_2.8.0-0.8.0 kafka
2.kafka自带了zookeeper,启动一个单点的zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties&
3.启动kafka
bin/kafka-server-start.sh config/server.properties&
4.创建一个topic
$ bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 1 --partition 1 --topic test1
creation succeeded!
bin/kafka-create-topic.sh --zookeeper 192.168.56.102:2181 --replica 1 --partition 1 --topic test1
5.查看topic
$ bin/kafka-list-topic.sh --zookeeper localhost:2181
bin/kafka-list-topic.sh --zookeeper 192.168.56.102:2181
root@Cassandra kafka]# bin/kafka-list-topic.sh --zookeeper localhost:2181
[2014-09-16 09:22:05,826] INFO Accepted socket connection from /0:0:0:0:0:0:0:1:54029 (org.apache.zookeeper.server.NIOServerCnxn)
[2014-09-16 09:22:05,830] INFO Client attempting to establish new session at /0:0:0:0:0:0:0:1:54029 (org.apache.zookeeper.server.NIOServerCnxn)
[2014-09-16 09:22:05,832] INFO Established session 0x1487c0c10b80002 with negotiated timeout 30000 for client /0:0:0:0:0:0:0:1:54029 (org.apache.zookeeper.server.NIOServerCnxn)
topic: 3test partition: 0 leader: 0 replicas: 0 isr: 0
topic: testtopic partition: 0 leader: 0 replicas: 0 isr: 0
[2014-09-16 09:22:06,294] INFO Processed session termination for sessionid: 0x1487c0c10b80002 (org.apache.zookeeper.server.PrepRequestProcessor)
[2014-09-16 09:22:06,296] INFO Closed socket connection for client /0:0:0:0:0:0:0:1:54029 which had sessionid 0x1487c0c10b80002 (org.apache.zookeeper.server.NIOServerCnxn)
说明:
partiton: partion id,由于此处只有一个partition,因此partition id 为0
leader:当前负责读写的lead broker id
relicas:当前partition的所有replication broker list
isr:relicas的子集,只包含出于活动状态的broker
6.启动生产者,生产消息,并且Ctrl+z退出shell
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test1
bin/kafka-console-producer.sh --broker-list 192.168.56.102:9092 --topic test1
This is a message
This is another message
7.启动命令行消费者,接收消息
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test1 --from-beginning
bin/kafka-console-consumer.sh --zookeeper 192.168.56.102:2181 --topic test1 --from-beginning
This is a message
This is another message
8.删除以前的topic
bin/kafka-run-class.sh kafka.admin.DeleteTopicCommand --topic 3test --zookeeper localhost:2181
关闭kafka
bin/kafka-server-stop.sh config/server.properties