標籤:
前面已經介紹了如何利用Thrift Source生產資料,今天介紹如何用Kafka Sink消費資料。
其實之前已經在Flume設定檔裡設定了用Kafka Sink消費資料
agent1.sinks.kafkaSink.type = org.apache.flume.sink.kafka.KafkaSinkagent1.sinks.kafkaSink.topic = TRAFFIC_LOGagent1.sinks.kafkaSink.brokerList = 10.208.129.3:9092,10.208.129.4:9092,10.208.129.5:9092agent1.sinks.kafkaSink.metadata.broker.list = 10.208.129.3:9092,10.208.129.4:9092,10.208.129.5:9092agent1.sinks.kafkaSink.producer.type=syncagent1.sinks.kafkaSink.serializer.class=kafka.serializer.DefaultEncoderagent1.sinks.kafkaSink.channel = memoryChannel
那麼當Flume的channel收到資料的時候,會根據設定檔主動把資料event發送到Kafka的broker上,所以只要安裝好Kafka就可以消費收據了。
Step 1: Download the code
下載安裝包並解壓
> tar -xzf kafka_2.11-0.10.0.0.tgz
> cd kafka_2.11-0.10.0.0
Step 2: Start the server
Kafka是基於Zookeeperl來實現分布式協同的,因此先啟動Zookeeper:
> %Zookeeper_Home%/bin/zkServer.sh start
在設定檔server.properties中把下面一句前面的注釋去掉,然後啟動Kafka伺服器
> #listeners=PLAINTEXT://:9092
> bin/kafka-server-start.sh config/server.properties
接下來啟動其他兩個broker:
> cp config/server.properties config/server-1.properties
> cp config/server.properties config/server-2.properties
修改設定檔,broker.id不能重複
config/server-1.properties: broker.id=1
config/server-2.properties: broker.id=2
Step 3: Create a topic
建立一個TRAFFIC_LOG主題的broker,複製因子為3(因為有3個Kafka伺服器叢集),分區個數為1
> bin/kafka-topics.sh --create --zookeeper 10.208.129.4:2181 --replication-factor 3 --partitions 1 --topic TRAFFIC_LOG
Step 5: Start a consumer > bin/kafka-console-consumer.sh --zookeeper 10.208.129.4:2181 --topic TRAFFIC_LOG --from-beginning
如果在終端看到之前接入的Thrift Source輸出,那麼整個Flume+Kafka算是跑通了
這裡已經引入了Flume和Kafka,下一篇將介紹Kafka以及Flume和Kafka的區別
Flume簡介與使用(三)——Kafka Sink消費資料之Kafka安裝