Flume从入门到精通4:Flume实战之采集文件内容并显示在屏幕上

案例:采集日志文件中最新信息显示在屏幕上。

1.定义Agent配置文件

由Flume的体系结构可知,Agent = Source + Chanel + Sink,所以定义Agent的配置信息,就是定义这三个组件的具体实现方式。

创建一个目录myagent用于存放Agent配置文件:

# mkdir /root/trainings/apache-flume-1.8.0-bin/myagent

定义一个Agent的配置文件a3.conf:

# vim /root/trainings/apache-flume-1.8.0-bin/myagent/a3.conf

内容如下:

# bin/flume-ng agent -n a3 -f myagent/a3.conf -c conf -Dflume.root.logger=INFO,console
#定义agent名, source、channel、sink的名称
a3.sources = r1
a3.channels = c1
a3.sinks = k1
#具体定义source
a3.sources.r1.type = exec
a3.sources.r1.command = tail -f /root/trainings/logs/history.log
#具体定义channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 10000
a3.channels.c1.transactionCapacity = 100
#具体定义sink
a3.sinks.k1.type = logger
#组装source、channel、sink
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1</pre>

2.启动Flume

# cd /root/trainings/apache-flume-1.8.0-bin/
# bin/flume-ng agent -n a3 -f myagent/a3.conf -c conf -Dflume.root.logger=INFO,console</pre>

参数agent 的选项说明:

  • -n 指定agent的名字
  • -f 指定agent配置文件
  • -c 指定flume配置文件的目录
  • -Dflume.root.logger=INFO,console 将INFO级别以上的日志显示在控制台</pre>

3.向日志文件追加内容

打开一个新的终端,以追加的方式写入数据(注意:/root/trainings/logs/history.log必须实现存在):

# echo "I love Beijing" >> /root/trainings/logs/history.log
# echo "I love China" >> /root/trainings/logs/history.log
# echo "Beijing is the capital of China" >> /root/trainings/logs/history.log

4.观察Flume采集到数据

在Flume的窗口中会打印出采集到数据:

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容