大数据之flume(一)

1、概述

flume是一个分布式、高可用、高可靠的海量日志采集、聚合、传输系统,支持在日志系统中定制各种数据发送方从而收集数据,并提供数据简单处理能力并传到各种数据接收方。flume设计原理是基于数据流的,能够将不同数据源的海量日志进行高效收集、聚合、移动、存储,能做到近似实时。

2、架构

在这里插入图片描述

2.1、Event

事件是flume内部数据传输的基本单元,将传输的文件进行封装。由存储数据的字节数据和头部信息组成。数据以事件的形式进行传递。

2.2、Agent

Agent是一个jvm进程,通过source、channel、sink三大组件将事件流从外部数据源发送到目的存储。

2.3、source

从数据发生器接收数据并将数据以Event形式传递给一个或多个通道。

2.4、channel

一种短暂的存储容器,连接source和sink。channel将event的数据缓存起来,当sink成功将event发送后,event从channel中移除。channe是一个完整的事务,从而保证数据收发的一致性,模型类似于队列。

  • memory channel:事件存储到内存中,队列容量为可存储的最大事件数量,适用于高吞吐场景,当agent出现错误时,数据可能丢失。
  • file channel:基于文件系统的持久化存储
2.5、sink

获取channel保存的数据并进行处理。sink从channel中移除事件并将其发送到下一个agent(即下一跳)或最终存储,如HDFS。

2.6、可靠性

事件在每个agent的channel中短暂存储,,之后事件被发送到下一个agent或者最终存储中。时间只有在存储在下一个agent或最终存储后才会从当前channel中删除。

flume使用事务的办法来保证事件的可靠传递。source和sink分别被封装到事务中,事务一般有channel提供,从而保证数据传输的可靠。在多个agent(多跳)中,上一跳的sink和下一跳的source都运行事务来保证数据的可靠性。

3、安装、使用(1.9.0版本)

下载地址:https://flume.apache.org/download.html

文档地址:https://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html

解压后删除lib目录下的guava-11.0.2.jar,防止和hadoop不兼容。

3.1、监听数据

步骤:

  1. 通过netcat向55555端口发送数据
  2. flume监控55555端口并通过source读取数据
  3. flume将读取到的数据通过sink写到控制台

操作:

  1. 安装netcat

    sudo  yum install -y nc
    
  2. 查看端口占用

     netstat -nlp|grep 55555
    
  3. 新建job文件夹,编写flume-netcat-logger.conf文件

    [root@hadoop101 apache-flume-1.11.0-bin]# mkdir job
    [root@hadoop101 apache-flume-1.11.0-bin]# mkdir job
     vim flume-netcat-logger.conf
    
    # Name the components on this 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 = 55555
    
    # 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#channel总容量
    a1.channels.c1.transactionCapacity = 100#单次事务的容量
    
    # Bind the source and sink to the channel
    a1.sources.r1.channels = c1#source可以绑定多个channel
    a1.sinks.k1.channel = c1#一个sink只能绑定一个channel
    

    执行命令

    [root@hadoop101 apache-flume-1.9.0-bin] bin/flume-ng agent -n a1 -c conf/ -f job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
    
    

在控制台查看消息日志
在这里插入图片描述

3.2、监控单个文件

步骤:

  1. 创建flume配置文件
  2. 执行配置文件并开启监控
  3. 开启Hive生成日志
  4. 查看HDFS上数据

