发布时间:2026/8/31 1:17:36
Flume 多维数据源采集实战:数据库、日志与埋点的统一接入之道 Flume 多维数据源采集实战数据库、日志与埋点的统一接入之道1. Flume 架构概述与多维数据源接入意义Apache Flume 是一个高可用、高可靠、分布式的海量日志采集、聚合和传输的系统专为日志收集中设计。在企业级数据中台建设过程中通常需要从多种异构数据源采集数据如数据库变更日志、服务器系统日志、应用程序埋点数据等。通过 Flume 的统一接入能力可以实现不同类型数据的标准化采集简化数据管道架构提高数据处理效率。Flume 的核心概念包括Agent一个独立的 Flume 进程包含 Source、Channel 和 Sink 三大组件Source数据收集组件从数据源采集数据Channel数据传输组件连接 Source 和 SinkSink数据发送组件将数据写入目的地在实际应用中通过合理配置这三个组件可以实现多种数据源的统一接入与处理。2. 数据库 Binlog 采集配置与实战MySQL 数据库的 Binlog 记录了所有更改数据的 SQL 语句是数据变更审计和实时数据同步的重要来源。Flume 可以通过 Debezium Source 插件或自定义 MySQL Binlog Source 实现 Binlog 采集。配置步骤启用 MySQL Binlog-- 在 MySQL 配置文件中添加以下内容 [mysqld] log-binmysql-bin binlog-formatROW server-id1创建 Flume 配置文件(mysql-binlog-flume.conf)# 定义名为 mysql-binlog 的源 agent.sources mysql-binlog # 配置 mysql-binlog 源 agent.sources.mysql-binlog.type org.apache.flume.source.exec.ExecSource agent.sources.mysql-binlog.command mysqlbinlog --read-from-remote-server --host127.0.0.1 --port3306 --userflume --passwordpassword --raw --stop-never mysql-bin.000001 agent.sources.mysql-binlog.shell /bin/bash -c agent.sources.mysql-binlog.batchSize 1000 agent.sources.mysql-binlog.channels memory-channel # 定义内存通道 agent.channels memory-channel agent.channels.memory-channel.type memory agent.channels.memory-channel.capacity 10000 # 定义 Kafka Sink agent.sinks kafka-sink agent.sinks.kafka-sink.type org.apache.flume.sink.kafka.KafkaSink agent.sinks.kafka-sink.brokerList localhost:9092 agent.sinks.kafka-sink.topic binlog-topic agent.sinks.kafka-sink.channel memory-channel agent.sinks.kafka-sink.requiredAcks 1 agent.sinks.kafka-sink.batchSize 1000启动 Flume Agentflume-ng agent --conf ./conf --conf-file ./mysql-binlog-flume.conf --name agent -Dflume.root.loggerINFO,console关键点说明使用mysqlbinlog命令直接读取 MySQL 的 Binlog通过内存通道作为中间缓冲平衡数据采集速率和写入速率最终将数据写入 Kafka实现高吞吐和持久化存储3. 系统日志与应用埋点采集实现3.1 系统日志采集系统日志如 Nginx 访问日志、系统日志等通常采用文件 Source 采集配置示例(syslog-flume.conf)# 定义 source agent.sources syslog-source # 配置 syslog source agent.sources.syslog-source.type exec agent.sources.syslog-source.command tail -F /var/log/nginx/access.log agent.sources.syslog-source.channels memory-channel # 定义通道 agent.channels memory-channel agent.channels.memory-channel.type memory agent.channels.memory-channel.capacity 10000 # 定义 HDFS Sink agent.sinks hdfs-sink agent.sinks.hdfs-sink.type hdfs agent.sinks.hdfs-sink.hdfs.path hdfs://namenode:8020/logs/%Y%m%d/%H agent.sinks.hdfs-sink.hdfs.fileType DataStream agent.sinks.hdfs-sink.hdfs.writeFormat Text agent.sinks.hdfs-sink.hdfs.rollInterval 3600 agent.sinks.hdfs-sink.hdfs.rollSize 134217728 agent.sinks.hdfs-sink.hdfs.rollCount 0 agent.sinks.hdfs-sink.channel memory-channel3.2 应用埋点采集应用埋点数据如 JSON 格式的用户行为数据可以通过 HTTP Source 接收配置示例(app-metrics-flume.conf)# 定义 source agent.sources http-source # 配置 HTTP source agent.sources.http-source.type org.apache.flume.http.HTTPSource agent.sources.http-source.port 5140 agent.sources.http-source.handler org.apache.flume.http.JSONHandler agent.sources.http-source.channels memory-channel agent.sources.http-source.processor.type default agent.sources.http-source.processor.maxThreads 8 # 定义通道 agent.channels memory-channel agent.channels.memory-channel.type memory agent.channels.memory-channel.capacity 10000 # 定义 Elasticsearch Sink agent.sinks elasticsearch-sink agent.sinks.elasticsearch-sink.type org.apache.flume.sink.elasticsearch.ElasticSearchSink agent.sinks.elasticsearch-sink.hostNames elasticsearch:9200 agent.sinks.elasticsearch-sink.indexName app-metrics agent.sinks.elasticsearch-sink.indexType logs agent.sinks.elasticsearch-sink.channel memory-channel agent.sinks.elasticsearch-sink.serializer org.apache.flume.sink.elasticsearch.ElasticSearchLogStashEventSerializer4. 多源数据汇聚与统一处理当需要将多种数据源汇聚到同一目的地时可以使用 Flume 的 Interceptor 机制进行数据预处理和统一格式化配置多源汇聚(multi-source-flume.conf)# 定义多个 source agent.sources binlog-source syslog-source http-source # 配置 binlog source agent.sources.binlog-source.type exec agent.sources.binlog-source.command mysqlbinlog --read-from-remote-server --host127.0.0.1 --port3306 --userflume --passwordpassword --raw --stop-never mysql-bin.000001 agent.sources.binlog-source.channels memory-channel # 配置 syslog source agent.sources.syslog-source.type exec agent.sources.syslog-source.command tail -F /var/log/nginx/access.log agent.sources.syslog-source.channels memory-channel # 配置 HTTP source agent.sources.http-source.type org.apache.flume.http.HTTPSource agent.sources.http-source.port 5140 agent.sources.http-source.handler org.apache.flume.http.JSONHandler agent.sources.http-source.channels memory-channel # 定义通道 agent.channels memory-channel agent.channels.memory-channel.type memory agent.channels.memory-channel.capacity 10000 # 定义 Kafka Sink agent.sinks kafka-sink agent.sinks.kafka-sink.type org.apache.flume.sink.kafka.KafkaSink agent.sinks.kafka-sink.brokerList localhost:9092 agent.sinks.kafka-sink.topic unified-topic agent.sinks.kafka-sink.channel memory-channel添加 Interceptor 进行数据格式化# 为每个 source 添加 interceptor agent.sources.binlog-source.interceptors i1 agent.sources.binlog-source.interceptors.i1.type org.apache.flume.interceptor.TimestampInterceptor$Builder agent.sources.syslog-source.interceptors i1 agent.sources.syslog-source.interceptors.i1.type org.apache.flume.interceptor.TimestampInterceptor$Builder agent.sources.http-source.interceptors i1 agent.sources.http-source.interceptors.i1.type org.apache.flume.interceptor.TimestampInterceptor$Builder使用 Avro 实现多 Agent 级联# 在第一级 Agent 中 agent.sources avro-source agent.sources.avro-source.type avro agent.sources.avro-source.bind 0.0.0.0 agent.sources.avro-source.port 41414 agent.sources.avro-source.channels memory-channel # 在第二级 Agent 中 agent.sources exec-source avro-source agent.sources.exec-source.type exec agent.sources.exec-source.command tail -F /var/log/application.log agent.sources.exec-source.channels memory-channel agent.sources.avro-source.type avro agent.sources.avro-source.bind 0.0.0.0 agent.sources.avro-source.port 41414 agent.sources.avro-source.channels memory-channel5. 完整示例与关键注意事项完整的多源采集配置示例# 定义 sources agent.sources binlog-source syslog-source http-source # 配置 binlog source agent.sources.binlog-source.type exec agent.sources.binlog-source.command mysqlbinlog --read-from-remote-server --host127.0.0.1 --port3306 --userflume --passwordpassword --raw --stop-never mysql-bin.000001 agent.sources.binlog-source.interceptors i1 agent.sources.binlog-source.interceptors.i1.type org.apache.flume.interceptor.TimestampInterceptor$Builder agent.sources.binlog-source.channels memory-channel # 配置 syslog source agent.sources.syslog-source.type exec agent.sources.syslog-source.command tail -F /var/log/nginx/access.log agent.sources.syslog-source.interceptors i1 agent.sources.syslog-source.interceptors.i1.type org.apache.flume.interceptor.TimestampInterceptor$Builder agent.sources.syslog-source.channels memory-channel # 配置 HTTP source agent.sources.http-source.type org.apache.flume.http.HTTPSource agent.sources.http-source.port 5140 agent.sources.http-source.handler org.apache.flume.http.JSONHandler agent.sources.http-source.interceptors i1 agent.sources.http-source.interceptors.i1.type org.apache.flume.interceptor.TimestampInterceptor$Builder agent.sources.http-source.channels memory-channel # 定义通道 agent.channels memory-channel agent.channels.memory-channel.type memory agent.channels.memory-channel.capacity 10000 # 定义 Kafka Sink agent.sinks kafka-sink agent.sinks.kafka-sink.type org.apache.flume.sink.kafka.KafkaSink agent.sinks.kafka-sink.brokerList localhost:9092 agent.sinks.kafka-sink.topic unified-topic agent.sinks.kafka-sink.channel memory-channel注意事项内存使用内存通道容量设置需考虑可用内存避免溢出背压处理当 Sink 无法及时处理时需要合理配置 Channel 和 Source 的参数数据格式统一使用 Interceptor 统一不同数据源的时间戳和格式高可用性通过配置多个 Agent 和负载均衡实现高可用监控告警配置 JMX 监控 Agent 运行状态及时发现问题数据清洗可在 Source 和 Sink 之间添加自定义 Channel Processor 进行数据清洗批量处理合理设置批量处理参数平衡实时性和吞吐量最小可直接运行示例# 简单的日志采集到控制台 a1.sources r1 a1.sinks k1 a1.channels c1 # Source 配置 a1.sources.r1.type exec a1.sources.r1.command tail -F /var/log/syslog # Sink 配置 a1.sinks.k1.type logger # Channel 配置 a1.channels.c1.type memory a1.channels.c1.capacity 1000 # 绑定 Source 和 Channel 到 Sink a1.sources.r1.channels c1 a1.sinks.k1.channel c1执行命令flume-ng agent --conf ./conf --conf-file ./simple-flume.conf --name a1 -Dflume.root.loggerINFO,consoleMySQL数据库BinlogFlume Binlog Source系统日志文件Flume Exec Source应用埋点APIFlume HTTP SourceMemory ChannelKafka SinkKafka集群实时数据处理引擎数据仓库数据湖Flume 多维数据源采集是企业数据平台建设的重要环节通过合理配置可以实现高效、可靠的数据采集。在实际应用中需要根据业务需求调整配置参数并结合监控和告警机制确保数据管道的稳定运行。

相关新闻

2026/8/31 1:17:36

js 文本 控件添加鼠标离开事件

例如:给控件鼠标离开后,如果控件值不存在action字符串,则自动添加action字符串document.getElementById("控件ID").bind("blur",function(){var value document.getElementById("控件ID").val(); if(value!nul…

2026/8/31 1:32:37

MATLAB优化工具箱实战指南:线性规划、非线性拟合与全局优化

想用 MATLAB 解决优化问题,最快的方法是直接使用官方优化算法工具箱(Optimization Toolbox)。它解决的不是某一个具体问题,而是一整类问题:线性规划、带线性或非线性约束的优化、无约束优化、二次规划、最小二乘数据拟…

2026/8/31 1:32:37

小红书2020校招算法笔试题全解析:考点、题型与备考策略

小红书2020校招算法笔试题卷三,我拿到手之后整体刷过两遍,也拿它给身边准备秋招的朋友做过模拟。这套卷子在当年的校招题库里属于风格比较典型的那一类:算法题占比高、机器学习基础考得细、场景题非常贴近内容平台的业务逻辑,和单…

2026/8/31 1:32:37

Codex + GitHub Pages:免费搭建AI驱动网站自动发布流程

先解决两个最扎心的问题:用 Codex 开发出来的网站,为什么别人访问不到?为什么每次改完需求,还得手动上传一次文件?答案很简单——本地开发服务器只在你电脑上跑,别人当然进不来;而手动上传的本质…

2026/8/31 1:32:37

DeepSeek API 涨价 1000% 后,开发者如何做好 token 成本治理?

DeepSeek API 最高 1000% 的价格调整已经正式落地。如果你最近在用 DeepSeek 的接口跑自动化脚本、接 Codex / Claude Code / VSCode 做编程助手,或者在企业微信、内部系统里集成了它,大概率已经感受到了账单变化。我的判断很直接:这次调价不…

2026/8/31 1:27:36

Transformer在雷达回波外推中的应用:从数据预处理到训练调参全解析

简介:本资源是一套基于Transformer架构的雷达回波外推模型实现,面向计算机、人工智能、气象信息处理及自动化等专业的本科生与研究生,聚焦于0–2小时临近降水预测这一关键气象任务。项目完整复现了雷达回波序列建模与时空外推的核心流程&…

2026/8/31 1:05:20

vSound小提琴数字处理器实操指南:从接线到演出的完整配置

电小提琴或者原声小提琴插电演出,第一个绕不开的坎就是声音难听。原声琴的共鸣和空气感一旦进了拾音器,出来的往往是一坨干瘪、发尖、带着奇怪塑料味的信号。我当初第一次把琴接上乐队调音台,直接被主唱吐槽"你这声音像在锯钢丝"。…

2026/8/30 0:03:35

传感器接口IC如何攻克生物化学传感的微弱信号难题?

1. 从电极到比特流:为什么生物化学传感必须依赖专用接口IC 做生物化学传感的人都有过类似的经历:明明传感器本身性能很好,信号输出却一塌糊涂——噪声大、漂移明显、重复性差,怎么调都达不到预期。很多时候问题并不在传感器&#…

2026/8/30 0:03:35

STM32F411CEU6多通道ADC采集:扫描模式+DMA实现详解

1. 多通道 ADC 的用武之地把“Multichannel ADC”和“STM32F411CEU6”这两个关键字放在一起,其实就是嵌入式开发里最常遇到的一类需求:用一块不算贵的 MCU,同时采集多路模拟信号。STM32F411CEU6 是 48 引脚的 Cortex-M4F 主控,主频…

2026/8/31 0:07:32

STM32C5设备支持包(IAR DFP)安装指南与常见坑

上一阵子在IAR里折腾一块基于STM32C5系列的新板子,工程从STM32CubeMX导出来之后怎么都编译不过。报错信息很干脆:找不到设备描述文件。跟着错误路径去查,发现指向的是一个让我愣了一下的名字:STMicroelectronics.stm32c5xx.2.1.0.…

2026/8/31 0:07:32

STM32N657 SWO引脚矛盾:CubeMX显示PB3,数据手册为PB5

拿到STM32N657这颗料的第一天,我就撞上了一个让人原地懵圈的引脚矛盾:CubeMX里清清楚楚显示SWO在PB3,翻开数据手册的引脚说明表,却赫然写着PB5。对于一个靠SWO输出调试日志吃饭的人而言,这种"工具和手册打架"…

2026/8/28 16:16:48

实测才敢推 AI论文网站 2026最新测评与推荐

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。一、综…

2026/8/28 16:16:50

2026必备!AI论文网站测评:最新推荐与深度对比

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

2026/8/28 11:06:45

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…