Flume 收集Nginx日志到Hdfs Tail-to-hdfs sink

本文详细介绍Flume配置,包括数据源(source)、接收器(sink)及通道(channel)的具体配置方式。通过实例展示如何配置tail-F数据源、HDFS接收器等组件,并设置合理的拦截器、通道类型及其他参数。
nginx,access.log日志约8000条/s,每100w条数据约253M,需要2min
agent1.sources = source1
agent1.sinks = sink1
agent1.channels = channel1

# Describe/configure spooldir source1
#agent1.sources.source1.type = spooldir
#agent1.sources.source1.spoolDir = /var/log/apache/flumeSpool1
#agent1.sources.source1.fileHeader = true

# Describe/configure tail -F source1
agent1.sources.source1.type = exec 
agent1.sources.source1.command = tail -F /tmp/log.log
agent1.sources.source1.channels = channel1

# Describe/configure nc source1
#agent1.sources.source1.type = netcat
#agent1.sources.source1.bind = localhost
#agent1.sources.source1.port = 44444

#configure host for source
agent1.sources.source1.interceptors = i1
agent1.sources.source1.interceptors.i1.type = host
agent1.sources.source1.interceptors.i1.hostHeader = hostname

# Describe sink1
#agent1.sinks.sink1.type = logger

agent1.sinks.sink1.type = hdfs
#a1.sinks.k1.channel = c1
#agent1.sinks.sink1.hdfs.path =hdfs://xxx:9000/tmp/tail/%y-%m-%d/%H%M%S
agent1.sinks.sink1.hdfs.path =hdfs://xxx:9000/tmp/tail/%y-%m-%d/%H
agent1.sinks.sink1.hdfs.filePrefix = %{hostname}/events-
agent1.sinks.sink1.hdfs.maxOpenFiles = 5000 
agent1.sinks.sink1.hdfs.batchSize= 500
agent1.sinks.sink1.hdfs.fileType = DataStream
agent1.sinks.sink1.hdfs.writeFormat =Text
agent1.sinks.sink1.hdfs.rollSize = 0
agent1.sinks.sink1.hdfs.rollCount = 1000000
agent1.sinks.sink1.hdfs.rollInterval = 600
#agent1.sinks.sink1.hdfs.round = true
#agent1.sinks.sink1.hdfs.roundValue = 10
#agent1.sinks.sink1.hdfs.roundUnit = minute
agent1.sinks.sink1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in memory
agent1.channels.channel1.type = memory
agent1.channels.channel1.keep-alive = 120
agent1.channels.channel1.capacity = 500000
agent1.channels.channel1.transactionCapacity = 600

# Bind the source and sink to the channel
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1
这里使用Flume采集日志文件nginx.log中的日志数据,并将采集的日志数据写入到HDFS。在虚拟机Hadoop1的/export/servers/flume-1.9.0/conf/目录下创建文件WebLog.conf,在该文件中指定采集方案,具体内容如文件11-2所示。 文件11-2 WebLog.conf 1 a1.sources = r1 2 a1.sinks = k1 3 a1.channels = c1 4 a1.sources.r1.type = exec 5 a1.sources.r1.command = tail -f /export/data/weblog/nginx.log 6 a1.sinks.k1.type = hdfs 7 a1.sinks.k1.hdfs.path = /weblog/%y-%m-%d/%H-%M/ 8 a1.sinks.k1.hdfs.filePrefix = itcast- 9 a1.sinks.k1.hdfs.fileType = DataStream 10 a1.sinks.k1.hdfs.useLocalTimeStamp = true 11 a1.channels.c1.type = memory 12 a1.channels.c1.capacity = 1000 13 a1.channels.c1.transactionCapacity = 100 14 a1.sources.r1.channels = c1 15 a1.sinks.k1.channel = c1 上述指定的采集方案中,指定Source类型为Exec Source,用于持续监控文件nginx.log内数据的变化,实现实时采集日志数据的功能。指定Sink类型为HDFS Sink,用于将实时采集的日志数据写入HDFS的指定目录。指定Channel类型为Memory Channel,用于将实时采集的日志数据缓存在内存中。 接下来,确保LogDataSource-1.0-SNAPSHOT.jar文件处于运行状态下,根据指定的采集方案,在虚拟机Hadoop1启动一个Agent,采集日志文件nginx.log中的日志数据,在Flume的安装目录执行如下命令。 $ bin/flume-ng agent --name a1 --conf conf/ --conf-file conf/WebLog.conf \ -Dflume.root.logger=INFO,console 上述命令的执行效果如图11-6所示。 图11-6 启动Agent 从图11-6可以看出,Agent的Source和Sink已经开始运行,并且将采集的数据写入到HDFS指定目录的临时文件中。 此时,我们可以通过HDFS的Web UI查看/weblog/22-09-02/23-34目录中的内容,如图11-7所示。 图11-7 查看/weblog/22-09-02/23-34目录中的内容 从图11-7可以看出,/weblog/22-09-02/23-34目录中存在多个文件,并且这些文件已经由临时文件滚动为目标文件,这些目标文件中存放着采集的日志数据,例如临时文件itcast-.1662132876787.tmp已经滚动为目标文件itcast-.1662132876787。 接下来,我们以查看目标文件itcast-.1662132876787的前5行数据为例,查看采集的数据内容,具体命令如下。 $ hdfs dfs -cat /weblog/22-09-02/23-34/itcast-.1662132876787 | head -5 上述命令执行完成的效果如图11-8所示。 图11-8 查看采集的数据 从图11-8可以看出,Flume采集的数据格式与模拟生成的日志数据格式一致,说明成功从日志文件nginx.log中采集模拟生成的日志数据。
最新发布
06-14
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值