1.下载源码包
下载地址:http://kafka.apache.org/downloads
2.上传源码包并解压到指定目录
tar -zxvf kafka_2.11-0.10.2.1.tgz -C /home/hadoop/app/
3.修改配置文件
cd kafka_2.11-0.8.2.2/config
sudo vim server.properties
修改如下内容:
#1.指定broker的id
broker.id=1
#2.允许删除topic,原来是标记删除(防止误操作)
delete.topic.enable=true
########socket server settings###########
#3.指定往Kafka中写数据的时候监听的地址和端口
listeners=PLAINTEXT://L1:9092
#4.指定往Kafka中读数据的时候监听的地址和端口
advertised.listeners=PLAINTEXT://L1:9092
#5.数据存放的目录
log.dirs=/bigdata/kafka_2.11-0.10.2.1/data
#6.数据删除的策略(数据可以保存多长时间)
log.retention.hours=168
#7.指定zookeeper的地址
zookeeper.connect=L1:2181,L2:2181,L3:2181
修改完之后,将kafka_2.11-0.8.2.2安装目录拷贝到其他机器上,修改server.properties这个配置文件,将broker.id设为不同的值,将 listeners=PLAINTEXT://L1:9092和advertised.listeners=PLAINTEXT://L1:9092修改为所在机器的主机名。
4. 基本命令
1.启动kafka
在每台机器上都要执行,也可以写一个启动脚本。
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-server-start.sh -daemon /bigdata/kafka_2.11-0.10.2.1/config/server.properties
2.停止kafka
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-server-stop.sh
3.创建topic
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-topics.sh --create --zookeeper L1:2181,L2:2181,L3:2181 --replication-factor 3 --partitions 3 --topic topicname
4.列出所有topic
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-topics.sh --list --zookeeper L1:2181,L2:2181,L3:2181
5.查看某个topic信息
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-topics.sh --describe --zookeeper L1:2181,L2:2181,L3:2181 --topic topicname
6.启动一个命令行的生产者
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-console-producer.sh --broker-list node-1:9092,node-1:9092,node-3:9092 --topic topicname
7.启动一个命令行的消费者
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-console-consumer.sh --zookeeper L1:2181,L2:2181,L3:2181 --topic topicname --from-beginning
8.消费者连接到borker的地址
/bigdata/kafka_2.11-0.10.2.1/bin/kafka-console-consumer.sh --bootstrap-server L3:9092,L4:9092,L5:9092 --topic topicname --from-beginning
5.关于Kafka的更多工具
1.Kafka Connect:
https://kafka.apache.org/documentation/#connect
http://docs.confluent.io/2.0.0/connect/connect-jdbc/docs/index.html
2.Kafka Stream:
https://kafka.apache.org/documentation/streams
https://spark.apache.org/docs/1.6.1/streaming-kafka-integration.html
3.kafka monitor:
https://kafka.apache.org/documentation/#monitoring
https://github.com/quantifind/KafkaOffsetMonitor
https://github.com/yahoo/kafka-manager
4.kafka生态圈:
https://cwiki.apache.org/confluence/display/KAFKA/Ecosystem