1. 程式人生 > >Kafka環境搭建(分散式)

Kafka環境搭建(分散式)

 

KafKa環境搭建(分散式)

 

1.上傳kafka_2.11-0.10.0.0.tgz到software下面

 

2.解壓kafka_2.11-0.10.0.0.tgz(並將kafka安裝包放到別的資料夾中,統一管理。可以不理)

 

         tar -zxvf kafka_2.11-0.10.0.0.tgz

 

3.將kafka_2.11-0.10.0.0修改成kafka_2.11-0.10

         mv  kafka_2.11-0.10.0.0  kafka_2.11-0.10

 

4.修改namenode的server.properties

         cd  kafka_2.11-0.10/config/

         vi  server.properties

        

         broker.id=0

         host.name=188.2.72.57

         port=9092

         log.dirs=/home/hadoop/kafka_2.11-0.10/data/log-0

         zookeeper.connect=localhost:2181

 

 

5. 修改datanode1的server.properties

         cd  kafka_2.11-0.10/config/

         vi  server.properties

       

         broker.id=1

         host.name=188.2.72.58

         port=9093

         log.dirs=/home/hadoop/kafka_2.11-0.10/data/log-1

         zookeeper.connect=localhost:2181

 

 

 

6.修改datanode2的server.properties

 

         cd  kafka_2.11-0.10/config/

         vi  server.properties

        

         broker.id=2

         host.name=188.2.72.59

         port=9094

         log.dirs=/home/hadoop/kafka_2.11-0.10/data/log-2

         zookeeper.connect=localhost:2181

 

 

 

7. 啟動kafka服務(分別啟動三臺)

 

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

 

看到三臺機器分別有以下資料,啟動成功

namenode

datanode1顯示

datanode2顯示

其中的188.2.72.57、9092、188.2.72.58、9093、188.2.72.59、9094說明我們的配置檔案起到了作用,並且都啟動成功

 

8. 建立topic(重新開啟一個終端或是ctrl+c返回)

 

bin/kafka-topics.sh --create --zookeeper localhost:2181  --replication-factor  2  --partition  3 --topic test

 

 (也可以寫為:

bin/kafka-topics.sh --create --zookeeper namenode:2181, datanode1:2181, datanode2:2181  --replication-factor  2  --partition  3 --topic test

也可以寫為:

bin/kafka-topics.sh --create --zookeeper 188.2.72.57:2181, 188.2.72.58:2181, 188.2.72.59:2181  --replication-factor  2  --partition  3 --topic test

 

)

 

9.檢視日誌

 

cd data/log-0/test1-0

ll

 

10.列出topic

 

         cd  ..

         cd  ..

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

看到名為test1的topic(test是之前建立的)

 

11.向topic裡寫入日誌(生產者)

 

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

this is a message:1

this is a message:3

this is a message:3

 

12.檢視資料(namenode上執行)(消費者)

 

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

看到之前輸入的資料,那麼kafka就搭建完成了(注kafka接收是沒有順序的)

this is a message:1

this is a message:3

this is a message:3