zoukankan      html  css  js  c++  java
  • Flume

    Flume概述

    1.Flume概述

    Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构,灵活简单。
     Flume适用于各种数据采集的场景,减少代码的书写
    

      如果不采用flume,针对不同的应用场景都需要编写客户端,代码量比较大,如果启用flume的话,我们可以不用编写客户端,flume可以接入各种场景吗,收集数据。
      Flume最主要的作用就是实时读取服务器本地磁盘的数据,将数据写入到hdfs.
      中文版:https://flume.liyifeng.org/#hdfs-sink
      英文版:http://flume.apache.org/FlumeUserGuide.html
    

    2.flume基础架构

    flume每个进程为agent,有三部分组成,source负责读取数据源中的数据,把数据包装成event,(source是把数据转换成flume中的标准类型的数据event),sink 接收event数据,并把数据写到hdfs中(原始数据)。channel是介于source和sink的缓冲区(source和sink大多时候传输速率不一致)类似看视频 网络读取的速度 5M 30kb 看视频的速度300kb/s   
     Agent:Agent是一个JVM进程,它以事件的形式将数据从源头送至目的。Agent主要有3个部分组成,Source、Channel、Sink。
     source:Source是负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。
      sink:Sink不断地轮询Channel中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个Flume Agent。
       Sink组件目的地包括hdfs、logger、avro、thrift、ipc、file、HBase、solr、自定义。
      channel:Channel是位于Source和Sink之间的缓冲区。因此,Channel允许Source和Sink运作在不同的速率上。Channel是线程安全的,可以同时处理几个Source的写入操作和几个Sink的读取操作。Flume自带两种Channel:Memory Channel和File Channel。
       Memory Channel是内存中的队列。Memory Channel在不需要关心数据丢失的情景下适用。如果需要关心数据丢失,那么Memory Channel就不应该使用,因为程序死亡、机器宕机或者重启都会导致数据丢失。
       File Channel将所有事件写到磁盘。因此在程序关闭或机器宕机的情况下不会丢失数据。
       event:传输单元,Flume数据传输的基本单元,以Event的形式将数据从源头送至目的地。Event由Header和Body两部分组成,Header用来存放该event的一些属性,为K-V结构,Body用来存放该条数据,形式为字节数组。
    

    3.flume案例

    3.1 安装部署

       (1)将apache-flume-1.9.0-bin.tar.gz上传到linux的/opt/software目录下
       (2)解压apache-flume-1.9.0-bin.tar.gz到/opt/module/目录下  tar -zxvf /opt/software/apache-flume-1.9.0-bin.tar.gz -C /opt/module/
       (3)修改apache-flume-1.9.0-bin的名称为flume         mv /opt/module/apache-flume-1.9.0-bin /opt/module/flume
       (4)将lib文件夹下的guava-11.0.2.jar删除以兼容Hadoop 3.1.3  rm /opt/module/flume/lib/guava-11.0.2.jar
    

    3.2 监控端口数据官方案例

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

        1)先安装netcat工具  sudo yum install -y nc
        2)判断44444端口是否被占用 
        3)创建Flume Agent配置文件 netcat-flume-logger.conf(配置文件名字无要求)
    
    # example.conf: A single-node Flume configuration
    
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source  //0.0.0.0  使用hostname的时候 仅限于本机 
    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
    a1.sources.r1.channels = c1
    a1.sinks.k1.channel = c1
    
       5) 修改一下 $flume/conf/log4j.properties  改变一下日志的输出路径
    

       6)开启flume
         $ bin/flume-ng agent -n $agent_name -c conf -f filename
         $ bin/flume-ng agent -n a1 -c conf -f job/netcat-flume-logger.conf
         第二种写法(没操作第5步)$ bin/flume-ng agent -n a1 -c conf -f job/netcat-flume-logger.conf -Dflume.root.logger=INFO,console
             或者全局      bin/flume-ng agent -c $FLUME_HOME/conf -f $FLUME_HOME/job/netcat-flume-logger.conf -n a1 -Dflume.root.logger=INFO,console
       7)使用netcat工具向本机的44444端口发送内容
          nc localhost 44444
          hello world 
         8)在Flume监听页面观察接收数据情况
    

    3.3 实时监控单个追加文件(日志信息)

       1)案例需求:实时监控Hive日志,并上传到HDFS中
    

       2)创建Flume Agent配置文件 cd $flume/job   vim exec-flume-hdfs.conf 
    
    # example.conf: A single-node Flume configuration
    
    # 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 -n 10 -F /opt/module/hive/logs/hive.log
    
    # Describe the sink [rollInterval-roundCount]控制生成新文件 [round-roundUnit]控制生成新文件夹
    a1.sinks.k1.type = hdfs
    a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M
    a1.sinks.k1.hdfs.filePrefix = events-%[localhost]
    a1.sinks.k1.hdfs.rollInterval = 60
    a1.sinks.k1.hdfs.roundSize = 134217728
    a1.sinks.k1.hdfs.roundCount = 0
    a1.sinks.k1.hdfs.fileType = DataStream
    a1.sinks.k1.hdfs.round = true
    a1.sinks.k1.hdfs.roundValue = 10
    a1.sinks.k1.hdfs.roundUnit = minute
    a1.sinks.k1.hdfs.useLocalTimeStamp = true
    # 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
    
    
       3) 开启hdfs 因为sink的输出位置在hdfs上
           start-dfs.sh
       4)开启flume 
         $ bin/flume-ng agent -n a1 -c conf -f job/exec-flume-hdfs.conf
          开启 hive
        5) 观察hdfs上的文件变化
    

    3.4 实时监控目录下多个新文件

       1)案例需求:使用Flume监听整个目录的文件,并上传至HDFS
    


    2)创建Flume Agent配置文件 cd $flume/job vim spool-flume-hdfs.conf

    # example.conf: A single-node Flume configuration
    
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source   事先定义好要监控的目录
    
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /opt/module/flume/spooling
    a1.sources.r1.ignorePattern = ^.*.tmp$
    # Describe the sink
    
    a1.sinks.k1.type = hdfs
    
    a1.sinks.k1.hdfs.path = /flume/spool/%y-%m-%d/%H%M
    a1.sinks.k1.hdfs.filePrefix = events-%[localhost]
    a1.sinks.k1.hdfs.rollInterval = 60
    a1.sinks.k1.hdfs.rollSize = 134217728
    a1.sinks.k1.hdfs.rollCount = 0
    a1.sinks.k1.hdfs.fileType = DataStream
    a1.sinks.k1.hdfs.round = true
    a1.sinks.k1.hdfs.roundValue = 10
    a1.sinks.k1.hdfs.roundUnit = minute
    a1.sinks.k1.hdfs.useLocalTimeStamp = true
    # 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
    
    
         3)开启flume (开启之前  事先定义好要监控的目录)
           bin/flume-ng agent -n a1 -c conf -f job/spool-flume-hdfs.conf
          4)往监控目录中添加文件
             不能向同一文件中追加数据
          5)观察hdfs上内容的变化
    

    3.5 实时监控目录下的多个追加文件

        Exec source适用于监控一个实时追加的文件,不能实现断点续传,数据容易丢失;Spooldir Source适合用于同步新文件,但不适合对实时追加日志的文件进行监听并同步;而Taildir Source适合用于监听多个实时追加的文件,并且能够实现断点续传。
       1)案例需求:使用Flume监听整个目录的实时追加文件,并上传至HDFS
    

        2)创建flume agent 配置文件 cd $flume/job   vim taildir-flume-hdfs.conf
    
    # example.conf: A single-node Flume configuration
    
    # 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.positionFile = /opt/module/flume/taildir_position.json
    a1.sources.r1.filegroups = f1 f2
    a1.sources.r1.filegroups.f1 = /opt/module/flume/taildir/.*file.*
    a1.sources.r1.filegroups.f2 = /opt/module/flume/taildir/.*log.*
    # Describe the sink
    a1.sinks.k1.type = hdfs
    a1.sinks.k1.hdfs.path = /flume/taildir/%y-%m-%d/%H%M/
    a1.sinks.k1.hdfs.filePrefix = events-%[localhost]
    a1.sinks.k1.hdfs.rollInterval = 60
    a1.sinks.k1.hdfs.rollSize = 134217728
    a1.sinks.k1.hdfs.rollCount = 0
    a1.sinks.k1.hdfs.fileType = DataStream
    a1.sinks.k1.hdfs.round = true
    a1.sinks.k1.hdfs.roundValue = 10
    a1.sinks.k1.hdfs.roundUnit = minute
    a1.sinks.k1.hdfs.useLocalTimeStamp = true
    # 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
    
    
          3)开启flume
            bin/flume-ng agent -n a1 -c conf -f job/taildir-flume-hdfs.conf
           4) 往监控文件夹中添加文件
              接着往文件中追加数据
           5)查看hdfs中的数据
          Taildir说明:
    Taildir Source维护了一个json格式的position File,其会定期的往position File中更新每个文件读取到的最新的位置,因此能够实现断点续传。Position File的格式如下:
      {"inode":2496272,"pos":12,"file":"/opt/module/flume/files/file1.txt"}
      {"inode":2496275,"pos":12,"file":"/opt/module/flume/files/file2.txt"}
      注:Linux中储存文件元数据的区域就叫做inode,每个inode都有一个号码,操作系统用inode号码来识别不同的文件,Unix/Linux系统内部不使用文件名,而使用inode号码来识别文件。
  • 相关阅读:
    在HTML中使用JavaScript
    网站发布流程
    React组件
    React渲染和事件处理
    Java IO(三)
    Java IO(二)
    Java IO(一)
    Java常用类库
    Java集合框架(四)
    Java集合框架(三)
  • 原文地址:https://www.cnblogs.com/xiao-bu/p/14337361.html
Copyright © 2011-2022 走看看