大数据之Flume:Flume实时监控单个文件

实时监控单个文件
1)案例需求:实时监控Hive日志,并上传到HDFS中
2)需求分析:
在这里插入图片描述
3)实现步骤:
组件选择 Exec Source,HDFS sink
1.Flume要想将数据输出到HDFS,必须持有Hadoop相关jar包

commons-configuration-1.6.jar、
hadoop-auth-2.7.2.jar、
hadoop-common-2.7.2.jar、
hadoop-hdfs-2.7.2.jar、
commons-io-2.4.jar、
htrace-core-3.1.0-incubating.jar
拷贝到/opt/module/flume/lib文件夹下。
2.创建flume-file-hdfs.conf文件
创建文件
[hadoop@hadoop101 job]$ vim flume-file-hdfs.conf
注:要想读取Linux系统中的文件,就得按照Linux命令的规则执行命令。由于Hive日志在Linux系统中所以读取文件的类型选择:exec即execute执行的意思。表示执行Linux命令来读取文件。
添加如下内容

# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2

# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /opt/module/hive/logs/hive.log
a2.sources.r2.shell = /bin/bash -c

# Describe the sink
a2.sinks.k2.type = hdfs
//一旦路径中含有基于时间的转义序列,要求event的header中必须有useLocalTimeStamp =时间戳
a2.sinks.k2.hdfs.path = hdfs://hadoop101:9000/flume/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k2.hdfs.filePrefix = logs-

//一下三个和目录滚动相关,,目录一旦设置了时间转义序列,基于时间戳滚动
#是否按照时间滚动文件夹
a2.sinks.k2.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k2.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k2.hdfs.roundUnit = hour
#是否使用本地时间戳

a2.sinks.k2.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a2.sinks.k2.hdfs.batchSize = 100
# 以下三个参数和文件滚动相关,以下三个参数为或的关系,为0表示禁用
#多久生成一个新的文件
a2.sinks.k2.hdfs.rollInterval = 10
#设置每个文件的滚动大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a2.sinks.k2.hdfs.rollCount = 0

# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 10000
a2.channels.c2.transactionCapacity = 1000

# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2

3.运行Flume

[hadoop@hadoop101 flume]$ bin/flume-ng agent -c conf/ -n a2 -f job/flume-file-hdfs.conf

4.开启Hadoop和Hive并操作Hive产生日志

[hadoop@hadoop101 hadoop-2.7.2]$ sbin/start-dfs.sh
[hadoop@hadoop102 hadoop-2.7.2]$ sbin/start-yarn.sh
[hadoop@hadoop101 hive]$ bin/hive
hive (default)>

5.在HDFS上查看文件。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43597208/article/details/113255938