2027年最全大数据之flume(一)(1) - 向监控目录中添加文件
Name the components on this agent
a1.sources = r1 a1.sinks = k1 a1.channels = c1
Describe/configure the source
a1.sources.r1.type = netcat a1.sources.r1.bind = localhost a1.sources.r1.port = 55555
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#channel总容量 a1.channels.c1.transactionCapacity = 100#单次事务的容量
Bind the source and sink to the channel
a1.sources.r1.channels = c1#source可以绑定多个channel a1.sinks.k1.channel = c1#一个sink只能绑定一个channel
执行命令
[root@hadoop101 apache-flume-1.9.0-bin] bin/flume-ng agent -n a1 -c conf/ -f job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
在控制台查看消息日志

##### 3.2、监控单个文件
步骤:
1. 创建flume配置文件
2. 执行配置文件并开启监控
3. 开启Hive生成日志
4. 查看HDFS上数据
操作:
1. 创建conf文件flume-file-hdfs.conf
a2.sources = r2 a2.sinks = k2 a2.channels = c2
Describe/configure the source
a2.sources.r2.type = exec a2.sources.r2.command = tail -F /tmp/root/hive.log a2.sources.r2.shell = /bin/bash -c
Describe the sink
a2.sinks.k2.type = hdfs a2.sinks.k2.hdfs.path = hdfs://hadoop100: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 #是否使用本地时间戳 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 = 60 #设置每个文件的滚动大小 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 = 1000 a2.channels.c2.transactionCapacity = 100
Bind the source and sink to the channel
a2.sources.r2.channels = c2 a2.sinks.k2.channel = c2
**注意: a2.sources.r2.command中hive的日志路径和 a2.sinks.k2.hdfs.path要根据自己的环境来写,查看路径是否正确使用命令。**
hadoop fs -ls hdfs://hadoop100:8020/
**如果能看到HDFS的目录说明没问题,如下图。**

2. 运行命令
bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
3. 启动Hive,可使用命令监控Hive日志 tail -F /tmp/root/hive.log
4. 查看HDFS是否有flume目录

##### 3.3、监控目录下的多个新文件
步骤:
1. 创建符合条件的flume配置文件
2. 执行配置文件开启监控
3. 向监控目录中添加文件
4. 查看HDFS检查文件是否上传成功
5. 查看监控目录中的文件是否已被标记
操作:
1. 创建conf文件flume-dir-hdfs.conf
a2.sources = r2 a2.sinks = k2 a2.channels = c2
Describe/configure the source
a2.sources.r2.type = spooldir a2.sources.r2.spoolDir=/export/soft/apache-flume-1.9.0-bin/upload a2.sources.r2.fileSuffix=.COMPLETED#上传完成后修改文件后缀名进行标记 a2.sources.r2.fileHeader=true a2.sources.r2.ignorePattern=([^ ]*.tmp)#忽略该类型文件
Describe the sink
a2.sinks.k2.type = hdfs a2.sinks.k2.hdfs.path = hdfs://hadoop100:8020/flume/upload/%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 #设置文件类型,可支持压缩 a2.sinks.k2.hdfs.fileType = DataStream #多久生成一个新的文件 a2.sinks.k2.hdfs.rollInterval = 60 #设置每个文件的滚动大小 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 = 1000 a2.channels.c2.transactionCapacity = 100
Bind the source and sink to the channel
a2.sources.r2.channels = c2 a2.sinks.k2.channel = c2
2. 创建upload文件夹
3. 运行flume
bin/flume-ng agent -n a2 -c conf/ -f job/flume-dir-hdfs.conf
4. 查看HDFS和文件标记


**注意:上传的文件不能重名,flume会报错!虽然会上传成功,但重名文件无法进行标记。**
##### 3.4、监控目录下的多个追加的文件
Exec Source适合监控一个实时追加的文件,不能实现断点续传;Spooldir Source适合同步新文件,不适合对实时追加的日志进行监听和同步;而Taildir Source适合用于监听多个实时追加的文件并能实现断点续传。
步骤:
1. 创建flume配置文件
2. 执行配置文件开启监控
3. 向监控文件中追加内容
4. 查看HDFS上的数据是否同步追加
操作:
1. 创建配置文件
a2.sources = r2 a2.sinks = k2 a2.channels = c2
Describe/configure the source
a2.sources.r2.type = TAILDIR a2.sources.r2.positionFile=/export/soft/apache-flume-1.9.0-bin/job/tail_dir.json a2.sources.r2.filegroups=f1 f2 a2.sources.r2.filegroups.f1=/export/soft/apache-flume-1.9.0-bin/job/f1/.file. a2.sources.r2.filegroups.f2=/export/soft/apache-flume-1.9.0-bin/job/f2/.log.
Describe the sink
a2.sinks.k2.type = hdfs a2.sinks.k2.hdfs.path = hdfs://hadoop100:8020/flume/upload2/%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 #设置文件类型,可支持压缩 a2.sinks.k2.hdfs.fileType = DataStream #多久生成一个新的文件 a2.sinks.k2.hdfs.rollInterval = 20 #设置每个文件的滚动大小 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 = 1000 a2.channels.c2.transactionCapacity = 100
Bind the source and sink to the channel
a2.sources.r2.channels = c2 a2.sinks.k2.channel = c2
2. 创建上传文件夹f1和f2 拼多多帮助力入口
3. 运行flume


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
5)]
[外链图片转存中...(img-2DbCEG0C-1714676027255)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**