Flume 之exec source、memory channel、hdfs sink
文章目录
- Flume 之exec source、memory channel、hdfs sink
- 2.2 实时监控单个追加文件
2.2 实时监控单个追加文件
1)案例需求:实时监控 Hive 日志,并上传到HDFS中
- 注意:tail -f (失败后不重试) tail -F (失败后重试一般3次)
2)需求分析:
3)实现步骤:
- (1)Flume 需要依赖 Hadoop 相关的 jar包,才能将数据输出到 HDFS 上
- 检查/etc/profile.d/my_env.sh文件,确认Hadoop和Java环境变量配置正确
- (2)创建 flume-file-logger.conf,flume-file-hdfs1.conf,flume-file-hdfs2.conf 文件
案例2版本一
[xiaoxq@hadoop105 jobs]$ vim file-flume-logger.conf
- 注:要想读取 Linux 系统中的文件,就得按照 Linux 命令的规则执行命令。由于 Hive 日志在 Linux 系统中所以读取文件的类型选择:exec 即 execute 执行的意思。表示执行 Linux 命令来读取文件。
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/module/hive-3.1.2/logs/hive.log
# Describe the sink
a1.sinks.k1.type = logger
# 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
[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent -c conf/ -f jobs/file-flume-logger.conf -n a1 -Dflume.root.logger=INFO,console
- 在hive中进行操作时日志会发生滚动
- 因为 /opt/module/hive-3.1.2/logs/hive.log 本身日志太长,可以选择自己创建在 /opt/module/datas/hive.log 自己通过 echo 命令追加到至文件中
- 将日志上传到HDFS上
案例2版本二
(1) 创建配置文件
[xiaoxq@hadoop105 jobs]$ vim file-flume-hdfs1.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/module/datas/hive.log
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume
# 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
(2) 运行Flume
[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent -c conf/ -f jobs/file-flume-hdfs1.conf -n a1
(3)开启 Hadoop 和 Hive 并操作 Hive 产生日志(或者自己追加)
- 创建日志存放文件夹
[xiaoxq@hadoop105 module]$ mkdir datas
[xiaoxq@hadoop105 module]$ cd datas
[xiaoxq@hadoop105 datas]$ ll
总用量 0
[xiaoxq@hadoop105 datas]$ vim hive.log
[xiaoxq@hadoop105 datas]$ ll
总用量 0
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 18:32 hive.log
- 先自己追加一条信息到 hive.log 中
[xiaoxq@hadoop105 datas]$ ll
总用量 0
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 18:32 hive.log
[xiaoxq@hadoop105 datas]$ echo '1111' >> hive.log
[xiaoxq@hadoop105 datas]$ ll
总用量 4
-rw-rw-r--. 1 xiaoxq xiaoxq 5 8月 7 18:43 hive.log
(4)在HDFS上查看文件。
- 查看 HDFS 上会出现 flume文件目录
- 出现乱码,官网上查看得知文件的类型默认是二进制格式(二进制格式,数据流格式,可压缩格式)
- 需要设置添加文件类型DataStream,使其不乱码
- 重新启动,会默认打印10行日志(tail -F)
- 再执行追加命令到日志(第一次启动会打印末尾的10行(tail -F))
[xiaoxq@hadoop105 datas]$ echo 'ababa' >> hive.log
[xiaoxq@hadoop105 datas]$ echo 'wukong' >> hive.log
[xiaoxq@hadoop105 datas]$ echo 'bajie' >> hive.log
案例2版本三(企业应用版)
(1) 创建配置文件
[xiaoxq@hadoop105 jobs]$ vim file-flume-hdfs2.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/module/datas/hive.log
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path =/flume/%Y%m%d/%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = logs-
#是否对时间戳取整
a1.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a1.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a1.sinks.k1.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a1.sinks.k1.hdfs.rollInterval = 10
#设置每个文件的滚动大小
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a1.sinks.k1.hdfs.rollCount = 0
# 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
注意:
- 对于所有与时间相关的转义序列,Event Header 中必须存在以 “timestamp”的 key(除非hdfs.useLocalTimeStamp 设置为 true,此方法会使用 TimestampInterceptor 自动添加timestamp)。
- a1.sinks.k1.hdfs.useLocalTimeStamp = true
(2) 运行Flume
[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent -c conf/ -f jobs/file-flume-hdfs2.conf -n a1
(3) 开启 Hadoop 和 Hive 并操作 Hive 产生日志(之前已经添加了内容,故不再操作)
(4)在HDFS上查看文件。
- 对比
配置文件小结:
sink hdfs
hdfs 路径 /flume/%Y%m%d/%H 配置以时间的文件夹
fileType Datastream 配置能看懂的格式
------滚动文件
rollInterval 按多久滚动一次hdfs文件 默认s为单位 在中小企业配成3600秒
rollSize 按多大滚动一次hdfs文件 默认字节为单位 在企业一般比块(128M)大小 小一些
rollCount 按事件个数滚动hdfs文件 默认单位是event 在企业常设为0
-----滚动文件夹
round 表示对时间戳取整 取整单位由下面两个参数决定 默认是flase
roundValue 表示滚动文件夹的值 默认1
roundUint 表示滚动文件夹的单位 默认s