Flume概述
Flume是一种分布式,可靠且可用的服务,用于有效地收集,聚合和移动大量日志数据。它具有基于流式数据流的简单灵活的架构。它具有可靠性机制和许多故障转移和恢复机制,具有强大的容错性。它使用简单的可扩展数据模型,允许在线分析应用程序。
Flume的中文翻译是“引水槽”,是用于引导水的,而在软件中,它是用于传输数据的。
为什么需要Flume?
从大数据的角度想,大数据的数据是从哪里来?
=>爬虫:如果本公司没有现成的数据,可以通过爬虫从互联网中获取需要的数据
=>日志数据:软件运行时会产生大量的日志文件,这些日志文件也是可以用作数据分析的,可通过flume采集
=>传统型数据库:即Mysql、Oracle数据库,可通过sqoop采集
flume架构
flume主要由三部分组成:
source:数据源,
产生数据流, 同时source将产生的数据流传输到channel。
channel:传输通道
用于桥接Source和Sinks的,把Source产生的数据流传输到Sinks。
Sinks:槽
从channel收集数据,并输出到文件中。
除了上面三种之外,还有一个部分
event:传输单元
Flume数据传输的基本单元,以事件的形式将数据送往目的地。
Flume安装部署
安装包:apache-flume-1.9.0-bin.tar.gz
服务器:由虚拟机搭建的三台CentOS7
Java版本:1.8
- 下载安装包,根据需求下载对应版本的安装包,此处我下载的版本是1.9.0
http://flume.apache.org/download.html - 上传压缩包到服务器(此处假设已按照前面一篇关于“Hadoop安装部署”的博文安装了三台虚拟服务器)
- 解压
tar -zxvf .tar
- 重命名conf/目录下的flume-env.sh.template
mv flume-env.sh.template flume-env.sh.template
- 修改配置文件
export JAVA_HOME=/home/even/hd/jdk1.8.0_141/
- 修改/etc/profile,配置环境变量,(之前以配置了其他的组件。)
export FLUME_HOME=/home/even/hd/flume-1.9.0
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$ZOOKEEPER_HOME/bin:$HIVE_HOME/bin:$FLUME_HOME/bin
使配置生效
source /etc/profile
8. 验证
flume-ng help
Flume使用
端口监听
- 编写监听端口的配置文件,flumejob_telnet.conf
#smple.conf: A single-node Flume configuration
# Name the components on this agent 定义变量方便调用 加s可以有多个此角色
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source 描述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(file) 使用内存 总大小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
# 一个sinks可以只能绑定一个channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
此配置定义名为a1的单个代理(agent)。a1有一个监听端口44444上的数据的source,一个缓冲内存中事件数据的channel,以及一个将事件数据记录到控制台的sink。配置文件命名各种组件,然后描述其类型和配置参数,给定配置文件可能会定义几个命名的代理,当一个给定的Flume进程启动时,需要指定一个代理名,告诉它要使用哪个代理。
- 启动Flume:
flume-ng agent \
--conf conf \
--conf-file flumejob_telnet.conf \
--name a1 \
-Dflume.root.logger=INFO,console
–conf=,指向flume的conf目录,此目录将包括一个脚本flume-env.sh和一个log4j的属性文件;–conf-file指向上面的配置文件;–name指定要运行的agent别名;-Dflume.root.logger是为了强制Flume登录到控制台并把内容显示出来。
- 发送数据(如果没有telnet命令,请安装yum install telnet)
telnet localhost 44444
- 结果:
在启动命令的当前目录会生成一个logs目录,里面会有一个flume.log日志文件记录了flume启动的信息
实时采集文件到HDFS
- 编写配置文件flumejob_hdfs.conf:
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
# exec 执行一个命令的方式去查看文件 tail -F 实时查看
a1.sources.r1.type = exec
# 要执行的脚本command tail -F 默认10行 man tail 查看帮助,此处监听hive的日志
a1.sources.r1.command = tail -F /tmp/root/hive.log
# 执行这个command使用的是哪个脚本 -c 指定使用什么命令
# whereis bash
# bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz
a1.sources.r1.shell = /usr/bin/bash -c
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hd-even-01:9000/flume/%Y%m%d/%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = logs-
#是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹 秒 (默认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 = 100
#设置文件类型,可支持压缩
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
# 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
- 启动hdfs:
start-dfs.sh
- 启动flume:
flume-ng agent \
--conf conf \
--conf-file flumejob_hdfs.conf \
--name a1
- 输出hive命令,生成日志记录:
- 结果:
实时监听文件夹
- 编写flume-dir.conf文件:
# 定义
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = spooldir
# 监控的文件夹
a1.sources.r1.spoolDir = /home/even/hd/spooldir
# 上传成功后显示后缀名
a1.sources.r1.fileSuffix = .COMPLETED
# 如论如何 加绝对路径的文件名 默认false
a1.sources.r1.fileHeader = true
#忽略所有以.tmp 结尾的文件(正在被写入),不上传
# ^以任何开头 出现无限次 以.tmp结尾的
a1.sources.r1.ignorePattern = ([^ ]*\.tmp)
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hd-even-01:9000/flume/spooldir/%Y%m%d/%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = spooldir-
#是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a1.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
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 = 600
#设置每个文件的滚动大小大概是 128M
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a1.sinks.k1.hdfs.rollCount = 0
#最小副本数
a1.sinks.k1.hdfs.minBlockReplicas = 1
# 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
- 启动hdfs
start-dfs.sh
- 启动flume
flume-ng agent \
--conf conf \
--conf-file flumejob_dir.conf \
--name a1
- 在/home/even/hd/spooldir目录下创建一个test.txt,添加内容后保存:
- 结果
多个channel/sink
如图所示,现在要把hive.log日志文件同时采集到hdfs和本地,需要多个channel和sink来实现。
flume1配置文件:
# name the components on this agent
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# 将数据流复制给多个 channel
a1.sources.r1.selector.type = replicating
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /tmp/root/hive.log
a1.sources.r1.shell = /bin/bash -c
# Describe the sink
# 分两个端口发送数据
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hd-even-01
a1.sinks.k1.port = 4141
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hd-even-01
a1.sinks.k2.port = 4142
# Describe the channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.channels.c2.type = memory
a1.channels.c2.capacity = 1000
a1.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2
flume2配置文件:
# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source
a2.sources.r1.type = avro
# 端口抓取数据
a2.sources.r1.bind = hd-even-01
a2.sources.r1.port = 4141
# Describe the sink
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://hd-even-01:9000/flume2/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k1.hdfs.filePrefix = flume2-
#是否按照时间滚动文件夹
a2.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a2.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k1.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a2.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a2.sinks.k1.hdfs.rollInterval = 600
#设置每个文件的滚动大小大概是 128M
a2.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a2.sinks.k1.hdfs.rollCount = 0
#最小副本数
a2.sinks.k1.hdfs.minBlockReplicas = 1
# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
flume3配置文件:
# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hd-even-01
a3.sources.r1.port = 4142
# Describe the sink
a3.sinks.k1.type = file_roll
a3.sinks.k1.sink.directory = /root/flume2
# Describe the channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1
按顺序运行:
flume-ng agent --conf flume-1.9.0/conf --conf-file flumejob_1.conf --name a1
flume-ng agent --conf flume-1.9.0/conf --conf-file flumejob_2.conf --name a2
flume-ng agent --conf flume-1.9.0/conf --conf-file flumejob_3.conf --name a3