Flume的四个入门案例(官方)

Flume入门案例一:监控端口数据

1)案例需求

使用 Flume 监听一个端口,收集该端口数据,并打印到控制台

2)需求分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tKrNNDd8-1643038993141)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124224743532.png)]

3)实现步骤

(1)安装 netcat 工具

sudo yum install -y nc

(2)判断 44444 端口是否被占用

sudo netstat -nlp | grep 44444

(3)若被占用可用以下命令杀死端口进程命令

sudo fuser -k -n tcp 44444

(4)在flume目录下创建job文件夹,并进入job文件夹(一般我们设定的配置文件放在此处)

(5)在job创建Flume Agent 配置文件 net-flum-logger.conf,添加如下内容

添加内容如下:
# 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 = 44444

# 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

(6)开启flume监听端口

第一种方法(推荐):

bin/flume-ng agent -c conf/ -n a1 -f job/net-flum-logger.conf -Dflume.root.logger=INFO,console

第二种方法:

bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console

(7)使用 netcat 工具向本机的 44444 端口发送内容

(8)在 Flume 监听页面观察接收数据情况

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yAi31E61-1643038993142)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124230009628.png)]

(9)配置文件解析
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-19Rfbj2H-1643038993142)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124230051060.png)]

Flume入门案例二:实时监控单个追加文件

1)案例需求:

实时监控 Hive 日志,并上传到 HDFS 中

2)需求分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HONPPelX-1643038993143)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124230158002.png)]

3)实现步骤

(1)在job文件夹下创建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
a2.sources.r2.command = tail -F /opt/module/hive/logs/hive.log

# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop102: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

(2)运行Flume

bin/flume-ng agent -c conf/ -n a2 -f job/flume-file-hdfs.conf

(3)运行Hive,进行一些操作,上Hadoop上查看日志
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s2fyGr8E-1643038993143)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124230801986.png)]
在这里插入图片描述

(4)相关配置文件详解
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-STQlLkWv-1643038993144)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124231038441.png)]

Flume入门案例三:实时监控目录下多个新文件

1)案例需求

使用 Flume 监听整个目录的文件,并上传至 HDFS

2)需求分析

3)实现步骤

(1)在job下创建flume-dir-hdfs.conf

a3.sources = r3
a3.sinks = k3
a3.channels = c3

# Describe/configure the source
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /opt/module/flume/upload
a3.sources.r3.fileSuffix = .COMPLETED
a3.sources.r3.fileHeader = true

#忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)

# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop102:8020/flume/upload/%Y%m%d/%H

#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k3.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a3.sinks.k3.hdfs.rollCount = 0

# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100

# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

(2)启动Flume监控

bin/flume-ng agent -c conf/ -n a3 -f job/flume-dir-hdfs.conf

(3)在flume下创建upload目录

我们配置信息里监控的是a3.sources.r3.spoolDir = /opt/module/flume/upload,在此目录下进行增添文件操作
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IdrPRJ1v-1643038993145)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124231729311.png)]
(4)HDFS查看信息
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HCtaKro9-1643038993147)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124231758631.png)]
(5)配置信息详解
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CPkpGeO1-1643038993148)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124231838436.png)]

Flume入门案例四:实时监控目录下的多个追加文件

Exec source 适用于监控一个实时追加的文件,不能实现断点续传;Spooldir Source 适合用于同步新文件,但不适合对实时追加日志的文件进行监听并同步;而 Taildir Source 适合用于监听多个实时追加的文件,并且能够实现断点续传。

1)案例需求

使用 Flume 监听整个目录的实时追加文件,并上传至 HDFS

2)需求分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RnvbPFqD-1643038993149)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124231946810.png)]

3)实现步骤

(1)在job下创建配置文件 flume-taildir-hdfs.con

a3.sources = r3
a3.sinks = k3
a3.channels = c3

# Describe/configure the source
a3.sources.r3.type = TAILDIR
a3.sources.r3.positionFile = /opt/module/flume/tail_dir.json
a3.sources.r3.filegroups = f1 f2
a3.sources.r3.filegroups.f1 = /opt/module/flume/files/.*file.*
a3.sources.r3.filegroups.f2 = /opt/module/flume/files2/.*log.*

# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop102:8020/flume/upload2/%Y%m%d/%H

#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k3.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a3.sinks.k3.hdfs.rollCount = 0

# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100

# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

(2)先在flume目录下创建两个目录files,files2(否则会报错)

(3)启动Flume监控

bin/flume-ng agent -c conf/ -n a3 -f job/flume-taildir-hdfs.conf

(4)向 files 文件夹中追加内容

注意:files目录下里添加的文件信息包含file、files2目录下里添加的文件信息包含log,才能被记录!!!
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UPtB5eD1-1643038993150)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220124234103767.png)]
(5)查看HDFS
在这里插入图片描述
(6)配置文件详解
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值