分享

about云日志分析项目准备9-1:Flume1.7安装和使用:解决不断增加的日志文件及追加数据

pig2 发表于 2017-8-20 20:19:16 [显示全部楼层] 只看大图 回帖奖励 阅读模式 关闭右栏 0 3839
问题导读
1.对于不断追加的文件可以使用flume哪个属性?
2.对于不断追加的文件及变化的文件个数,可是使用flume哪个属性?
3.该如何配置能够搜集网站日志的flume?






本文的背景:
在搜集日志的过程中,日志文件的个数及日志文件需要不断的追加。flume1.6中,可以使用tail -f可以解决不断追加的文件,但是由于日志文件的个数是变化的,不可能只产生一个文件。所以tail -f就已经不能解决这个搜集日志的问题。

需求:
需要能够监控文件,并且追加这个,同时文件个数也是不断变化的。

解决办法:
这时候flume1.7就产生了,很好的通过 TAILDIRl解决了这个问题。TAILDIRl可以监控一个目录下的文件。

官网地址:http://flume.apache.org/FlumeUserGuide.html

官网文档截图:

1.jpg


上面加粗为常用属性。

这里我们只使用了下面两个属性
a1.sources.source1.filegroups.f1 = /data/aboutyunlog/.*log.*
a1.sources.source1.type = TAILDIR

flume1.7安装包
链接:http://pan.baidu.com/s/1c1Pzo9i 密码:fxa4



一、Flume安装

1. 压缩安装包
  1. tar -zxvf ~/jar/apache-flume-1.7.0-bin.tar.gz -C /data
  2. mv /data/apache-flume-1.7.0-bin/ /data/flume-1.7.0 # 重命名
复制代码

2. 配置环境变量
  1. echo -e "export FLUME_HOME=/data/flume-1.7.0\nexport PATH=\$FLUME_HOME/bin:\$PATH" >> ~/.bashrc
  2. source ~/.bashrc
复制代码

3. 配置flume
  1. cp flume-env.sh.template flume-env.sh修改JAVA_HOME
  2. export JAVA_HOME= /data/jdk1.8.0_111
复制代码


4. 验证安装
  1. flume-ng version
复制代码

1.jpg


二、Flume使用

一个agent由source、channel、sink组成。这儿我们使用Spooling Directory Source、File Channel、Kafka Sink。

1. 单节点的agent
1) 增加配置文件
  1. cd $FLUME_HOME/conf
  2. vim single_agent.conf
复制代码

将以下内容拷贝进去

  1. # agent的名称为a1
  2. a1.sources = source1
  3. a1.channels = channel1
  4. a1.sinks = sink1

  5. # set source
  6. #a1.sources.source1.type = spooldir
  7. a1.sources.source1.type = TAILDIR
  8. a1.sources.source1.filegroups = f1
  9. a1.sources.source1.filegroups.f1 = /data/aboutyunlog/.*log.*
  10. #a1.sources.source1.spoolDir=/data/aboutyunlog
  11. a1sources.source1.fileHeader = flase

  12. # set sink
  13. a1.sinks.sink1.type = org.apache.flume.sink.kafka.KafkaSink
  14. #a1.sinks.sink1.kafka.bootstrap.servers = master:9092,slave1:9092,slave2:9092
  15. a1.sinks.sink1.brokerList= master:9092,slave1:9092,slave2:9092
  16. a1.sinks.sink1.topic= aboutyunlog
  17. a1.sinks.sink1.kafka.flumeBatchSize = 20
  18. a1.sinks.sink1.kafka.producer.acks = 1
  19. a1.sinks.sink1.kafka.producer.linger.ms = 1
  20. a1.sinks.sink1.kafka.producer.compression.type = snappy

  21. # set channel
  22. a1.channels.channel1.type = file
  23. a1.channels.channel1.checkpointDir = /data/flume_data/checkpoint
  24. a1.channels.channel1.dataDirs= /data/flume_data/data

  25. # bind
  26. a1.sources.source1.channels = channel1
  27. a1.sinks.sink1.channel = channel1

复制代码



2. 创建所需文件

  1. mkdir -p /data/aboutyunlog
  2. mkdir -p /data/flume_data/checkpoint
  3. mkdir -p /data/flume_data/data
复制代码

3. 查看kafka现有的topic
  1. kafka-topics.sh --zookeeper master:2181,slave1:2181,slave2:2181 --list
复制代码



4. 在kafka上创建名为aboutyunlog的topic
  1. kafka-topics.sh --zookeeper master:2181,slave1:2181,slave2:2181 --create --topic aboutyunlog --replication-factor 1 --partitions 3
复制代码


5. 启动flume
  1. flume-ng agent --conf-file /data/flume-1.7.0/conf/single_agent.conf --name a1 -Dflume.root.logger=INFO,console
复制代码
启动过程中控制台会输出很多日志。

6. 创建一个kafka的consumer
  1. kafka-console-consumer.sh --zookeeper master:2181,slave1:2181,slave2:2181  --topic aboutyunlog --from-beginning
复制代码
这条命令的意思是说创建aboutyunlog这个topic下的消费者,消费时从最开始的一条信息开始消费。

上图说明该消费者创建成功,由于本地/data/aboutyunlog目录下没有新文件加入,造成aboutyunlog这个topic没有信息输入,所以消费者没有得到一条信息。

7.  添加文件到flume source目录

  1. echo -e "this is a test file! \nhttp://www.aboutyun.com20170820"
  2. mv log.1 /data/aboutyunlog/
复制代码
为:echo -e "this is a test file! \nhttp://www.aboutyun.com20170820">log.1
再次执行
  1. echo -e "this is a test file! \nhttp://www.aboutyun.com20170820">log.2
复制代码

1.jpg

然后我们看到

master上
3.jpg


8. 再次查看kafka consumer


切换到创建kafka consumer的shell界面,会看到我们log.1中文件的内容被打印在屏幕上。

2.jpg
上图说明我们已经成功使用flume监控/data/aboutyunlog目录,并将监控目录中的内容发送到kafka的aboutyunlog主题中。








没找到任何评论,期待你打破沉寂

关闭

推荐上一条 /2 下一条