ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-25 00:03:25  阅读:207  来源: 互联网

标签:Flume hdfs sinks sources k3 案例 a3 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)配置文件详解
在这里插入图片描述

标签:Flume,hdfs,sinks,sources,k3,案例,a3,flume,入门
来源: https://blog.csdn.net/mynameisgt/article/details/122677300

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有