Flume框架介绍
文章目录
1,Flume概述
1.1 Flume定义
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构,灵活简单。
1.2 Flume 基础架构
Agent
Agent 是JVM的一个进程,它以event的形式将数据从源头送至目的地,如上图所示
Agent 主要有3个部分组成,Source、Channel、Sink。
Source
Source 是接收外部数据到 Agent 的一个组件。该组件可以处理多种类型和多种格式的日志数据,包括 avro、thrift、exec、jms、spooling directory、netcat、 taildir 、sequence generator、syslog、http、legacy。
Sink
Sink 会不断轮询Channel中的 events 且批量地移出它们,并将这些 events 批量写入到存储中或外部索引系统,再者,被发送到另一个Flume Agent(用于聚合)。
Channel
Channel 是Source和Sink两者之间的缓冲区,Channel运行Source和Sink运作在两个不同的速率上。Channel是线程安全的组件,可以同时接收几个Source传入events和几个Sink读取events。
Flume自带两种Channel:Memory Channel和File Channel。
Memory Channel是内存中的队列。该Channel在不需要关心数据丢失的情况下使用,因为Memory Channel会因为程序死亡、机器宕机或机器重启等原因导致内存中数据还未写入磁盘从而发生数据丢失。
File Channel 将所有events写到了磁盘中,因此不会因为程序死亡等问题丢失数据。
event
event是Flume中的基本数据传输单元。event由Header(用来存放event的一些属性,为K-V结构)和Body(用来存放该条数据,为字节数组)两部分组成。
2,Flume入门案例
1)监控端口数据官方案例
案例需求:
使用Flume监听一个端口,收集该端口数据,并打印到控制台。
需求分析:
实现步骤如下
1 安装netcat工具
hadoop102 software]$ sudo yum install -y nc
2 检查44444端口是否被占用
hadoop102 flume-telnet]$ sudo netstat -nlp | grep 44444
3 进入Flume目录下创建一个job文件夹,在该文件夹下创建并配置flume-netcat-logger.config文件
# 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 = 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
4 先开启flume监听端口
第一种写法:
[hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
第二种写法:
[hadoop102 flume]$ bin/flume-ng agent -c conf/ -n a1 -f job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
参数说明:
–conf/-c:表示配置文件存储在conf/目录
–name/-n:表示给agent起名为a1
–conf-file/-f:flume本次启动读取的配置文件是在job文件夹下的flume-telnet.conf文件。
-Dflume.root.logger=INFO,console :-D表示flume运行时动态修改flume.root.logger参数属性值,并将控制台日志打印级别设置为INFO级别。日志级别包括:log、info、warn、error。
5 使用netcat工具向本机的44444端口发送内容
[hadoop102 ~]$ nc localhost 44444
hello
ysy
6 在Flume监听页面观察接收数据情况
2)实时监控单个追加文件
1 案例需求:实时分析Hive日志,并上传到HDFS上
2 需求如下
实现步骤:
(1)Flume要想将数据输出到HDFS,依赖Hadoop相关jar包
检查/etc/profile.d/my_env.sh文件,确认Hadoop和Java环境变量配置正确
JAVA_HOME=/opt/module/jdk1.8.0_212
HADOOP_HOME=/opt/module/ha/hadoop-3.1.3
PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
export PATH JAVA_HOME HADOOP_HOME
(2)创建flume-file-hdfs.conf文件
创建文件
[@hadoop102 job]$ vim flume-file-hdfs.conf
注:要想读取Linux系统中的文件,就得按照Linux命令的规则执行命令。由于Hive日志在Linux系统中所以读取文件的类型选择:exec即execute执行的意思。表示执行Linux命令来读取文件。
添加如下内容
# 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 /opt/module/hive/logs/hive.log
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop102: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
注意:
对于所有与时间相关的转义序列,Event Header中必须存在以 “timestamp”的key(除非hdfs.useLocalTimeStamp设置为true,此方法会使用TimestampInterceptor自动添加timestamp)。
(3)运行Flume
[hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
(4)开启Hadoop和Hive并操作Hive产生日志
[hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
[hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
[