最新【Flume】(六)Flume 开发实战案例分享_flume 多数据源,大数据开发自定义view面试

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

使用 Flume 监听整个目录的文件。

3.2 需求分析:

在这里插入图片描述

3.3 实现步骤:
1.创建配置文件 flume-dir-hdfs.conf

创建一个文件

[root@hadoop102 job]$ touch flume-dir-hdfs.conf

打开文件

[root@hadoop102 job]$ vim 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:9000/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 = 600 
#设置每个文件的滚动大小大概是 128M 
a3.sinks.k3.hdfs.rollSize = 134217700 
#文件的滚动与 Event 数量无关 
a3.sinks.k3.hdfs.rollCount = 0 
#最小冗余数 
a3.sinks.k3.hdfs.minBlockReplicas = 1 

# 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.启动监控文件夹命令
[root@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir-hdfs.conf

说明: 在使用 Spooling Directory Source 时

  1. 不要在监控目录中创建并持续修改文件
  2. 上传完成的文件会以.COMPLETED 结尾
  3. 被监控文件夹每 500 毫秒扫描一次文件变动
3.向 upload 文件夹中添加文件

在/opt/module/flume 目录下创建 upload 文件夹

[root@hadoop102 flume]$ mkdir upload

向 upload 文件夹中添加文件

[root@hadoop102 upload]$ touch demo.txt 
[root@hadoop102 upload]$ touch demo.tmp 
[root@hadoop102 upload]$ touch demo.log

4.查看 HDFS 上的数据

在这里插入图片描述

5.等待 1s,再次查询 upload 文件夹
[root@hadoop102 upload]$ ll 
总用量 0 -rw-rw-r--. 1 demodemo 0 5 月 20 22:31 demo.log.COMPLETED 
-rw-rw-r--. 1 demodemo0 5 月 20 22:31 demo.tmp 
-rw-rw-r--. 1 demodemo0 5 月 20 22:31 demo.txt.COMPLETED

四、单数据源多出口案例(选择器)

单 Source 多 Channel、Sink 如图所示。

单 Source 多 Channel、Sink

4.1 案例需求:

使用 Flume-1 监控文件变动,Flume-1 将变动内容传递给 Flume-2,Flume-2 负责存储到 HDFS。同时 Flume-1 将变动内容传递给 Flume-3,Flume-3 负责输出到 Local FileSystem。

4.2 需求分析:

在这里插入图片描述

4.3 实现步骤:

0.准备工作
在/opt/module/flume/job 目录下创建 group1 文件夹

[root@hadoop102 job]$ cd group1/ 

在/opt/module/datas/目录下创建 flume3 文件夹

[root@hadoop102 datas]$ mkdir flume3

1.创建 flume-file-flume.conf

配置 1 个接收日志文件的 source 和两个 channel、两个 sink,分别输送给 flume-flume-hdfs 和 flume-flume-dir。

创建配置文件并打开

[root@hadoop102 group1]$ touch flume-file-flume.conf 
[root@hadoop102 group1]$ vim flume-file-flume.conf

添加如下内容

# Name the components on this agent 
a1.sources = r1 
a1.sinks = k1 k2 
a1.channels = c1 c2 
# 将数据流复制给所有 channel 
a1.sources.r1.selector.type = replicating 

# Describe/configure the source 
a1.sources.r1.type = exec 
a1.sources.r1.command = tail -F /opt/module/hive/logs/hive.log 
a1.sources.r1.shell = /bin/bash -c 

# Describe the sink
a1.sinks.k1.type = avro 
a1.sinks.k1.hostname = hadoop102 
a1.sinks.k1.port = 4141 

a1.sinks.k2.type = avro 
a1.sinks.k2.hostname = hadoop102 
a1.sinks.k2.port = 4142 

# Describe the channel 
a1.channels.c1.type = memory 
a1.channels.c1.capacity = 1000 
a1.channels.c1.transactionCapacity = 100 

a1.channels.c2.type = memory 
a1.channels.c2.capacity = 1000 
a1.channels.c2.transactionCapacity = 100 

# Bind the source and sink to the channel 
a1.sources.r1.channels = c1 c2 
a1.sinks.k1.channel = c1 
a1.sinks.k2.channel = c2

注:Avro 是由 Hadoop 创始人 Doug Cutting 创建的一种语言无关的数据序列化和 RPC 框 架。

注:RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程 序上请求服务,而不需要了解底层网络技术的协议。

2.创建 flume-flume-hdfs.conf

配置上级 Flume 输出的 Source,输出是到 HDFS 的 Sink。

创建配置文件并打开

[root@hadoop102 group1]$ touch flume-flume-hdfs.conf 
[root@hadoop102 group1]$ vim flume-flume-hdfs.conf

添加如下内容

# Name the components on this agent 
a2.sources = r1 
a2.sinks = k1 
a2.channels = c1 

# Describe/configure the source 
a2.sources.r1.type = avro 
a2.sources.r1.bind = hadoop102 
a2.sources.r1.port = 4141 

# Describe the sink 
a2.sinks.k1.type = hdfs 
a2.sinks.k1.hdfs.path = hdfs://hadoop102:9000/flume2/%Y%m%d/%H 
#上传文件的前缀 
a2.sinks.k1.hdfs.filePrefix = flume2- 
#是否按照时间滚动文件夹 
a2.sinks.k1.hdfs.round = true 
#多少时间单位创建一个新的文件夹 
a2.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位 
a2.sinks.k1.hdfs.roundUnit = hour 
#是否使用本地时间戳 
a2.sinks.k1.hdfs.useLocalTimeStamp = true 
#积攒多少个 Event 才 flush 到 HDFS 一次 
a2.sinks.k1.hdfs.batchSize = 100 
#设置文件类型,可支持压缩 
a2.sinks.k1.hdfs.fileType = DataStream 
#多久生成一个新的文件 
a2.sinks.k1.hdfs.rollInterval = 600 
#设置每个文件的滚动大小大概是 128M 
a2.sinks.k1.hdfs.rollSize = 134217700 
#文件的滚动与 Event 数量无关 
a2.sinks.k1.hdfs.rollCount = 0 
#最小冗余数 
a2.sinks.k1.hdfs.minBlockReplicas = 1 

# Describe the channel 
a2.channels.c1.type = memory 
a2.channels.c1.capacity = 1000 
a2.channels.c1.transactionCapacity = 100 

# Bind the source and sink to the channel 
a2.sources.r1.channels = c1 
a2.sinks.k1.channel = c1

3.创建 flume-flume-dir.conf

配置上级 Flume 输出的 Source,输出是到本地目录的 Sink。

创建配置文件并打开

[root@hadoop102 group1]$ touch flume-flume-dir.conf 
[root@hadoop102 group1]$ vim flume-flume-dir.conf

添加如下内容

# Name the components on this agent 
a3.sources = r1 
a3.sinks = k1 
a3.channels = c2 

# Describe/configure the source 
a3.sources.r1.type = avro 
a3.sources.r1.bind = hadoop102 
a3.sources.r1.port = 4142 

# Describe the sink 
a3.sinks.k1.type = file_roll 
a3.sinks.k1.sink.directory = /opt/module/datas/flume3 

# Describe the channel 
a3.channels.c2.type = memory 
a3.channels.c2.capacity = 1000 
a3.channels.c2.transactionCapacity = 100 

# Bind the source and sink to the channel 
a3.sources.r1.channels = c2 
a3.sinks.k1.channel = c2

提示:输出的本地目录必须是已经存在的目录,如果该目录不存在,并不会创建新的目录。

4.执行配置文件

分别开启对应配置文件:flume-flume-dir,flume-flume-hdfs,flume-file-flume。

[root@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a3 --conf-file job/group1/flume-flume-dir.conf 
[root@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/group1/flume-flume-hdfs.conf 
[root@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/group1/flume-file-flume.conf

5.启动 Hadoop 和 Hive
[root@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh 
[root@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh 
[root@hadoop102 hive]$ bin/hive hive (default)>

6.检查 HDFS 上数据

在这里插入图片描述

7.检查/opt/module/datas/flume3 目录中数据
[root@hadoop102 flume3]$ ll 总用量 8 -rw-rw-r--. 1 demo demo 5942 5 月 22 00:09 1526918887550-3

五、单数据源多出口案例(Sink 组)

单 Source、Channel 多 Sink(负载均衡)如图所示。

单 Source、Channel 多 Sink

5.1 案例需求:

使用 Flume-1 监控文件变动,Flume-1 将变动内容传递给 Flume-2,Flume-2 负责存储到 HDFS。同时 Flume-1 将变动内容传递给 Flume-3,Flume-3 也负责存储到 HDFS。

5.2 需求分析:

在这里插入图片描述

5.3 实现步骤:
0.准备工作

在/opt/module/flume/job 目录下创建 group2 文件夹

[root@hadoop102 job]$ cd group2/

1.创建 flume-netcat-flume.conf

配 置 1 个 接 收 日 志 文 件 的 source 和 1 个 channel 、 两 个 sink , 分 别 输 送 给 flume-flume-console1 和 flume-flume-console2。

创建配置文件并打开

[root@hadoop102 group2]$ touch flume-netcat-flume.conf 
[root@hadoop102 group2]$ vim flume-netcat-flume.conf

添加如下内容

# Name the components on this agent 
a1.sources = r1 
a1.channels = c1 
a1.sinkgroups = g1 
a1.sinks = k1 k2 

# Describe/configure the source 
a1.sources.r1.type = netcat 
a1.sources.r1.bind = localhost 
a1.sources.r1.port = 44444 

a1.sinkgroups.g1.processor.type = load_balance 
a1.sinkgroups.g1.processor.backoff = true 
a1.sinkgroups.g1.processor.selector = round_robin a1.sinkgroups.g1.processor.selector.maxTimeOut=10000 

# Describe the sink 
a1.sinks.k1.type = avro 
a1.sinks.k1.hostname = hadoop102 
a1.sinks.k1.port = 4141 

a1.sinks.k2.type = avro 
a1.sinks.k2.hostname = hadoop102 
a1.sinks.k2.port = 4142 

# Describe the channel 
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.sinkgroups.g1.sinks = k1 k2 
a1.sinks.k1.channel = c1 
a1.sinks.k2.channel = c1

2.创建 flume-flume-console1.conf

配置上级 Flume 输出的 Source,输出是到本地控制台。

创建配置文件并打开

[root@hadoop102 group2]$ touch flume-flume-console1.conf 
[root@hadoop102 group2]$ vim flume-flume-console1.conf

添加如下内容

# Name the components on this agent 
a2.sources = r1 
a2.sinks = k1 
a2.channels = c1 

# Describe/configure the source 
a2.sources.r1.type = avro 
a2.sources.r1.bind = hadoop102 
a2.sources.r1.port = 4141 

# Describe the sink 
a2.sinks.k1.type = logger 

# Describe the channel 
a2.channels.c1.type = memory 
a2.channels.c1.capacity = 1000 
a2.channels.c1.transactionCapacity = 100 

# Bind the source and sink to the channel 
a2.sources.r1.channels = c1 
a2.sinks.k1.channel = c1

3.创建 flume-flume-console2.conf

配置上级 Flume 输出的 Source,输出是到本地控制台。

创建配置文件并打开

[root@hadoop102 group2]$ touch flume-flume-console2.conf 
[root@hadoop102 group2]$ vim flume-flume-console2.conf

添加如下内容

# Name the components on this agent 
a3.sources = r1 
a3.sinks = k1 
a3.channels = c2 

# Describe/configure the source 
a3.sources.r1.type = avro 
a3.sources.r1.bind = hadoop102 
a3.sources.r1.port = 4142 

# Describe the sink 
a3.sinks.k1.type = logger 

# Describe the channel 
a3.channels.c2.type = memory 
a3.channels.c2.capacity = 1000 
a3.channels.c2.transactionCapacity = 100 

# Bind the source and sink to the channel 
a3.sources.r1.channels = c2 
a3.sinks.k1.channel = c2

4.执行配置文件

分别开启对应配置文件:flume-flume-console2,flume-flume-console1,flume-netcat-flume。

[root@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a3 --conf-file job/group2/flume-flume-console2.conf -Dflume.root.logger=INFO,console 
[root@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/group2/flume-flume-console1.conf -Dflume.root.logger=INFO,console 
[root@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/group2/flume-netcat-flume.conf

5.使用 telnet 工具向本机的 44444 端口发送内容
$ telnet localhost 44444

6.查看 Flume2 及 Flume3 的控制台打印日志

六、多数据源汇总案例

多 Source 汇总数据到单 Flume 如图所示。

多 Flume 汇总数据到单 Flume

6.1 案例需求:

hadoop103 上的 Flume-1 监控文件/opt/module/group.log,
hadoop102 上的 Flume-2 监控某一个端口的数据流,
Flume-1 与 Flume-2 将数据发送给 hadoop104 上的 Flume-3,
Flume-3 将最终数据打印到控制台。

6.2 需求分析:

在这里插入图片描述

6.3 实现步骤:
0.准备工作

分发 Flume

[root@hadoop102 module]$ xsync flume

在hadoop102、hadoop103以及hadoop104的/opt/module/flume/job目录下创建一个group3 文件夹。

[root@hadoop102 job]$ mkdir group3 
[root@hadoop103 job]$ mkdir group3 
[root@hadoop104 job]$ mkdir group3

1.创建 flume1-logger-flume.conf

配置 Source 用于监控 hive.log 文件,配置 Sink 输出数据到下一级 Flume。

在 hadoop103 上创建配置文件并打开

[root@hadoop103 group3]$ touch flume1-logger-flume.conf 
[root@hadoop103 group3]$ vim flume1-logger-flume.conf

添加如下内容

# Name the components on this agent 
a1.sources = r1 
a1.sinks = k1 
a1.channels = c1 

# Describe/configure the source 
a1.sources.r1.type = exec 
a1.sources.r1.command = tail -F /opt/module/group.log 
a1.sources.r1.shell = /bin/bash -c 

# Describe the sink 
a1.sinks.k1.type = avro 
a1.sinks.k1.hostname = hadoop104 
a1.sinks.k1.port = 4141

# Describe the channel 
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

2.创建 flume2-netcat-flume.conf

配置 Source 监控端口 44444 数据流,配置 Sink 数据到下一级 Flume:

在 hadoop102 上创建配置文件并打开

[root@hadoop102 group3]$ touch flume2-netcat-flume.conf 
[root@hadoop102 group3]$ vim flume2-netcat-flume.conf

添加如下内容

# Name the components on this agent 
a2.sources = r1 
a2.sinks = k1 
a2.channels = c1 


![img](https://img-blog.csdnimg.cn/img_convert/d0bc3e5c372cf9f99e8353de478befa4.png)
![img](https://img-blog.csdnimg.cn/img_convert/c88ae50824dafe3a6d6d6100f46badbb.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

.r1.channels = c1 
a1.sinks.k1.channel = c1

2.创建 flume2-netcat-flume.conf

配置 Source 监控端口 44444 数据流,配置 Sink 数据到下一级 Flume:

在 hadoop102 上创建配置文件并打开

[root@hadoop102 group3]$ touch flume2-netcat-flume.conf 
[root@hadoop102 group3]$ vim flume2-netcat-flume.conf

添加如下内容

# Name the components on this agent 
a2.sources = r1 
a2.sinks = k1 
a2.channels = c1 


[外链图片转存中...(img-368gIRan-1715499677612)]
[外链图片转存中...(img-Rh4Jact2-1715499677612)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 15
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值