数据采集-flume的使用

前言

<code>在一个完整的大数据处理系统中, 

除了hdfs+mapreduce(或spark)+hive组成分析系统的核心之外,还需要数据采集、结果数据导出、任务调度等不可或缺的辅助系统,
而这些辅助工具在hadoop生态体系中都有便捷的开源框架,如图所示:/<code>
数据采集-flume的使用

日志采集框架Flume

Flume介绍

概述

  • Flume是一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统。
  • Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集的数据输出到HDFS、hbase、kafka等众多外部存储系统中
  • 一般的采集需求,通过对flume的简单配置即可实现
  • Flume针对特殊场景也具备良好的自定义扩展能力,因此,flume可以适用于大部分的日志数据采集场景。

运行机制

  1. Flume分布式系统中最核心的角色是agent,flume采集系统就是由一个个agent所连接形成
  2. 每一个agent相当于一个数据传递员,内部有三个组件:

==Source到Channel到Sink之间传递数据的形式是Event事件:Event事件是一个数据流单元==

<code>a)Source:采集源,用于,用于跟数据源对接,以获取数据

b)Sink:下沉地,采集数据传送的目的地,用于往下一级agent传递数据或者往最终存储系统传递数据

c)Channel:agent内部的数据传输通道,用于从source将数据传递到sink/<code>
数据采集-flume的使用

Flume采集系统结构图

1.简单结构

<code>单个agent采集数据/<code>
数据采集-flume的使用

2.复杂结构

<code>多级agent之间串联/<code>
数据采集-flume的使用

Flume的体系结构

Flume的体系结构分成三个部分:数据源、Flume、目的地

数据源种类有很多:可以来自directory、http、kafka等,flume提供了source组件用来采集数据源。

1、source作用:采集日志

source种类:

1、spooling directory source:采集目录中的日志

2、htttp source:采集http中的日志

3、kafka source:采集kafka中的日志

……

采集到的日志需要进行缓存,flume提供了channel组件用来缓存数据。

2、channel作用:缓存日志

channel种类:

1、memory channel:缓存到内存中(最常用)

2、本地文件

3、JDBC channel:通过JDBC缓存到关系型数据库中

4、kafka channel:缓存到kafka中

……

<code>例如:
#描述和配置channel组件,此处使用是内存缓存的方式
a1.channels.c1.type=memory
#默认该通道中最大的可以存储的event数量
a1.channels.c1.capacity=1000
#每次最大可以从source中拿到或者送到sink中的event数量
a1.channels.c1.transactionCapacity=100
--------------------------------------------------------
#对于channel的配置描述 使用文件做数据的临时缓存 这种的安全性要高
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /home/uplooking/data/flume/checkpoint
a1.channels.c1.dataDirs = /home/uplooking/data/flume/data/<code>

==生产中一般用的是memory==

缓存的数据最终需要进行保存,flume提供了sink组件用来保存数据。

3、sink作用:保存日志

sink种类:

1、HDFS sink:保存到HDFS中

2、HBase sink:保存到HBase中

3、Hive sink:保存到Hive中

4、kafka sink:保存到kafka中

……

Flume实战案例

Flume的安装部署

<code>1、Flume的安装非常简单,只需要解压即可,当然,前提是已有hadoop环境。上传安装包到数据源所在节点上
然后解压 tar -zxvf apache-flume-1.6.0-bin.tar.gz
然后进入flume的目录,修改conf下的flume-env.sh,在里面配置JAVA_HOME
2、根据数据采集的需求配置采集方案,在配置文件中进行描述(文件名可任意自定义)
3、指定采集方案配置文件,在相应的节点上启动flume agent/<code>

示例

<code>先用一个最简单的例子来测试一下程序环境是否正常

1、先在flume的conf目录下新建一个文件
vi netcat-logger.conf/<code>
<code>#定义这个agent中各组件的名字
a1.sources=r1
a1.sinks=k1
a1.channels=c1

#描述和配置source组件:r1
a1.sources.r1.type=netcat
#这里如果填的是localhost回环地址,那么只有本机可以访问。如果填写的是server1,其他机器就可以访问了
a1.sources.r1.bind=localhost
a1.sources.r1.port=8888

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


#描述和配置channel组件,此处使用是内存缓存的方式
a1.channels.c1.type=memory
#默认该通道中最大的可以存储的event数量
a1.channels.c1.capacity=1000
#每次最大可以从source中拿到或者送到sink中的event数量
a1.channels.c1.transactionCapacity=100

#描述和配置source,channel,sink之间的连接关系。注意,这里的sources的channel有s。不要漏了
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1/<code>
<code>2.启动agent去采集数据/<code>
<code>bin/flume-ng agent -c conf -f conf/netcat-logger.conf -n a1 -Dflume.root.logger=INFO,console/<code>
<code>-c conf 指定flume自身的配置文件所在目录
-f conf/netcat-logger.conf 指定我们所描述的采集方案
-n a1 指定我们这个agent的名字

3.测试
先要往agent采集监听的端口上发送数据,让agent有数据可采
随便在一个能跟agent节点联网的机器上/<code>
<code>telnet agent-hostname port/<code>

==题外话:经常有人问到linux中硬链接和软链接的区别:只需记得硬链接实际上只是一个引用,就跟java中的对应一样。而软件链接实际上是一个文件,当我们用rm -rf去删除一个使用了软件链接的文件时,会把该文件真正删掉==

