Flume入门

一、安装

1、必备条件

因为要把日志上传到hdfs上,所以需要以下hadoop依赖包:

  • commons-configuration-1.6
  • hadoop-auth-2.5.0-cdh5.3.6
  • hadoop-common-2.5.0-cdh5.3.6
  • hadoop-hdfs-2.5.0-cdh5.3.6

2、安装使用

# 讲压缩包解压安装到cdh目录下,进入flume目录进行测试:
bin/flume-ng version
# 查看flume都有哪些命令
bin/flume-ne

3、说明
本文中的代码相应位置使用的值如下,请根据个人情况自行修改:
Linux用户:hadoop
解压目录:/home/hadoop/cdh
主机名:master
hdfs:hdfs://master:9000
hadoop目录:
flume目录:
hive目录:

二、案例

打开 http://flume.apache.org ,左侧 Documentation > Flume User Guide

1、日志来源于端口的情况下conf的配置
参考官方文档 Set up > A simple example

conf目录下新建flume.conf

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source   注意更换localhost为使用Telnet发送日志的主机名
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

配置好之后启动flume

bin/flume-ng agent --conf conf --conf-file conf/flume.conf --name a1 -Dflume.root.logger=INFO,console

重新启动一个控制台,telnet连接之后发送任意字符,即可在启动flume的控制台看到发送的内容

telnet localhost 44444
hello

如果没有安装Telnet,去下载以下三个文件:

  • telnet-0.17-48.el6.x86_64
  • telnet-server-0.17-48.el6.x86_64
  • xinetd-2.3.14-40.el6.x86_64
# telnet 安装  
rpm -i *.rpm   
# 重新启动xinetd守护进程 
service xinetd restart

2、日志来源于某个目录
参考官方文档 Configuration > Flume Source > Exec Source

conf目录下新建一个exec.conf

## define agent
a1.sources = r1
a1.channels = c1
a1.sinks = k1

## define sources
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/hadoop/cdh/test/text.log
a1.sources.r1.shell = /bin/bash  -c

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

## define sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://master:9000/user/hadoop/flume/hive-logs
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.batchSize = 10

## bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

配置好之后启动flume

 bin/flume-ng agent \
 -c conf \
 -n a1 \
 -f conf/exec.conf \
 -Dflume.root.logger=DEBUG,console
```
如果启动失败,那就是没有把hadoop依赖放入lib目录
> Failed to start agent because dependencies were not found in classpath. 

通过向test.log里面写入内容来模拟日志生成过程,flume就会把text.log里面的内容传到HDFS上
```
# 新建input文件随便写入内容,再新建text.log文件 通过以下命令模拟日志生成
cat input >> text.log
```

## 三、架构
##### Flume架构设计要点

Flume的架构主要有一下几个核心概念:

- Event:一个数据单元,带有一个可选的消息头
- Flow:Event从源点到达目的点的迁移的抽象
- Client:操作位于源点处的Event,将其发送到Flume Agent
- Agent:一个独立的Flume进程,包含组件Source、Channel、Sink
- Source:用来消费传递到该组件的Event
- Channel:中转Event的一个临时存储,保存有Source组件传递过来的Event
- Sink:从Channel中读取并移除Event,将Event传递到Flow Pipeline中的下一个Agent(如果有的话)

Flume NG架构,如图所示:




Flume之所以这么神奇,是源于它自身的一个设计,这个设计就是agent,agent本身是一个Java进程,运行在日志收集节点—所谓日志收集节点就是服务器节点。 

agent里面包含3个核心的组件:source—->channel—–>sink,类似生产者、仓库、消费者的架构。

source:source组件是专门用来收集数据的,可以处理各种类型、各种格式的日志数据,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy、自定义。

channel:source组件把数据收集来以后,临时存放在channel中,即channel组件在agent中是专门用来存放临时数据的——对采集到的数据进行简单的缓存,可以存放在memory、jdbc、file等等。 sink:

sink组件是用于把数据发送到目的地的组件,目的地包括hdfs、logger、avro、thrift、ipc、file、null、Hbase、solr、自定义。 
## 四、理解
> 
Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregating and moving large amounts of log data from many different sources to a centralized data store.

Flume NG是一个分布式、可靠、可用的系统,它能够将不同数据源的海量日志数据进行高效收集、聚合、移动,最后存储到一个中心化数据存储系统中。


## 五、应用场景
多个Agent顺序连接
可以将多个Agent顺序连接起来,将最初的数据源经过收集,存储到最终的存储系统中。这是最简单的情况,一般情况下,应该控制这种顺序连接的Agent的数量,因为数据流经的路径变长了,如果不考虑failover的话,出现故障将影响整个Flow上的Agent收集服务。


多个Agent的数据汇聚到同一个Agent
这种情况应用的场景比较多,比如要收集Web网站的用户行为日志,Web网站为了可用性使用的负载均衡的集群模式,每个节点都产生用户行为日志,可以为每个节点都配置一个Agent来单独收集日志数据,然后多个Agent将数据最终汇聚到一个用来存储数据存储系统,如HDFS上。

多路(Multiplexing)Agent
这种模式,有两种方式,一种是用来复制(Replication),另一种是用来分流(Multiplexing)。Replication方式,可以将最前端的数据源复制多份,分别传递到多个channel中,每个channel接收到的数据都是相同的,

实现load balance功能
Load balancing Sink Processor能够实现load balance功能,上图Agent1是一个路由节点,负责将Channel暂存的Event均衡到对应的多个Sink组件上,而每个Sink组件分别连接到一个独立的Agent上


实现failover能
Failover Sink Processor能够实现failover功能,具体流程类似load balance,但是内部处理机制与load balance完全不同:Failover Sink Processor维护一个优先级Sink组件列表,只要有一个Sink组件可用,Event就被传递到下一个组件。如果一个Sink能够成功处理Event,则会加入到一个Pool中,否则会被移出Pool并计算失败次数,设置一个惩罚因子,


## 参考文章
[Flume架构以及应用介绍](http://blog.csdn.net/a2011480169/article/details/51544664)
[Flume(NG)架构设计要点及配置实践](http://shiyanjun.cn/archives/915.html)

## Flume好文
[基于Flume的美团日志收集系统(一)架构和设计](http://www.aboutyun.com/thread-8317-1-1.html)

你可能感兴趣的:(Flume入门)