kafka 安装教学(官方版)

最近在学校kafka,学习kafka第一步,让咱们先把kafka安装起来吧!

使用环境:ubuntu系统

kafka下载地址:http://kafka.apache.org/downloads

官方安装教学:http://kafka.apache.org/quickstart


一、解压下载的安装包(我下载了kafka_2.11-1.1.0.tgz)

解压下载的安装包

tar -xzf kafka_2.11-1.1.0.tgz 


ok,到这你就已经完成了安装了!对,是的完成安装了!无需其他配置!下面接着介绍如何启动、如何在单机上部署集群!


二、kafka常用命令(命令都是在解压目录中执行)

进入解压后的目录


(1)启动kafka(先启动zookeeper)

bin/zookeeper-server-start.sh config/zookeeper.properties 
bin/kafka-server-start.sh config/server.properties 

(2)创建topic

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

(3)查看topic

bin/kafka-topics.sh --list --zookeeper localhost:2181

(4)发送消息

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

(5)查看消息

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning


(6)多broker启动方式

a.复制配置文件

cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties

b.修改配置文件

使用vim 编辑server-1.properties、server-2.properties,修改broker.id、listeners、log.dir属性。(相同的话启动会报错)

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

c.启动集群

bin/kafka-server-start.sh config/server-1.properties &
bin/kafka-server-start.sh config/server-2.properties &

d.创建备份为3的topic(每个broker都有备份,如果有个broker无法工作了,系统还能正常工作)

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated

e.查看topic

 bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic

Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
Topic: my-replicated-topic  Partition: 0    Leader: 1   Replicas: 1,2,0 Isr: 1,2,0


(7)使用文件导入消息

a.创建导入文件(消息内容为foo\bar),该文件请在解压目录下创建

 echo -e "foo\nbar" > test.txt

b.使用默认配置进行导入

bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties

c.查看导入和到处的文件

more test.sink.txt
foo
bar

d.查看kafka中的消息

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic connect-test --from-beginning
{"schema":{"type":"string","optional":false},"payload":"foo"}

e.connectors会继续处理text.txt文本中的内容,向文本重新向内容。可以继续观察test.sink.txt和控制台的输出

 echo Another line>> test.txt









猜你喜欢

转载自blog.csdn.net/nash885/article/details/80045534