目录
一. Flume概述
1.1 Flume定义
Flume
是 Cloudera 提供的一个高可用
的,高可靠
的,分布式的海量日志采集
、聚合和传输的系统
。Flume 基于流式架构,灵活简单
1.2 Flume基础架构
Flume 组成架构如下图所示。
1.2.1 Agent
Agent 是一个 JVM 进程
,它以事件
的形式将数据从源头
送至目的
。
Agent 主要有 3 个部分组成,Source、Channel、Sink
。
1.2.2 Source
Source
是负责接收数据到 Flume Agent 的组件。Source 组件可以处理各种类型、各种格式的日志
数据,包括 avro、thrift、exec、jms、spooling directory、netcat、taildir、sequence generator、syslog、http、legacy
。
其中:
logger:打印到控制台
exec: 动态监控文件,但不支持断点续传
spooling directory: 监控一个目录,把目录当中除了忽略掉的(本身可以配置忽略项)进行上传,不支持实时更新的数据
taildir: 支持断点续传
netcat: 监听端口数据
1.2.3 Sink
Sink
不断地轮询 Channel
中的事件
且批量地移除
它们,并将这些事件批量写入
到存储或索引系统
、或者被发送
到另一个 Flume Agent
。
Sink 组件目的地包括 hdfs、logger、avro、thrift、ipc、file、HBase、solr、自定义
。
1.2.4 Channel
Channel
是位于 Source 和 Sink 之间的缓冲区
。因此,Channel 允许 Source 和 Sink 运作在不同的速率上。Channel 是线程安全的,可以同时处理几个 Source 的写入操作和几个Sink 的读取操作。
Flume 自带两种 Channel:Memory Channel(内存)
和 File Channel(磁盘)
。
Memory Channe
l 是内存
中的队列。Memory Channel 在不需要关心数据丢失的情景下适用。如果需要关心数据丢失,那么 Memory Channel 就不应该使用,因为程序死亡、机器宕机或者重启都会导致数据丢失。
File Channel
将所有事件写到磁盘
。因此在程序关闭或机器宕机的情况下不会丢失数据。
1.2.5 Event
传输单元,Flume 数据传输的基本单元,以 Event 的形式将数据从源头送至目的地。
Event
由 Header
和 Body
两部分组成,Header
用来存放该 event
的一些属性
,为 K-V 结构
,Body
用来存放该条数据
,形式为字节数组
。
二. Flume入门
2.1 Flume入门案例
2.1.1 监控端口数据案例
1)案例需求:
使用 Flume 监听一个端口,收集该端口数据,并打印到控制台。
2)需求分析:
3)实现步骤:
(1)安装 netcat 工具
(2)判断 44444 端口是否被占用
(3)在flume目录下的 job 文件夹下创建 Flume Agent 配置文件net-flume-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 = 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
其中:
注:
同台机器
的agent
具有唯一性
:当同台机器开启多个agent任务时,agent的名字必须保持不一样!!!
(4)在服务端hadoop102开启 flume 监听端口
(5)在客户端hadoop102使用 netcat 工具向本机的 44444 端口发送内容
(6)在 Flume 监听页面观察接收数据情况(实时接收),查看效果: