Flume使用

238 阅读5分钟

1.介绍

1.1 概述

Flume 是一种分布式、可靠、可用的服务,用于高效收集、聚合和移动大量日志数据。它具有基于流式数据流的简单灵活的体系结构。它具有可调节的可靠性机制和许多故障转移和恢复机制,具有健壮性和容错性。它使用允许在线分析应用程序的简单可扩展数据模型。 参考:flume.apache.org/

1.2 数据流模型

一个Flume 事件被定义为具有字节有效负载和一组可选字符串属性的数据流单元。

图片.png

  • Agent 是一个 (JVM) 进程,它托管组件,事件通过这些组件从外部源流到下一个目的地(跃点)。如图所示,包含:Source 、 Channel、Sink

  • Source
    负责接收数据到 Flume Agent 的组件。Source 组件可以处理各种类型、各种格式的日志数据,包括 avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。

  • Channel
    Channel 是位于 Source 和 Sink 之间的缓冲区。因此,Channel 允许 Source 和 Sink 运作在不同的速率上。Channel 是线程安全的,可以同时处理几个 Source 的写入操作和几个Sink 的读取操作。

    Flume 自带两种 Channel:Memory Channel 和 File Channel 以及 Kafka Channel。
    Memory Channel:是内存中的队列,在不需要关心数据丢失的情景下适用;
    File Channel :将所有事件写到磁盘。相对Memory Channel 安全一些;
    Kafka Channel:将事件数据写到kafka集群中,比较推荐

  • Sink
    Sink 不断地轮询 Channel 中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个 Flume Agent。 Sink 组件目的地包括 hdfs、logger、avro、thrift、ipc、file、HBase、solr、自定义。

  • Event
    传输单元,Flume 数据传输的基本单元,以 Event 的形式将数据从源头送至目的地。 Event 由 Header 和 Body 两部分组成,Header 用来存放该 event 的一些属性,为 K-V 结构, Body 用来存放该条数据,形式为字节数组。

2.Flume安装

1.1 环境准备

  • java 1.8以上
  • 内存: 根据需要准备、用于源、通道、接收器使用
  • 存储: 根据需要准备、用于通道、接收器使用
  • 指定目录读写权限
  • 安装包:官网下载,本文使用的是apache-flume-1.11.0-bin.tar.gz

1.2 部署安装

  • 上传压缩包文件并解压到指定目录下

    图片.png

  • 修改配置信息
    修改conf/flume-env.sh.template文件

    • 将名称改为flume-env.sh, mv flume-env.sh.template flume-env.sh
    • 添加JAVA_HOME 图片.png
  • 添加环境变量vi /etc/profile,并使其生效source /etc/profile

    图片.png

  • 查看版本flume-ng version

    Flume 1.11.0
    Source code repository: https://git.apache.org/repos/asf/flume.git
    Revision: 1a15927e594fd0d05a59d804b90a9c31ec93f5e1
    Compiled by rgoers on Sun Oct 16 14:44:15 MST 2022
    From source with checksum bbbca682177262aac3a89defde369a37
    

3.使用

创建job目录,并切换到job目录下

3.1 监听端口

文件 flume-netcat-logger.conf配置内容如下:

# Name the components on this agent     定义变量方便调用 加s可以有多个此角色
a1.sources = r1
a1.sinks = k1
a1.channels = c1
 
# Describe/configure the source   此配置属于tcp source 必须是netcat类型
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    使用内存 总大小1000 每次传输100
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
 
# Bind the source and sink to the channel 一个source可以绑定多个channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动命令: flume-ng agent --conf conf/ --name a1 --conf-file job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console

要先启动,然后执行客户端,向端口发送指令 nc 127.0.0.1 44444

图片.png

3.2 监听文件

日志文件上传到HDFS

 Name the components on this agent     定义变量方便调用 加s可以有多个此角色
a1.sources = r1
a1.sinks = k1
a1.channels = c1
 
# Describe/configure the source  
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F  /tmp/model/logs/exec.log

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

# Describe the sink   输出日志文件
a1.sinks.k1.type = hdfs
# hdfs上传路径
a1.sinks.k1.hdfs.path = hdfs://ambari:8020/tmp/flume/test/%Y%m%d_%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = logs-
#是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
#a多少时间单位创建一个新的文件夹  秒 (默认30s)
a1.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位(每分钟滚动一个文件夹)
a1.sinks.k1.hdfs.roundUnit = minute
#是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a1.sinks.k1.hdfs.batchSize = 50
#设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件 秒
a1.sinks.k1.hdfs.rollInterval = 30
#设置每个文件的滚动大小 字节(最好128M)
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a1.sinks.k1.hdfs.rollCount = 0
#最小冗余数(备份数 生成滚动功能则生效roll hadoop本身有此功能 无需配置) 1份 不冗余
a1.sinks.k1.hdfs.minBlockReplicas = 1

# Bind the source and sink to the channel 一个source可以绑定多个channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动:flume-ng agent --conf conf/ --name a1 --conf-file job/flume-file-hdfs-logger.conf 图片.png

错误: 图片.png

3.2 监听目录

3.2.1 目录文件上传到HDFS

# Name the components on this agent     定义变量方便调用 加s可以有多个此角色
a1.sources = r1
a1.sinks = k1
a1.channels = c1
 
# Describe/configure the source  
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /tmp/model/dir-test
a1.sources.r1.fileSuffix = .COMPLETED
a1.sources.r1.fileHeader = true

# 过滤.temp结尾文件
a1.sources.r1.ignorePattern = ([^ ]*\.tmp)

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

# Describe the sink   输出日志文件
a1.sinks.k1.type = hdfs
# hdfs上传路径
a1.sinks.k1.hdfs.path = hdfs://ambari:8020/tmp/flume/test/%Y%m%d_%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
#a多少时间单位创建一个新的文件夹  秒 (默认30s)
a1.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位(每分钟滚动一个文件夹)
a1.sinks.k1.hdfs.roundUnit = minute
#是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a1.sinks.k1.hdfs.batchSize = 50
#设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件 秒
a1.sinks.k1.hdfs.rollInterval = 30
#设置每个文件的滚动大小 字节(最好128M)
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a1.sinks.k1.hdfs.rollCount = 0
#最小冗余数(备份数 生成滚动功能则生效roll hadoop本身有此功能 无需配置) 1份 不冗余
a1.sinks.k1.hdfs.minBlockReplicas = 1

# Bind the source and sink to the channel 一个source可以绑定多个channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动: flume-ng agent --conf conf/ --name a1 --conf-file job/flume-dir-hdfs-logger.conf

执行效果

图片.png

3.2.2 监听日志数据上传到kafka

配置信息

# 定义组件
a1.sources = r1
a1.channels = c1 

# 设置sources
a1.sources.r1.type = TAILDIR
a1.sources.r1.filegroups = f1 
a1.sources.r1.filegroups.f1 = /tmp/model/logs/app.*
a1.sources.r1.positionFile = /tmp/model/flume/file_kafka.json

# 设置channel

a1.channels.c1.type = org.apache.flume.channel.kafka.KafkaChannel
a1.channels.c1.kafka.bootstrap.servers = ambari:6667
a1.channels.c1.kafka.topic = test_topic
a1.channels.c1.parseAsFlumeEvent = false


# 组装
a1.sources.r1.channels = c1

启动:flume-ng agent --conf conf/ --name a1 --conf-file job/flume-dir-kafka-logger.conf

执行效果 图片.png

参考