Flume 的基本使用

Flume 的基本使用

Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。

当前 Flume 有两个版本。Flume 0.9X 版本的统称 Flume OG(originalgeneration),Flume1.X 版本的统称 Flume NG(next generation)。由于 FlumeNG 经过核心组件、核心配置以及代码架构重构,与 Flume OG 有很大不同,使用时请注意区分。

我这里使用的是Flume NG。

运行机制

Flume 的核心是把数据从数据源(source)收集过来,在将收集到的数据送到指定的目的地(sink)。为了保证输送的

过程一定成功,在送到目的地(sink)之前,会先缓存数据(channel),待数据真正到达目的地(sink)后,flume 在删除

自己缓存的数据。

Flume 分布式系统中核心的角色是 agent,agent 本身是一个 Java 进程,一般运行在日志收集节点。flume 采集

系统就是由一个个 agent 所连接起来形成。每一个 agent 相当于一个数据传递员,内部有三个组件:

source:采集源,通过这个和数据源对接,采集数据

sink:下沉地,数据的传送目的地,用于向下一级的agent或者最终的存储系统传递数据

channel:agent的内部数据传输通道,用于连接source和sink,在传输过程中,流动的是event,他是Flume内部数据传输的最基本单元。

一个完整的 event 包括:event headers、event body、event 信息,其中event 信息就是 flume 收集到的日记记录。

flume帮助文档官网:http://flume.apache.org/FlumeUserGuide.html

可以通过查看文档来编写采集配置

flume的安装与简单使用

安装

上传安装包到数据源所在节点上

flume安装包下载:http://flume.apache.org/download.html

然后解压 tar -zxvf apache-flume-1.6.0-bin.tar.gz

然后进入 flume 的目录,修改 conf 下的 flume-env.sh,在里面配置 JAVA_HOME

使用

1、在采集数据时需要根据需求来配置采集方案,在conf目录下创建配置文件

2、在相应的节点上启动flume,指定配置的采集方案

用了一个简单实例来测试程序的运行环境

#从网络端口接收数据,下沉到logger
#采集配置文件,netcat-logger.conf

先在conf目录下创建一个文件

vi net-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  指定内存来作为souce和sink之间的传递通道
#capacity:默认该通道中最大的可以存储的 event 数量
#trasactionCapacity:每次最大可以从 source 中拿到或者送到 sink 中的 event数量
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel  把source、channel、sink连接起来
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
启动flume采集数据
#在flume的安装目录下执行
bin/flume-ng agent -c conf -f conf/netcat-logger.conf -n a1 -Dflume.root.logger=INFO,console

-c conf 指定flume自身的配置文件所在目录

-f conf/netcat-logger.conf 指定采集方案

-n a1 指定agent的名字

-Dflume.root.logger=INFO,console 在控制台打印日志

需要往监听的端口上发送数据,这里我们使用telnet这个组件

#传入数据:
#先安装一下telnet
yum install -y telnet

#使用telnet anget-hostname port

$ telnet localhost 44444

Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
Hello world! (回车)
OK

#就能看到打印出的Hello world!

采集目录到 HDFS

采集需求:服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到 HDFS 中去

由此我们可知:

source: 需要监控文件目录----spooldir

sink:HDFS文件系统-----hdfs sink

channel:可以用file channel 或者 memory

编写配置文件

vi spooldir-hdfs.conf

# 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 = /root/logs
a1.sources.r1.fileHeader = true

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = /flume/events/%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 = 3
a1.sinks.k1.hdfs.rollSize = 20
a1.sinks.k1.hdfs.rollCount = 5
a1.sinks.k1.hdfs.batchSize = 1
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

启动采集

bin/flume-ng agent -c conf/ -f conf/spooldir-logger.conf -n a1 -Dflume.root.logger=INFO,console

采集文件到 HDFS

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

由此可知:

source: 监控文件内容更新 : exec ‘tail -F file’

sink:HDFS文件系统-----hdfs sink

channel:可以用file channel 或者 memory

编写配置文件

vi tail-hdfs.conf
# 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 /root/logs/test.log
a1.sources.r1.channels = c1

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = /flume/tailout/%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 = 3
a1.sinks.k1.hdfs.rollSize = 20
a1.sinks.k1.hdfs.rollCount = 5
a1.sinks.k1.hdfs.batchSize = 1
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

启动采集

bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1 -Dflume.root.logger=INFO,console

模拟文件内容的变化,可以通过以下的脚本来实现

while true ; do echo 'access  access....' >>/root/logs/text.log;sleep 0.5;done

猜你喜欢

转载自blog.csdn.net/lsy107816/article/details/84554651