模拟使用Flume监听日志变化_并且把增量的日志文件写入到hdfs中

栏目: 服务器 · 发布时间: 8年前

内容简介:模拟使用Flume监听日志变化_并且把增量的日志文件写入到hdfs中

1.采集日志文件时一个很常见的现象

采集需求:比如业务系统使用log4j生成日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs中。

1.1.根据需求,首先定义一下3大要素:

采集源,即source—监控日志文件内容更新:exec ‘tail -F file’

下沉目标,即sink—HDFS文件系统:hdfs sink

Source和sink之间的传递通道—-channel,可用file channel也可以用 内存channel。

1.2.进入/home/tuzq/software/apache-flume-1.6.0-bin/agentconf,编写配置文件tail-hdfs.conf,文件内容如下:

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
## exec表示flume回去调用给的命令,然后从给的命令的结果中去拿数据
a1.sources.r1.type = exec
## 使用tail这个命令来读数据
a1.sources.r1.command = tail -F /home/tuzq/software/flumedata/test.log
a1.sources.r1.channels = c1

# Describe the sink
## 表示下沉到hdfs,类型决定了下面的参数
a1.sinks.k1.type = hdfs
## sinks.k1只能连接一个channel,source可以配置多个
a1.sinks.k1.channel = c1
## 下面的配置告诉用hdfs去写文件的时候写到什么位置,下面的表示不是写死的,而是可以动态的变化的。表示输出的目录名称是可变的
a1.sinks.k1.hdfs.path = /flume/tailout/%y-%m-%d/%H%M/
##表示最后的文件的前缀
a1.sinks.k1.hdfs.filePrefix = events-
## 表示到了需要触发的时间时,是否要更新文件夹,true:表示要
a1.sinks.k1.hdfs.round = true
## 表示每隔1分钟改变一次
a1.sinks.k1.hdfs.roundValue = 1
## 切换文件的时候的时间单位是分钟
a1.sinks.k1.hdfs.roundUnit = minute
## 表示只要过了3秒钟,就切换生成一个新的文件
a1.sinks.k1.hdfs.rollInterval = 3
## 如果记录的文件大于20字节时切换一次
a1.sinks.k1.hdfs.rollSize = 20
## 当写了5个事件时触发
a1.sinks.k1.hdfs.rollCount = 5
## 收到了多少条消息往dfs中追加内容
a1.sinks.k1.hdfs.batchSize = 10
## 使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件类型,默认是Sequencefile,可用DataStream:为普通文本
a1.sinks.k1.hdfs.fileType = DataStream

# Use a channel which buffers events in memory
##使用内存的方式
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

1.3.编写完成之后,启动flume,执行的命令是:

[root@hadoop1 flumedata]#cd /home/tuzq/software/apache-flume-1.6.0-bin/agentconf
[root@hadoop1 flumedata]#bin/flume-ng agent -c conf -f agentconf/tail-hdfs.conf -n a1

1.4.通过写一个死循环往test.log中写数据的方式模式日志文件增长

编写 shell 脚本,模拟日志增长变化。

[root@hadoop1 flumedata]# cd /home/tuzq/software/flumedata
[root@hadoop1 flumedata]# while true
>do
> date >> test.log
> sleep 2
> done

查看日志变化

[root@hadoop1 ~]# cd /home/tuzq/software/flumedata/
[root@hadoop1 flumedata]# ls
access.log  error.log  test.log
[root@hadoop1 flumedata]# tail -f test.log 
2017年 06月 13日 星期二 22:02:22 CST
2017年 06月 13日 星期二 22:02:24 CST
2017年 06月 13日 星期二 22:02:26 CST
2017年 06月 13日 星期二 22:02:28 CST
2017年 06月 13日 星期二 22:02:30 CST
2017年 06月 13日 星期二 22:02:32 CST

通过上面的文件,可以看到test.log在不停的追加数据。

到hdfs中进行查看,效果如下:

[root@hadoop1 ~]# hdfs dfs -ls /
Found 5 items
drwxr-xr-x   - root supergroup          0 2017-06-13 12:01 /40000
drwxr-xr-x   - root supergroup          0 2017-06-13 22:01 /flume
-rw-r--r--   3 root supergroup       3719 2017-06-10 12:11 /kms.sh
drwxrwxrwx   - root supergroup          0 2017-06-10 22:06 /tmp
drwxr-xr-x   - root supergroup          0 2017-06-10 22:27 /user
[root@hadoop1 ~]# hdfs dfs -ls /flume
Found 1 items
drwxr-xr-x   - root supergroup          0 2017-06-13 22:01 /flume/tailout
[root@hadoop1 ~]# hdfs dfs -ls /flume/tailout
Found 1 items
drwxr-xr-x   - root supergroup          0 2017-06-13 22:03 /flume/tailout/17-06-13
[root@hadoop1 ~]# hdfs dfs -ls /flume/tailout/17-06-13
Found 4 items
drwxr-xr-x   - root supergroup          0 2017-06-13 22:01 /flume/tailout/17-06-13/2201
drwxr-xr-x   - root supergroup          0 2017-06-13 22:03 /flume/tailout/17-06-13/2202
drwxr-xr-x   - root supergroup          0 2017-06-13 22:04 /flume/tailout/17-06-13/2203
drwxr-xr-x   - root supergroup          0 2017-06-13 22:04 /flume/tailout/17-06-13/2204
[root@hadoop1 ~]# hdfs dfs -ls /flume/tailout/17-06-13
Found 5 items
drwxr-xr-x   - root supergroup          0 2017-06-13 22:01 /flume/tailout/17-06-13/2201
drwxr-xr-x   - root supergroup          0 2017-06-13 22:03 /flume/tailout/17-06-13/2202
drwxr-xr-x   - root supergroup          0 2017-06-13 22:04 /flume/tailout/17-06-13/2203
drwxr-xr-x   - root supergroup          0 2017-06-13 22:05 /flume/tailout/17-06-13/2204
drwxr-xr-x   - root supergroup          0 2017-06-13 22:05 /flume/tailout/17-06-13/2205
[root@hadoop1 /]# hdfs dfs -ls /flume/tailout/17-06-13
Found 6 items
drwxr-xr-x   - root supergroup          0 2017-06-13 22:01 /flume/tailout/17-06-13/2201
drwxr-xr-x   - root supergroup          0 2017-06-13 22:03 /flume/tailout/17-06-13/2202
drwxr-xr-x   - root supergroup          0 2017-06-13 22:04 /flume/tailout/17-06-13/2203
drwxr-xr-x   - root supergroup          0 2017-06-13 22:05 /flume/tailout/17-06-13/2204
drwxr-xr-x   - root supergroup          0 2017-06-13 22:06 /flume/tailout/17-06-13/2205
drwxr-xr-x   - root supergroup          0 2017-06-13 22:06 /flume/tailout/17-06-13/2206
[root@hadoop1 /]

通过上面的案例可以知道,增加的日志文件已经被写入到HDFS中。

可供参考的文件: http://blog.csdn.net/tototuzuoquan/article/details/73194903


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Seasoned Schemer

The Seasoned Schemer

Daniel P. Friedman、Matthias Felleisen / The MIT Press / 1995-12-21 / USD 38.00

drawings by Duane Bibbyforeword and afterword by Guy L. Steele Jr.The notion that "thinking about computing is one of the most exciting things the human mind can do" sets both The Little Schemer (form......一起来看看 《The Seasoned Schemer》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具