一、Flume安装部署

1. 安装配置

1) 上传安装包到数据源所在节点上
2) 解压:tar -zxvf 安装包
3) 进入安装目录下的conf下
    cp flume-env.sh.template flume-env.sh
    修改flume-env.sh,配置JAVA_HOME
4) 在flume安装路径下创建一个目录agentconf,在该目录下创建一个配置文件:
    netcat-logger.properties,配置文件内容在下面:
    mkdir agentconf
    touch netcat-logger.properties
5) 启动agent去采集数据
    在flume安装路径下执行:
    bin/flume-ng agent -c conf -f agentconf/netcat-logger.properties
     -n a1 -Dflume.root.logger=INFO,console
6) 测试
     在安装flume节点:执行telnet localhost 44444
     输入数据,数据就会被采集到
     首先要安装nc,telnet
     sudo yum -y install nc
     sudo yum -y install telnet
配置文件内容:
# 定义这个 agent 中各个组件的名字
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 描述和配置 source 组件:r1
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# 描述和配置 sink 组件:k1
a1.sinks.k1.type = logger

# 描述和配置 channel 组件,此处使用是内存缓存的方式
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 描述和配置 source channel sink 之间的连接关系
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

2. Flume实战案例

1) 采集目录到HDFS

采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件
    出现,就需要把文件采集到HDFS中去
        三大要素:
        数据源组件,即source--监控文件目录:spooldir
        下沉组件,即sink--HDFS文件系:hdfs sink
        通道组件,即channel--可用file channel也可以用内存channel
        spooldir特性:
            1、监视一个目录,只要目录中出现新文件,就会采集文件中的内容
            2、采集完成的文件,会被agent自动添加一个后缀:.COMPLETED
            3、所监视的目录不允许重复出现相同文件名的文件
    1、配置:
        在flume安装路径的agentconf下新建配置文件:spooldir-hdfs.properties,
        配置文件内容在下面:
    2、启动:
        在flume安装路径下启动:
        bin/flume-ng agent -c conf -f agentconf/spooldir-hdfs.properties
         -n agent1
    3、测试:
        (1)如果 HDFS 集群是高可用集群,那么必须要放入 core-site.xml 和 
            hdfs-site.xml 文件到$FLUME_HOME/conf 目录中
        (2)查看监控的/home/Hadoop/logs 文件夹中的文件是否被正确上传到HDFS上
        (3)在该目录中创建文件,或者从其他目录往该目录加入文件,验证是否新增的
            文件能被自动的上传到 HDFS
配置文件内容:spooldir-hdfs.properties

#定义三大组件的名称
agent1.sources = source1
agent1.sinks = sink1
agent1.channels = channel1

# 配置 source 组件
agent1.sources.source1.type = spooldir
agent1.sources.source1.spoolDir = /home/centos/logs/
agent1.sources.source1.fileHeader = false

#配置拦截器
agent1.sources.source1.interceptors = i1
agent1.sources.source1.interceptors.i1.type = host
agent1.sources.source1.interceptors.i1.hostHeader = hostname
# 配置 sink 组件
agent1.sinks.sink1.type = hdfs
agent1.sinks.sink1.hdfs.path=hdfs://bd1804/flume_log/%y-%m-%d/%H-%M
agent1.sinks.sink1.hdfs.filePrefix = events
agent1.sinks.sink1.hdfs.maxOpenFiles = 5000
agent1.sinks.sink1.hdfs.batchSize= 100
agent1.sinks.sink1.hdfs.fileType = DataStream
agent1.sinks.sink1.hdfs.writeFormat =Text
agent1.sinks.sink1.hdfs.rollSize = 102400
agent1.sinks.sink1.hdfs.rollCount = 1000000
agent1.sinks.sink1.hdfs.rollInterval = 60
#agent1.sinks.sink1.hdfs.round = true
#agent1.sinks.sink1.hdfs.roundValue = 10
#agent1.sinks.sink1.hdfs.roundUnit = minute
agent1.sinks.sink1.hdfs.useLocalTimeStamp = true
# Use a channel which buffers events in memory
agent1.channels.channel1.type = memory
agent1.channels.channel1.keep-alive = 120
agent1.channels.channel1.capacity = 500000
agent1.channels.channel1.transactionCapacity = 600

# Bind the source and sink to the channel
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1

2) 采集文件到HDFS