操作:

  1. 创建conf文件flume-file-hdfs.conf

     a2.sources = r2
     a2.sinks = k2
     a2.channels = c2
    
     # Describe/configure the source
     a2.sources.r2.type = exec
     a2.sources.r2.command = tail -F /tmp/root/hive.log
     a2.sources.r2.shell = /bin/bash -c
    
     # Describe the sink
     a2.sinks.k2.type = hdfs
     a2.sinks.k2.hdfs.path = hdfs://hadoop100:8020/flume/%Y%m%d/%H
     #上传文件的前缀
     a2.sinks.k2.hdfs.filePrefix = logs-
     #是否对时间戳取整
     a2.sinks.k2.hdfs.round = true
     #多少时间单位创建一个新的文件夹
     a2.sinks.k2.hdfs.roundValue = 1
     #重新定义时间单位
     a2.sinks.k2.hdfs.roundUnit = hour
     #是否使用本地时间戳
     a2.sinks.k2.hdfs.useLocalTimeStamp = true
     #积攒多少个Event才flush到HDFS一次
     a2.sinks.k2.hdfs.batchSize = 100
     #设置文件类型,可支持压缩
     a2.sinks.k2.hdfs.fileType = DataStream
     #多久生成一个新的文件
     a2.sinks.k2.hdfs.rollInterval = 60
     #设置每个文件的滚动大小
     a2.sinks.k2.hdfs.rollSize = 134217700
     #文件的滚动与Event数量无关
     a2.sinks.k2.hdfs.rollCount = 0
    
     # Use a channel which buffers events in memory
     a2.channels.c2.type = memory
     a2.channels.c2.capacity = 1000
     a2.channels.c2.transactionCapacity = 100
    
     # Bind the source and sink to the channel
     a2.sources.r2.channels = c2
     a2.sinks.k2.channel = c2
    
    

    注意: a2.sources.r2.command中hive的日志路径和 a2.sinks.k2.hdfs.path要根据自己的环境来写,查看路径是否正确使用命令。

    hadoop fs -ls hdfs://hadoop100:8020/
    

    如果能看到HDFS的目录说明没问题,如下图。
    在这里插入图片描述

  2. 运行命令

    bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
    
  3. 启动Hive,可使用命令监控Hive日志 tail -F /tmp/root/hive.log

  4. 查看HDFS是否有flume目录
    在这里插入图片描述

3.3、监控目录下的多个新文件

步骤:

  1. 创建符合条件的flume配置文件
  2. 执行配置文件开启监控
  3. 向监控目录中添加文件
  4. 查看HDFS检查文件是否上传成功
  5. 查看监控目录中的文件是否已被标记

操作:

  1. 创建conf文件flume-dir-hdfs.conf

     a2.sources = r2
     a2.sinks = k2
     a2.channels = c2
    
     # Describe/configure the source
     a2.sources.r2.type = spooldir
     a2.sources.r2.spoolDir=/export/soft/apache-flume-1.9.0-bin/upload
     a2.sources.r2.fileSuffix=.COMPLETED#上传完成后修改文件后缀名进行标记
     a2.sources.r2.fileHeader=true
     a2.sources.r2.ignorePattern=([^ ]*\.tmp)#忽略该类型文件
    
     # Describe the sink
     a2.sinks.k2.type = hdfs
     a2.sinks.k2.hdfs.path = hdfs://hadoop100:8020/flume/upload/%Y%m%d/%H
     #上传文件的前缀
     a2.sinks.k2.hdfs.filePrefix = logs-
     #是否对时间戳取整
     a2.sinks.k2.hdfs.round = true
     #多少时间单位创建一个新的文件夹
     a2.sinks.k2.hdfs.roundValue = 1
     #重新定义时间单位
     a2.sinks.k2.hdfs.roundUnit = hour
     #是否使用本地时间戳
     a2.sinks.k2.hdfs.useLocalTimeStamp = true
     #积攒多少个Event才flush到HDFS一次
    a2.sinks.k2.hdfs.batchSize = 100
     #设置文件类型,可支持压缩
     a2.sinks.k2.hdfs.fileType = DataStream
     #多久生成一个新的文件
     a2.sinks.k2.hdfs.rollInterval = 60
     #设置每个文件的滚动大小
     a2.sinks.k2.hdfs.rollSize = 134217700
     #文件的滚动与Event数量无关
     a2.sinks.k2.hdfs.rollCount = 0
    
     # Use a channel which buffers events in memory
     a2.channels.c2.type = memory
     a2.channels.c2.capacity = 1000
     a2.channels.c2.transactionCapacity = 100
    
     # Bind the source and sink to the channel
     a2.sources.r2.channels = c2
     a2.sinks.k2.channel = c2
    
    
  2. 创建upload文件夹

  3. 运行flume

     bin/flume-ng agent -n a2 -c conf/ -f job/flume-dir-hdfs.conf
    
    
  4. 查看HDFS和文件标记
    在这里插入图片描述
    在这里插入图片描述
    注意:上传的文件不能重名,flume会报错!虽然会上传成功,但重名文件无法进行标记。

