### `版本:kafka_2.11-0.10.1.0和kafka_2.12-2.2.0`
### 初始化操作
`kafka_2.11-0.10.1.0/kafka_2.12-2.2.0`
```sh
$ export KAFKA_HOME=/usr/local/kafka/
$ cd $KAFKA_HOME
```
### 启动zookeeper
`kafka_2.11-0.10.1.0/kafka_2.12-2.2.0`
```sh
$ bin/zookeeper-server-start.sh config/zookeeper.properties
```
### 启动kafka
`kafka_2.11-0.10.1.0/kafka_2.12-2.2.0`
```sh
$ bin/kafka-server-start.sh config/server.properties
```
### 创建topic
`kafka_2.11-0.10.1.0`
```sh
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
```
`kafka_2.12-2.2.0`
```sh
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
```
### 查看topic列表
`kafka_2.11-0.10.1.0`
```sh
$ bin/kafka-topics.sh --list --zookeeper localhost:2181
```
`kafka_2.12-2.2.0`
```sh
$ bin/kafka-topics.sh --list --bootstrap-server localhost:9092
```
### 查看topic详细
`kafka_2.11-0.10.1.0`
```sh
$ bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
```
`kafka_2.12-2.2.0`
```sh
$ bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test
```
### producer发送消息
`kafka_2.11-0.10.1.0/kafka_2.12-2.2.0`
```sh
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
```
输入
```sh
This is a message
This is another message
```
### consumer消费消息
`kafka_2.11-0.10.1.0/kafka_2.12-2.2.0`
```sh
$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
```
输出
```sh
This is a message
This is another message
````
#### 管理消费组(Consumer Groups)
`kafka_2.11-0.10.1.0/kafka_2.12-2.2.0`
```sh
# 查看消费组列表
$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
# 查看消费组offsets详情
$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group group_name
```
`kafka_2.11-0.10.1.0/kafka_2.12-2.2.0`
如果使用的旧版消费者,组的元数据存在zookeeper(offsets.storage=zookeeper),请使用下面的命令
```sh
# 查看消费组列表
$ bin/kafka-consumer-groups.sh --zookeeper localhost:2181 --list
# 查看消费组offsets详情
$ bin/kafka-consumer-groups.sh --zookeeper localhost:2181 --describe --group group_name
```
#### 查看topic消息统计
`kafka_2.11-0.10.1.0`
```sh
$ bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test
```