Flume的初步认识

Flume的概述

  • Flume的定义
    Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统.Flume基于流式架构,灵活简单。
  • Flume的优点
    1、可以和任意存储进程集成。
    2、输入的的数据速率大于写入目的存储的速率,flume会进行缓冲,减小hdfs的压力。
    3、flume中的事务基于channel,使用了两个事务模型(sender + receiver),确保消息被可靠发送。
    4、Flume使用两个独立的事务分别负责从soucrce到channel,以及从channel到sink的事件传递。一旦事务中所有的数据全部成功提交到channel,那么source才认为该数据读取完成。同理,只有成功被sink写出去的数据,才会从channel中移除

Flume基础架构

  • Flume架构图如下
    在这里插入图片描述
    Source是数据的源头
    Channel是通道,传输数据
    Sink是消费数据

Flume的安装使用

  • 解压至指定文件夹:tar -zxvf /download/apache-flume-1.7.0-bin.tar.gz -C /home/
  • 重命名:mv mv apache-flume-1.7.0-bin/ flume
  • 在flume根目录下创建一个job文件夹,用来存储配置文件:mkdir job
  • 复制配置文件并且重命名:cp conf/flume-env.sh.template conf/flume-env.sh
  • 修改配置文件flume-env.sh中的jdk路径即可

Flume入门案列

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

实现步骤

  1. 安装netcat工具:yum install -y nc
  2. 判断44444端口是否被占用:netstat -tunlp | grep 44444
  3. 使用netcat命令开启服务端:nc -lk 44444
  4. 使用netcat命令开启客户端:nc 127.0.0.1 44444
  5. 在flume目录下创建job,在job目录下创建Flume Agent配置文件flume-netcat-logger.conf
  6. 在配置文件下添加以下内容
# 在本次agent里为三个组件起别名
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 描述和配置sources
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# 描述sinks
a1.sinks.k1.type = logger

# 描述定义一个channels
a1.channels.c1.type = memory
# 1000个事件是容量
a1.channels.c1.capacity = 1000
# 一次传输的数据量
a1.channels.c1.transactionCapacity = 100

# 绑定sources,channels和sinks
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

注意:sinks只能绑定一个channels,一个channels可以绑定多个sinks
7. flume的测试

# 执行语句
bin/flume-ng agent --conf conf --conf-file /home/flume/job/flume-netcat-logger.conf
 --name a1 -Dflume.root.logger=INFO,console
 # 执行语句这样写也可以
 bin/flume-ng agent -c conf -f /home/flume/job/flume-netcat-logger.conf
 -n a1 -Dflume.root.logger=INFO,console

在这里插入图片描述

实时监控文件

监控单个文件,并且输出到控制台

配置文件

# 定义别名
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 定义监控的文件
a1.sources.r1.type = exec
# 大写的F 如果错了会尝试重新连接,但是如果是小写的f出错了不会重新连接,而是直接放弃
a1.sources.r1.command = tail -F /home/flume/logs/a
# 日志
a1.sinks.k1.type = logger

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动语句

bin/flume-ng agent -c conf -name a1 -f /home/flume/job/file-hdfs-flume.conf
 -Dflume.root.logger=INFO,console

在这里插入图片描述

监控单个文件,并且上传到HDFS

大概流程
在这里插入图片描述

  • 注意:Flume想要将数据输出到HDFS,必须持有Hadoop相关jar包
    commons-configuration-1.6.jar
    hadoop-auth-2.7.2-jar
    hadoop-common-2.7.2.jar
    hadoop-hdfs-2.7.2.jar
    commons-io-2.4.jar
    htrace-core-3.1.0-incubating.jar

配置文件

# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2

# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /home/flume/logs/a
a2.sources.r2.shell = /bin/bash -c

# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://d:9000/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 = 1000
#设置文件类型,可支持压缩
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

启动

监控一个文件夹,上传至HDFS

配置文件

a3.sources = r3
a3.sinks = k3
a3.channels = c3

# Describe/configure the source
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /home/flume/logs/
a3.sources.r3.fileSuffix = .COMPLETED
a3.sources.r3.fileHeader = true
#忽略所有以.tmp结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)

# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://d:9000/flume/upload/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a3.sinks.k3.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a3.sinks.k3.hdfs.rollCount = 0

# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100

# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

运行命令

bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir-hdfs.conf

监控多个追加文件

Exec source 适用于监控一个实时追加的文件,但不能保证数据不丢失;SpooldirSource能够保证数据不丢失,且能够实现断点续传,但延迟较高,不能实时监控;而TaildirSource既能够实现断点续传,又可以保证数据不丢失,还能够进行实时监控。|
在这里插入图片描述

配置文件

# 定义别名
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 定义监控的文件
a1.sources.r1.type = TAILDIR 
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /data/*file1.txt
a1.sources.r1.filegroups.f2 = /data/*file2.txt
a1.sources.r1.positionFile = /data/postition.json
# 日志
a1.sinks.k1.type = logger

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值