3.4、监控目录下的多个追加的文件

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

步骤:

  1. 创建flume配置文件
  2. 执行配置文件开启监控
  3. 向监控文件中追加内容
  4. 查看HDFS上的数据是否同步追加

操作:

  1. 创建配置文件

     a2.sources = r2
     a2.sinks = k2
     a2.channels = c2
    
     # Describe/configure the source
     a2.sources.r2.type = TAILDIR
     a2.sources.r2.positionFile=/export/soft/apache-flume-1.9.0-bin/job/tail_dir.json
     a2.sources.r2.filegroups=f1 f2
     a2.sources.r2.filegroups.f1=/export/soft/apache-flume-1.9.0-bin/job/f1/.*file.*
     a2.sources.r2.filegroups.f2=/export/soft/apache-flume-1.9.0-bin/job/f2/.*log.*
    
     # Describe the sink
     a2.sinks.k2.type = hdfs
     a2.sinks.k2.hdfs.path = hdfs://hadoop100:8020/flume/upload2/%Y%m%d/%H
     #上传文件的前缀
     a2.sinks.k2.hdfs.filePrefix = logs-
     #是否对时间戳取整
     a2.sinks.k2.hdfs.round = true
     #多少时间单位创建一个新的文件夹
     a2.sinks.k2.hdfs.roundValue = 1
     #重新定义时间单位
     a2.sinks.k2.hdfs.roundUnit = hour
     #是否使用本地时间戳
     a2.sinks.k2.hdfs.useLocalTimeStamp = true
     #积攒多少个Event才flush到HDFS一次
    a2.sinks.k2.hdfs.batchSize = 100
     #设置文件类型,可支持压缩
     a2.sinks.k2.hdfs.fileType = DataStream
     #多久生成一个新的文件
     a2.sinks.k2.hdfs.rollInterval = 20
     #设置每个文件的滚动大小
     a2.sinks.k2.hdfs.rollSize = 134217700
     #文件的滚动与Event数量无关
     a2.sinks.k2.hdfs.rollCount = 0
    
     # Use a channel which buffers events in memory
     a2.channels.c2.type = memory
     a2.channels.c2.capacity = 1000
     a2.channels.c2.transactionCapacity = 100
    
     # Bind the source and sink to the channel
     a2.sources.r2.channels = c2
     a2.sinks.k2.channel = c2
    
    
  2. 创建上传文件夹f1和f2

  3. 运行flume

     bin/flume-ng agent -n a2 -c conf/ -f job/flume-taildir-hdfs.conf
    
  4. 创建测试文件file.txt和log.txt,并使用echo追加内容

    cd /export/soft/apache-flume-1.9.0-bin/job/f1
    vim file.txt
    echo nihao  >>file.txt
    cd /export/soft/apache-flume-1.9.0-bin/job/f2
     vim log.txt
    echo nihao  >>log.txt
    
  5. 查看HDFS中追加的内容是否记录

  6. 查看记录位置文件tail_dir.json
    在这里插入图片描述

4、进阶

4.1、事务

在这里插入图片描述

4.2、内部原理

在这里插入图片描述

4.3、拓扑结构
  1. 简单串联
    在这里插入图片描述
    将多个flume顺序连起来,不建议连接过多flume,会影响传输速度和稳定性。

  2. 复制和多路复用
    在这里插入图片描述
    将事件流向一个或多个目的地,这种模式可以将相同的数据复制到多个channel中并分发到其他channel中。

  3. 负载均衡和故障转移
    在这里插入图片描述
    flume支持使用多个sink逻辑上分成一组,sink组配合不同的SinkProcessor来实现负载均衡和故障转移。

  4. 聚合
    在这里插入图片描述
    生产环境中较为常见,使用flume将所有日志进行聚合

4.4、自定义(待续)
  1. 自定义拦截器
  2. 自定义source
  3. 自定义sink

5、监控工具Ganglia(待续)

5.1、下载安装
5.2、启动使用
  • 17
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值