采集需求:业务系统使用log4j生成的日志,日志内容不断增加,需要把追
    加到日志文件中的数据实时采集到HDFS
        三大要素:
        采集源,即source--监控文件内容更新:exec 'tail -F file'
        其他同上
    1、配置:
        在flume安装路径的agentconf下新建配置文件:tail-hdfs.properties,
        配置文件内容在下面:
    2、启动:
        在flume安装路径下启动:
        bin/flume-ng agent -c conf -f agentconf/tail-hdfs.properties
         -n agent1
    3、测试:
        (1)模拟像指定的日志文件/home/hadoop/logs/catalina.out 追加内容
        (2)验证 HDFS 上的对应文件是否有新增内容
配置文件内容:tail-hdfs.properties

agent1.sources = source1
agent1.sinks = sink1
agent1.channels = channel1

# Describe/configure tail -F source1
agent1.sources.source1.type = exec
agent1.sources.source1.command = tail -F /home/hadoop/logs/catalina.out
agent1.sources.source1.channels = channel1

#configure host for source
agent1.sources.source1.interceptors = i1
agent1.sources.source1.interceptors.i1.type = host
agent1.sources.source1.interceptors.i1.hostHeader = hostname

# Describe sink1
agent1.sinks.sink1.type = hdfs
#a1.sinks.k1.channel = c1
agent1.sinks.sink1.hdfs.path =hdfs://bd1804/weblog/flume-event/%y-%m-%d/%H-%M
agent1.sinks.sink1.hdfs.filePrefix = tomcat_
agent1.sinks.sink1.hdfs.maxOpenFiles = 5000
agent1.sinks.sink1.hdfs.batchSize= 100
agent1.sinks.sink1.hdfs.fileType = DataStream
agent1.sinks.sink1.hdfs.writeFormat =Text
agent1.sinks.sink1.hdfs.rollSize = 102400
agent1.sinks.sink1.hdfs.rollCount = 1000000
agent1.sinks.sink1.hdfs.rollInterval = 60
agent1.sinks.sink1.hdfs.round = true
agent1.sinks.sink1.hdfs.roundValue = 10
agent1.sinks.sink1.hdfs.roundUnit = minute
agent1.sinks.sink1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in memory
agent1.channels.channel1.type = memory
agent1.channels.channel1.keep-alive = 120
agent1.channels.channel1.capacity = 500000
agent1.channels.channel1.transactionCapacity = 600

# Bind the source and sink to the channel
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1

3) 多路复用采集

两台机器上:
    centos01:tail-avro.properties
            使用 exec “tail -F /home/hadoop/testlog/welog.log”获取采集数据
            使用 avro sink 数据都下一个 agent
    centos02:avro-hdfs.properties
            使用 avro 接收采集数据
            使用 hdfs sink 数据到目的地
    1、配置:
        在centos01上flume安装路径的agentconf下新建配置文件: tail-avro.properties:
        在centos02上flume安装路径的agentconf下新建配置文件: avro-hdfs.properties:
        配置文件内容在下面:
    2、启动:
        在对应机器上flume安装路径下先启动centos02上的agent:
        bin/flume-ng agent -c conf -f agentconf/avro-hdfs.properties
    -n a1 -Dflume.root.logger=INFO,console
        在对应机器上flume安装路径下后启动centos01上的agent:
        bin/flume-ng agent -c conf -f agentconf/tail-avro.properties
    -n a1 -Dflume.root.logger=INFO,console
    3、测试:
        执行一个普通的脚本往 centos01的/home/hadoop/testlog/date.log 
    中追加数据
        至此会发现在 centos01 agent 发送的数据会转到 centos02 agent,然后被
    sink 到了HDFS 的对应目录 hdfs://bd1804/testlog/flume-event/
脚本文件
#!/bin/bash
while true
do
echo `date` >> /home/hadoop/testlog/date.log
sleep 1
done
配置文件内容: tail-avro.properties

a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/centos/testlog/date.log
a1.sources.r1.channels = c1
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.channel = c1
a1.sinks.k1.hostname = centos02
a1.sinks.k1.port = 4141
a1.sinks.k1.batch-size = 2

# 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
配置文件内容: avro-hdfs.properties:

a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 4141