采集案例

采集目录

<code>采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就采集
根据需求,首先定义一下3大要素/<code>
  • 采集源,即source--监控文件目录:spooldir
  • 下沉目标,即sink--logger:logger
  • source和sink之间的传递通道--channel,可用file channel也可用channel编写配置文件
<code># Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = spooldir
#监听的文件目录
a1.sources.r1.spoolDir = /home/hadoop/flumespool
#表示在flume读取数据之后,是否在封装出来的event中将文件名添加到event的header中。
a1.sources.r1.fileHeader = true

# Describe the sink
a1.sinks.k1.type = logger

# channel以缓存的方式
a1.channels.c1.type = memory
#channel中最多可以缓存1000个event
a1.channels.c1.capacity = 1000
#100个event会传输到channel或指定目的地

a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1/<code>
<code>启动/<code>
<code>bin/flume-ng agent -c conf -f conf/spoodir-logger.conf -n a1 -Dflume.root.logger=INFO,console/<code>

采集文件到HDFS

<code>采集需求:比如业务系统使用log4j生成的日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs

根据需求,首先定义以下3大要素/<code>
  • 采集源,即source--监控文件内容更新:exec 'tail -F file'
  • 下沉目标,即sink--HDFS文件系统:hdfs sink
  • source和sink之间的传递通道--channel,可用file channel也可以用内存channel1.配置文件编写
<code># Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#exec 指的是命令
# Describe/configure the source
a1.sources.r1.type = exec
#F根据文件名追中, f根据文件的nodeid追中
a1.sources.r1.command = tail -F /home/hadoop/log/test.log
a1.sources.r1.channels = c1

# Describe the sink

#下沉目标
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
#指定目录, flum帮做目的替换
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
#文件的命名, 前缀
a1.sinks.k1.hdfs.filePrefix = events-

#10 分钟就改目录
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute

#文件滚动之前的等待时间(秒)
a1.sinks.k1.hdfs.rollInterval = 3

#文件滚动的大小限制(bytes)
a1.sinks.k1.hdfs.rollSize = 500

#写入多少个event数据后滚动文件(事件个数)。也就是说写入20个event或者文件满500字节或者等待3秒,该文件就会滚动一次。
a1.sinks.k1.hdfs.rollCount = 20

#5个事件就往里面写入(flush到hdfs)
a1.sinks.k1.hdfs.batchSize = 5

#用本地时间格式化目录
a1.sinks.k1.hdfs.useLocalTimeStamp = true

#下沉后, 生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream

# 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/<code>
<code>2.仿照日志生成脚本/<code>
<code>#!/bin/bash
while true
do
echo iamkris >> /home/hadoop/log/test.log
sleep 1
done/<code>
<code>3.启动/<code>
<code>bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1

./makelog.sh/<code>

配置avro

<code>当我们有多个agent,多个agent之间的通信可以通过配置avro实现

1.编写avro客户端配置文件/<code>
<code># Name the components on this agent
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/hadoop/log/test.log
a1.sources.r1.channels = c1

# Describe the sink
#绑定的不是本机, 是另外一台机器的服务地址, sink端的avro是一个发送端, avro的客户端, 往server2这个机器上发
a1.sinks = k1
a1.sinks.k1.type = avro
a1.sinks.k1.channel = c1
a1.sinks.k1.hostname = server2
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/<code>
<code>2.编写avro服务端配置文件/<code>
<code>a1.sources=r1
a1.sinks=k1
a1.channels=c1

#avro服务端
a1.sources.r1.type=avro
#绑定本机的任何地址进行接收
a1.sources.r1.bind=0.0.0.0
a1.sources.r1.port=4141

a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path=/flume/avrotohdfs/%y-%m-%d/%H-%M
a1.sinks.k1.hdfs.filePrefix=events-

a1.sinks.k1.hdfs.round=true
a1.sinks.k1.hdfs.roundValue=10
a1.sinks.k1.hdfs.roundUnit=minute

a1.sinks.k1.hdfs.rollInterval=60
a1.sinks.k1.hdfs.rollSize=500
a1.sinks.k1.hdfs.rollCount=20

a1.sinks.k1.hdfs.batchSize=5

a1.sinks.k1.hdfs.useLocalTimeStamp=true

a1.sinks.k1.hdfs.fileType=DataStream

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

a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1/<code>
<code>3.启动每个agent/<code>
<code>#avro服务端
bin/flume-ng agent -c conf -f conf/avro-hdfs.conf -n a1

#avro客户端
bin/flume-ng agent -c conf -f conf/tail-avro.conf -n a1/<code>

采集到kafka

config配置

<code>a1.sources=r1
a1.channels=c1
a1.sinks=k1

a1.sources.r1.type=exec
a1.sources.r1.command=tail -F /export/servers/logs/data/data.log

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transationCapacity=100

a1.sinks.k1.type=org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.topic=flumetokafka
a1.sinks.k1.brokerList=server1:9092
a1.sinks.k1.requiredAcks=1


a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1/<code>

启动

<code>bin/flume-ng agent -n a1 -c conf -f conf/catdata.conf -Dflume.root.logger=INFO,console/<code>


分享到:


相關文章: