Flume入门案例(三个简单的小案例)

本文详细介绍Flume在三种场景下的应用:监听端口并打印数据、实时监控单个文件追加、监控多目录下的多个文件追加。涵盖FlumeAgent配置、Netcat工具使用、HDFS数据上传等关键步骤。

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

1)安装netcat工具
[@hadoop102 software]$ sudo yum install -y nc

(2)判断44444端口是否被占用
[@hadoop102 flume-telnet]$ sudo netstat -tunlp | grep 444443)创建Flume Agent配置文件netcat-flume-logger.conf
在flume目录下创建job文件夹并进入job文件夹。
[@hadoop102 flume]$ mkdir job
[@hadoop102 flume]$ cd job/
在job文件夹下创建Flume Agent配置文件netcat-flume-logger.conf。
[@hadoop102 job]$ vim netcat-flume-logger.conf
在netcat-flume-logger.conf文件中添加如下内容。

# Name the components on this agent
a1.sources = r1    a1就是agent的名字
a1.sinks = k1
a1.channels = c1

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

# Describe the sink   这是sink
a1.sinks.k1.type = logger

# Use a channel which buffers events in memory  这是channels
a1.channels.c1.type = memory  内存channels
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

(4)先开启flume监听端口
[@hadoop102 flume]$ bin/flume-ng agent -c conf/ -n a1 -f job/netcat-flume-logger.conf -Dflume.root.logger=INFO,console

(5)使用netcat工具向本机的44444端口发送内容
[@hadoop102 ~]$ nc localhost 444446)在Flume监听页面观察接收数据情况

案例二:实时监控单个文件的追加

*Source 用到的是exec,channel是memory,sink是hdfs。
前期准备:因为/opt/module/hive/logs/hive.log本身日志太长,选择自己创建在/opt/module/datas/hive.log 自己通过echo 命令追加到至文件中。

在job中创建 vim file-flume-hdfs2.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/datas/hive.log

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path =/flume/%Y%m%d/%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = logs-
#是否对时间戳取整
a1.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a1.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a1.sinks.k1.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a1.sinks.k1.hdfs.rollInterval = 10
#设置每个文件的滚动大小
a1.sinks.k1.hdfs.rollSize = 134217700

#文件的滚动与Event数量无关
a1.sinks.k1.hdfs.rollCount = 0

# 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

运行Flume
[@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-file-hdfs.conf

echo “bigdataJ”>> /opt/module/datas/hive.log
在hdfs上查看文件

案例三:实时监控多目录下的多个追加文件

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

1)在job中创建配置文件taildir-flume-hdfs.conf
添加如下内容:

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

# Describe/configure the source
a1.sources.r1.type = TAILDIR
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /opt/module/file1/file1.txt
a1.sources.r1.filegroups.f2 = /opt/module/file2/file.*.txt
a1.sources.r1.positionFile = /opt/module/taildir/tail_dir.json
# /opt/module/taildir/tail_dir.json这是记录文件回滚的位置信息

# 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



(2)启动监控文件夹命令
[@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/files-flume-hdfs.conf3)向files文件夹中追加内容

在/opt/module/flume目录下创建files文件夹
mkdir file1
mkdir file2
追加文件
echo hello >> file1/file1.txt
echo world >> file2/fileqwer.txt

(4)数据
(5)可以在/opt/module/taildir/tail_dir.json观察信息。
### Flume入门教程 Apache Flume 是一种分布式、可靠且高可用的日志收集系统,主要用于高效地采集、聚合并传输大量日志数据至集中存储系统(如 HDFS)。以下是关于 Flume 的基本概念及其组成部分的概述: #### Flume 基本定义与组成 Flume 主要由三个核心组件构成:Source、Channel 和 Sink[^1]。 - **Source** 负责接收外部数据流并将这些数据写入 Channel 中。它可以支持多种输入方式,例如 syslog 数据、文件尾部跟踪等。 - **Channel** 可视为临时缓冲区,在 Source 接收的数据被传递给 Sink 之前暂存其中。常见的 Channel 类型有 Memory Channel 和 File Channel。 - **Sink** 将来自 Channel 的数据发送到目标位置,比如 HDFS 或者 Kafka。 #### Flume Agent 内部工作原理 每个 Flume 实例被称为一个 Agent,它运行在一个独立 JVM 上,并通过配置文件指定其行为模式。Agent 包含多个 Source、Channel 和 Sink 组件实例化后的对象组合而成。当启动某个 Agent 后,各部分之间会依据预设规则协同运作完成整个流水线操作过程。 --- ### 大数据项目管理中的Flume应用 在实际生产环境中部署实施基于 Apache Flume 构建的大规模日志处理平台时,需考虑以下几个方面来优化性能表现及稳定性保障措施: #### 需求分析阶段的重要性 对于任何成功的软件工程而言,“知己知彼百战不殆”,因此做好前期调研至关重要。这一步骤涉及明确业务场景下的具体要求是什么?期望达到怎样的效果等等问题解答清楚之后再进入下一步规划环节显得尤为重要[^2]。 #### 架构设计方案说明 根据不同的应用场景可以选择相应的解决方案架构图来进行搭建。例如采用 Master-Slave 结构形式或者 Ring Buffer 方式都可以有效提升系统的吞吐量水平;另外还需要注意设置合理的 checkpoint 时间间隔参数值以便于发生异常情况时候能够快速恢复状态信息继续正常运转下去而不至于丢失太多未保存下来的重要资料记录等内容细节都需要仔细斟酌决定才行哦! --- ### 案例分析——实时读取本地文件到HDFS案例展示了如何利用 Flume 把位于服务器上的文本文件内容不间断地传送到远程 Hadoop 文件系统 (HDFS) 当中去储存起来供后续进一步加工计算使用的目的达成方法步骤如下所示: 1. 创建一个新的 conf 目录用于放置自定义配置文件; 2. 编辑 flume-conf.properties 文件添加相应条目描述 source type 设置为 spooling directory monitor 并指明待观察的目标路径地址以及 target hdfs location URI 地址等相关属性字段填写完整无误即可成功建立连接关系链路从而实现预期功能目的. ```properties agent.sources = dir-source agent.sinks = hdfs-sink agent.channels = memory-channel # Define the source agent.sources.dir-source.type = spooldir agent.sources.dir-source.spoolDir = /path/to/spooldir agent.sources.dir-source.fileHeader = true # Define the sink agent.sinks.hdfs-sink.type = hdfs agent.sinks.hdfs-sink.hdfs.path = hdfs://namenode:8020/user/flume/ agent.sinks.hdfs-sink.hdfs.fileType = DataStream agent.sinks.hdfs-sink.hdfs.writeFormat = Text # Use a channel which buffers events in memory agent.channels.memory-channel.type = memory agent.channels.memory-channel.capacity = 1000 agent.channels.memory-channel.transactionCapacity = 100 # Bind the source and sink to the channel agent.sources.dir-source.channels = memory-channel agent.sinks.hdfs-sink.channel = memory-channel ``` 上述脚本片段演示了一个典型的 Flume 应用程序配置示例,其中包括 Spool Directory Source 和 HDFS Sink 的联合运用情景下所必需的各项关键参数设定项逐一列举出来供大家参考借鉴学习之用。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值