数据采集之flume

本文详细介绍了Flume的基本概念,包括其作为大数据实时采集工具的角色,以及使用Flume进行数据采集的步骤,如配置文件设定、启动流程等。通过实例展示了如何利用netcat进行端口通信测试,以确保Flume正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

上学的时候没有认真的学flume,现在来弥补。真是年少不知flume好,错把吴汉当成宝儿!


提示:以下是本篇文章正文内容,下面案例可供参考

一、flume是什么?

不专业的理解:英文翻译为:引水槽,放水沟。水就好比数据,那么我们就按照这个意思给它理解为引入数据的一个工具。
专业的解释:Apache Flume是一个分布式的、可靠的、可用的系统,用于有效地收集、 聚合和将大量日志数据从许多不同的源移动到一个集中的数据存储。

二、使用步骤

1.flume框架

数据源(日志)—>agent—>hdfs

agent:source、channel、sink。其中channel起到一个数据缓冲的作用。agent是一个jvm进程,输送数据。

2.安装netcat轻量级通讯工具(测试使用)

->sudo yum install -y nc
->判断端口是否被占用 sudo netstat -nlp | grep 9999
开服务端 nc -lk 9999
开客户端 nc localhost 9999
在服务端输入hello,在客户端可以看到接收到hello

3.配置flume工作文件

$mkdir job
$cd job
$touch net-flume-logger.conf

写入:

#example.conf: A single-node Flume configuration
#Name the components on this agent 每个组件的名字,a1是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 source和sink绑定channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

4.启动flume

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

5.监控文件

先修改配置文件

a1.sources = r1
a1.channels = c1
a1.sinks = k1
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /var/log/secure.log#监控的文件名称
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path=hdfs://hadoop:50070/flume/%Y%m%d/%H
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

### Apache Flume 数据采集框架使用指南 #### 1. 理解 Apache Flume 的基本概念 Apache Flume 是一个高可用的、可靠的、分布式的系统,用于有效地收集、聚合并移动大量日志数据[^1]。其设计核心在于能够处理大量的事件流,并支持多种源(Source)、通道(Channel)接收器(Sink),从而实现灵活的数据传输。 #### 2. 安装与配置环境准备 为了运行 Flume,需先安装 Java 运行时环境 (JRE) 或者开发工具包(JDK)[^2]。接着下载官方发布的最新版本二进制文件,并将其解压到指定目录下。设置好 `FLUME_HOME` PATH 变量以便于命令行操作。 #### 3. 配置文件编写实例 创建名为 `file-flume-kafka-1.conf` 的配置文件来定义 Source、Channel Sink 组件之间的关系: ```properties # Define the components of this agent. a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure source a1.sources.r1.type = exec a1.sources.r1.command = tail -F /var/log/syslog # Describe 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 ``` 此示例展示了如何通过执行命令读取 `/var/log/syslog` 文件中的新条目并将它们发送给 Logger 接收端进行记录。 #### 4. 启动 Agent 实现数据流动 利用如下 shell 命令启动带有特定配置文件名 (`file-flume-kafka-1.conf`) 的代理程序(a1): ```bash nohup flume-ng agent --conf-file /opt/program/flume/file-flume-kafka-1.conf --name a1 -Dflume.root.logger=INFO,LOGFILE > /opt/program/flume/log1.txt 2>&1 & ``` 上述指令会以后台模式启动 Flume 并将标准输出重定向至 log1.txt 中保存下来[^4]。 #### 5. 监控与维护 定期查看日志文件以确保正常运作;当遇到性能瓶颈时调整 Channel 参数如容量大小等优化效率;对于生产环境中长期稳定的服务建议部署监控告警机制及时发现异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值