案例需求:
实时监控 Hive 日志,并上传到 HDFS 中
需求分析:
实现步骤:
注意:Flume 要想将数据输出到 HDFS,依赖 Hadoop 相关 jar 包 ,确认 Hadoop 和 Java 环境变量配置正确
在job目录下创建 flume-file-hdfs.conf 文件
vim job/flume-file-hdfs.conf
注:要想读取 Linux 系统中的文件,就得按照 Linux 命令的规则执行命令。由于 Hive 日志在 Linux 系统中所以读取文件的类型选择:exec 即 execute 执行的意思。表示执行Linux 命令来读取文件
flume-file-hdfs.conf文件内容如下
# Name the components on this agent a2.sources = r2 a2.sinks = k2 a2.channels = c2 # Describe/configure the source a2.sources.r2.type = exec # source执行的linux命令,监控的hive日志文件路径改为自己的hive日志文件的所在路径即可 a2.sources.r2.command = tail -F /export/servers/hive/logs/hive.log # Describe the sink a2.sinks.k2.type = hdfs # 定义hdfs的文件上传路径 %Y%m%d/%H:时间的正则表达式,根据event中的head中的时间戳来创建文件夹 a2.sinks.k2.hdfs.path = hdfs://hadoop101:8020/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 # 是否使用本地时间戳,默认为false,必须设置为true,如果为false,那么event中的head中将没有时间戳 a2.sinks.k2.hdfs.useLocalTimeStamp = true # 积攒多少个Event才flush到HDFS一次 a2.sinks.k2.hdfs.batchSize = 100 # 设置文件类型,可支持压缩 a2.sinks.k2.hdfs.fileType = DataStream # 只要满足下面三个条件中的一个就会创建文件 # 多久生成一个新的文件,单位为秒 a2.sinks.k2.hdfs.rollInterval = 30 # 设置每个文件的滚动大小,接近128M a2.sinks.k2.hdfs.rollSize = 134217700 # 0:文件的滚动与Event数量无关 a2.sinks.k2.hdfs.rollCount = 0 # Use a channel which buffers events in memory a2.channels.c2.type = memory a2.channels.c2.capacity = 1000 a2.channels.c2.transactionCapacity = 100 # Bind the source and sink to the channel a2.sources.r2.channels = c2 a2.sinks.k2.channel = c2
注意:对于所有与时间相关的转义序列,Event Header 中必须存在以 “timestamp”的key(除非 hdfs.useLocalTimeStamp 设置为 true,此方法会使用 TimestampInterceptor 自动添加 timestamp)
运行Flume
bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
启动hadoop和hive,并操作 Hive 产生日志
在 HDFS 上查看文件