# Describe k1
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path =hdfs://bd1804/testlog/flume-event/%y-%m-%d/%H-%M
a1.sinks.k1.hdfs.filePrefix = date_
a1.sinks.k1.hdfs.maxOpenFiles = 5000
a1.sinks.k1.hdfs.batchSize= 100
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat =Text
a1.sinks.k1.hdfs.rollSize = 102400
a1.sinks.k1.hdfs.rollCount = 1000000
a1.sinks.k1.hdfs.rollInterval = 60
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# 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

3. 高可用部署案例

1)需求分析

A、B 两台日志服务机器实时生产日志主要类型为 access.log、nginx.log、web.log
    现在要求:
    把 A、B 机器中的 access.log、nginx.log、web.log 采集汇总到 C 机器上然后统一收集到 hdfs中。
    但是在 hdfs 中要求的目录为:
    /source/logs/access/20160101/**
    /source/logs/nginx/20160101/**
    /source/logs/web/20160101/**

2)准备3台服务器

A服务器:centos01
    B服务器:centos02
    C服务器:centos03

3)设计采集方案

在服务器 A 和服务器 B 上的$FLUME_HOME/agentconf创建采集方案的配置
    文件 exec_source_avro_sink.properties,
        在服务器 C 上的$FLUME_HOME/agentconf 中创建配置文件:
    avro_source_hdfs_sink.properties

4)启动

配置完成之后,在服务器 A 和 B 上的/home/centos/data 有数据文件 
    access.log、nginx.log、web.log。
        先启动服务器 C(centos03)上的 flume,
        启动命令:在 flume 安装目录下执行:
        bin/flume-ng agent -c conf -f 
        agentconf/avro_source_hdfs_sink.properties -name a1 -
        Dflume.root.logger=DEBUG,console
         然后在启动服务器上的 A(centos01)和 B(centos02),
         启动命令:在 flume 安装目录下执行:
        bin/flume-ng agent -c conf -f 
        agentconf/exec_source_avro_sink.properties -name a1 -
        Dflume.root.logger=DEBUG,console

5)测试

向access.log、nginx.log、web.log添加数据,观察hdfs下的文件

配置文件

配置文件 exec_source_avro_sink.properties,文件内容为:

# 指定各个核心组件
a1.sources = r1 r2 r3
a1.sinks = k1
a1.channels = c1

# 准备数据源
## static 拦截器的功能就是往采集到的数据的 header 中插入自己定义的 key-value 对
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/centos/flume_data/access.log
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
a1.sources.r1.interceptors.i1.key = type
a1.sources.r1.interceptors.i1.value = access

a1.sources.r2.type = exec
a1.sources.r2.command = tail -F /home/centos/flume_data/nginx.log
a1.sources.r2.interceptors = i2
a1.sources.r2.interceptors.i2.type = static
a1.sources.r2.interceptors.i2.key = type
a1.sources.r2.interceptors.i2.value = nginx

a1.sources.r3.type = exec
a1.sources.r3.command = tail -F /home/centos/flume_data/web.log
a1.sources.r3.interceptors = i3
a1.sources.r3.interceptors.i3.type = static
a1.sources.r3.interceptors.i3.key = type
a1.sources.r3.interceptors.i3.value = web

# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = centos03
a1.sinks.k1.port = 41414

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

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sources.r2.channels = c1
a1.sources.r3.channels = c1
a1.sinks.k1.channel = c1
配置文件 avro_source_hdfs_sink.properties,文件内容为:

#定义 agent 名, source、channel、sink 的名称
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#定义 source
a1.sources.r1.type = avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port =41414

#添加时间拦截器
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type=org.apache.flume.interceptor.TimestampInterceptor$Builder

#定义 channels
a1.channels.c1.type = memory
a1.channels.c1.capacity = 20000
a1.channels.c1.transactionCapacity = 10000

#定义 sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path=hdfs://bd1804/source/logs/%{type}/%Y%m%d
a1.sinks.k1.hdfs.filePrefix =events
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
#时间类型
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件不按条数生成
a1.sinks.k1.hdfs.rollCount = 0
#生成的文件按时间生成
a1.sinks.k1.hdfs.rollInterval = 30
#生成的文件按大小生成
a1.sinks.k1.hdfs.rollSize = 10485760
#批量写入 hdfs 的个数
a1.sinks.k1.hdfs.batchSize = 20
#flume 操作 hdfs 的线程数(包括新建,写入等)
a1.sinks.k1.hdfs.threadsPoolSize=10
#操作 hdfs 超时时间
a1.sinks.k1.hdfs.callTimeout=30000

#组装 source、channel、sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1