kafka的一些常用命令
-
启动zookeeper
bin/zkServer.sh start conf/zoo.cfg & -
启动kafka
bin/kafka-server-start.sh config/server.properties & -
创建topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test -
查看topic list
bin/kafka-topics.sh --list --zookeeper localhost:2181 -
topic 详情
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test -
Kafka自带一个命令行客户端,它从文件或标准输入中获取输入,并将其作为message(消息)发送到Kafka集群。默认情况下,每行将作为单独的message发送。运行 producer,然后在控制台输入一些消息以发送到服务器。
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
just for fun -
启动一个 consumer
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning -
设置多代理集群。把集群扩展到三个节点(仍然在本地机器上)。首先,为每个代理创建一个配置文件
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties
修改属性
config/server-1.properties:
broker.id=1
listeners=PLAINTEXT://:9093
log.dir=/tmp/kafka-logs-1
config/server-2.properties:
broker.id=2
listeners=PLAINTEXT://:9094
log.dir=/tmp/kafka-logs-2
broker.id属性是集群中每个节点的名称,这一名称是唯一且永久的。我们必须重写端口和日志目录,因为我们在同一台机器上运行这些,我们不希望所有的代理尝试在同一个端口注册,或者覆盖彼此的数据
启动两个新的节点
bin/kafka-server-start.sh config/server-1.properties &
bin/kafka-server-start.sh config/server-2.properties &现在创建一个副本为3的新topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic test-replicated-topic