kafka学习笔记1--kafka安装以及简单使用

最简单的使用方式 , 使用自带的zookeeper

1.下载解压

从apache-kafka官方下载最新的kafka安装包,我所使用的为kafka1.0.1.
tar -zxvf kafka-xxx.tar.gz

2.修改配置

转到kafka的解压目录,
vim config/server.properties
修改项如下(有则修改,无则添加):
host.name=你的ip
listeners=PLAINTEXT://你的ip:9092
advertised.listeners=PLAINTEXT://你的ip:9092
zookeeper.connect=你的ip:2181

3.启动 (nohub 和 最后的 &都是为了后台启动,避免ctrl+c 或者关闭链接而kill)

启动zookeeper:nohup bin/zookeeper-server-start.sh config/zookeeper.properties 1>zookeeper.log 2>zookeeper.err &
启动kafka:nohup bin/kafka-server-start.sh config/server.properties &

4.创建topic 并查看

创建topic:bin/kafka-topics.sh --create --topic test --replication-factor 1 --partitions 1 --zookeeper 你的IP:2181
输出:Created topic "test".
 
查看topic list:bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --list
输出:test
 
查看topic明细:bin/kafka-topics.sh  --zookeeper 127.0.0.1:2181 --topic test --describe

输出:Topic:test PartitionCount:1 ReplicationFactor:1 Configs:
   Topic: test Partition: 0 Leader: 0 Replicas: 0 Isr: 0

5.收发消息验证

发:bin/kafka-console-producer.sh --broker-list 你的IP:9092 –topic test
输出:>hello
           >nice to meet you
 
收:bin/kafka-console-consumer.sh --bootstrap-server 你的IP:9092  --from-beginning --topic test  
输出:hello
           nice to meet you
 

6.查看指定group下的消息总数及未消费的消息总数

 bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group 0 

 LOG-END-OFFSET为消息总数,CURRENT-OFFSET为当前组的OFFSET,LAG为未消息的消息数量。CONSUMER-ID/HOST/CLIENT-ID均为当前实时在线的客户端信息。

7.调整group的offset,重复消息上一条或多条消息

bin/kafka-console-consumer.sh --bootstrap-server 你的IP:9092 --topic cims --partition 0 --offset 703

8.可能存在的问题

启动失败
  1. 报错说没有log.dirs=/tmp/kafka-logs,这个目录没有写权限,这时就得ll检查一下权限了,比如我用root安装,用work启动,就会遇到此问题
  2. 收不到消息:连不上zookeeper,但是zookeeper进程在,可能是因为 步骤2 没有修改IP,原因是kafk会自动获取,可能会获取出错。
  3. 假死:进程在,但是无法使用,或者调用 bin/zookeeper-server-stop.sh 和bin/kafka-server-stop.sh后进程依然在,就ps –ef | grep kafka , ps –ef | grep zookeeper手动kill -9 一下
原文地址:https://www.cnblogs.com/gxiaoyang/p/9188800.html