大家:
好! flume同kafka的对接,请参考
本质上是将flume作为kafka的生产者来说的,监控一个目录,kafka消费者显示
第一步:编辑flume_kafka的配置文件,是在flume的conf 目录下flume-kafka.sh(脚本在后面)
说明:此步前提是要先在kafka中创建一个名为kafkatest的topic,
第二步:启动flume脚本
bin/flume-ng agent -c conf -f conf/flume-kafka.sh -n agent
说明:后面的-n 是agent的名称,我踩过一次坑
启动flume之后的截图如下所示:
显示正在等待输入的状态
第三步:启动kafka的消费者的程序
kafka-console-consumer.sh --topic kafkatest--zookeeper hadoop:2181
第四步:往日志文件中添加数据,观察kafka的客户端是否显示
往日志文件中增加数据,可以是echo 的命令,也可以用crontab ,也可以用azkaba
[root@hadoop test]# ll
total 40
-rw-r--r-- 1 root root 1 Mar 11 13:14 abc.log
-rw-r--r-- 1 root root 675 Aug 2 2018 derby.log
-rw-r--r-- 1 root root 85 Jul 9 2018 hbase.txt
drwxr-xr-x 5 root root 4096 Aug 2 2018 metastore_db
-rw-r--r-- 1 root root 36 Oct 20 2017 person.txt
-rw-r--r-- 1 root root 239 Jun 29 2018 stu.txt
-rw-r--r-- 1 root root 14246 Feb 22 16:59 zookeeper.out
[root@hadoop test]# echo " hello hai" >> abc.log
[root@hadoop test]# echo " hello hai1" >> abc.log
检查kafka是否进行了消费
[root@hadoop ~]# kafka-console-consumer.sh --topic kafkatest --zookeeper hadoop:2181
hello hai
hello hai1
可以看到,kafka已经进行了消费,验证完毕
---flume-kafka.sh的脚本如下所示:
# 启动flume bin/flume-ng agent -c conf -f conf/flume-kafka.sh -n agent
agent.sources = s1
agent.channels = c1
agent.sinks = k1
agent.sources.s1.type=exec
agent.sources.s1.command=tail -F /root/test/abc.log
agent.sources.s1.channels=c1
agent.channels.c1.type=memory
agent.channels.c1.capacity=10000
agent.channels.c1.transactionCapacity=100
#设置一个kafka接收器
agent.sinks.k1.type= org.apache.flume.sink.kafka.KafkaSink
#设置kafka的broker地址和端口号(所有的)
agent.sinks.k1.brokerList=192.168.16.100:9092
#设置kafka的topic
agent.sinks.k1.topic=kafkatest
#设置一个序列化方式
agent.sinks.k1.serializer.class=kafka.serializer.StringEncoder
#组装
agent.sinks.k1.channel=c1