Flume

Flume概述

https://flume.apache.org/releases/content/1.7.0/FlumeUserGuide.html

  1. Flume 定义

    Flume 是 Cloudera 提供的一个高可用,高可靠,分布式的海量日志采集、聚合和传输的系统。Flume 基于流式架构,灵活简单。

    Flume 最主要的作用就是,实时读取服务器本地磁盘的数据,将数据写到HDFS上

    Agent 是一个 JVM 进程,以事件的形式将数据从源头送至目的地。

    重点:Source、Channel、Sink

  2. Flume 案例

    bin/flume-ng agent --conf conf/ --conf-file job/nc-flume-logger.conf --name a1 -Dflume.root.logger=INFO,console
    
    bin/flume-ng agent -n a1 -c conf/ -f job/nc-flume-logger.conf -Dflume.root.logger=INFO,console
    
    # 从hive采集的时候要加hadoop依赖到flume的lib下
    bin/flume-ng agent -n a2 -c conf/ -f job/file-flume-hdfs.conf
    
    #监控本地文件夹 Spooling Directory Source 
    mkdir upload
    bin/flume-ng agent -n a2 -c conf/ -f job/dir-flume-hdfs.conf
    cp 3.txt upload/
    #说明:
    

1. 不要再监控目录中创建并持续修改文件

2. 上传完成的文件会以 .COMPLETED 结尾

3. 被监控文件夹每500毫秒扫描一次文件变动

Taildir Source 断点续传

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




3. Flume 事务

1. **Put事务:Source传到Channel**

   doPut:将批数据写入临时缓冲区putList

   doCommit:检查channel内存队列是否足够合并

   doRollback:channel内存队列空间不足,回滚数据

2. **Take事务**

   doTake:将数据取到临时缓冲区 takeList,并将数据发送到HDFS

   doCommit:如果数据全部发送成功,则清除临时缓冲区takeList

   doRollback:数据发送过程中如果出现异常,rollback 将临时缓冲区takeList中的数据归还给channel内存队列



4. Flume Agent 内部原理



5. Flume拓扑结构

 1. 直接串联:将多个flume顺序连接起来
 2. 复制和多路复用:用副本机制 replication
 3. 负载均衡和故障转移
 4. 聚合

 

6. Channel:memory、file、kafka



7. Sink:logger、hdfs、file



8. Channel选择器副本机制

```shell
bin/flume-ng agent -n a3 -c conf/ -f job/group/flume3.conf
bin/flume-ng agent -n a2 -c conf/ -f job/group/flume2.conf
bin/flume-ng agent -n a1 -c conf/ -f job/group/flume1.conf
  1. Sink组故障转移

    bin/flume-ng agent -n a3 -c conf/ -f job/group2/flume3.conf -Dflume.root.logger=INFO,console
    bin/flume-ng agent -n a2 -c conf/ -f job/group2/flume2.conf -Dflume.root.logger=INFO,console
    bin/flume-ng agent -n a1 -c conf/ -f job/group2/flume1.conf
    
  2. Sink组负载均衡

    cp -r group2/ group3
    #修改
    bin/flume-ng agent -n a3 -c conf/ -f job/group3/flume3.conf -Dflume.root.logger=INFO,console
    bin/flume-ng agent -n a2 -c conf/ -f job/group3/flume2.conf -Dflume.root.logger=INFO,console
    bin/flume-ng agent -n a1 -c conf/ -f job/group3/flume1.conf
    
  3. 聚合组 02taildir + 03netcat -> 04sink 实现多路聚合

    #scp -r 
    #hadoop04
    bin/flume-ng agent -n a4 -c conf/ -f job/group4/flume04.conf -Dflume.root.logger=INFO,console
    #hadooop03
    bin/flume-ng agent -n a3 -c conf/ -f job/group4/flume03.conf
    #hadooop02
    bin/flume-ng agent -n a2 -c conf/ -f job/group4/flume02.conf
    
  4. Flume自定义拦截器

    实现 org.apache.flume.interceptor.Interceptor 和 Interceptor.Builder

  5. Flume自定义Source

    public class MySource extends AbstractSource implements Configurable, PollableSource{}
    
    bin/flume-ng agent -n a1 -c conf/ -f job/mysource.conf -Dflume.root.logger=INFO,console
    
  6. Flume 实现自定义 Sink

    Sink不断地轮询Channel中的事件且批量地移除他们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个Flume Agent。

    Sink是完全事务性的。在从Channel批量删除数据之前,每个Sink用Channel启动一个事务。批量事件一旦写成功到存储系统或下一个Flume Agent,Sink 就利用 Channel 提交事务。事务一旦被提交,该Channel从自己的内部缓冲区删除事件。

    bin/flume-ng agent -n a1 -c conf/ -f job/mysink.conf -Dflume.root.logger=INFO,console
    
  7. Ganglia -> Flume 数据流监控

    sudo yum -y install httpd php
    #装装装
    #该配置
    #
    
  8. 重点总结

    taildir source用的比较多,一般用来监控日志数据

    memory ,file 用的比较多,对于像用户行为数据,可以丢一点的用memory,像财务数据这种的不可以丢的用file,确保数据不会丢

    Flume 参数调优

    Flume采集数据会丢失吗?

    ​ 根据Flume的架构原理,Flume是不可能丢数据,其内部有完善的事务机制,Source到Channel是事务性的,Channel和Sink是事务性的,因此这2个环节不会出现数据丢失,唯一可能出现问题的是memorychannel,agent宕机导致数据丢失或者Channel存储的数据已满,导致Source不再写入,未写入的数据丢失。

    ​ Flume 不会丢数据,但是可能造成数据的重复,例如数据已经成功由Sink发出,但是没有接收到响应,Sink会再次发送数据,此时可能会导致数据的重复。

posted @ 2024-01-30 12:53  停不下的时光  阅读(8)  评论(0编辑  收藏  举报