六、Flume 对接 Kafka
1、简单实现
1)配置flume
[xiaoxq@hadoop105 jobs]$ pwd
/opt/module/flume-1.9.0/jobs
[xiaoxq@hadoop105 jobs]$ vim flume-kafka.conf
- 添加如下内容
# define
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/module/datas/flume.log
# sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.bootstrap.servers = hadoop105:9092,hadoop106:9092,hadoop107:9092
a1.sinks.k1.kafka.topic = third
a1.sinks.k1.kafka.flumeBatchSize = 20
a1.sinks.k1.kafka.producer.acks = 1
a1.sinks.k1.kafka.producer.linger.ms = 1
# channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# bind
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2) 创建日志接收文件及相关主题
[xiaoxq@hadoop105 datas]$ pwd
/opt/module/datas
[xiaoxq@hadoop105 datas]$ touch flume.log
[xiaoxq@hadoop105 datas]$ ll
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 26 20:27 flume.log
[xiaoxq@hadoop105 kafka_2.11-2.4.1]$ bin/kafka-topics.sh --create --topic third --bootstrap-server hadoop105:9092
3) 启动kafka消费者
- 启动zookeeper和hadoop集群
- 启动kafka
[xiaoxq@hadoop105 kafka_2.11-2.4.1]$ bin/kafka-console-consumer.sh --bootstrap-server hadoop105:9092 --topic third --consumer.config config/consumer.properties
4)进入flume根目录下,启动flume
[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent -c conf/ -n a1 -f jobs/flume-kafka.conf
5) 向 /opt/module/data/flume.log里追加数据,查看kafka消费者消费情况
[xiaoxq@hadoop105 datas]$ echo hello >> /opt/module/datas/flume.log
[xiaoxq@hadoop105 datas]$ echo flume >> /opt/module/datas/flume.log
[xiaoxq@hadoop105 datas]$ echo hadoop >> /opt/module/datas/flume.log