Apache Flume大数据开发工具概述与入门(二)

385 阅读2分钟

「这是我参与2022首次更文挑战的第25天,活动详情查看:2022首次更文挑战」。

Flume 安装部署

  • Flume 的安装非常简单

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

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

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

  • 根据数据采集需求配置采集方案,描述在配置文件中(文件名可任意自定义)

  • 指定采集方案配置文件,在相应的节点上启动 flume agent

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

1、先在 flume 的 conf 目录下新建一个文件

vi netcat-logger.conf

# 定义这个 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、启动 agent 去采集数据

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.con 指定我们所描述的采集方案

-n a1 指定我们这个 agent 的名字

3、测试

先要往 agent 采集监听的端口上发送数据,让 agent 有数据可采。

随便在一个能跟 agent 节点联网的机器上:

telnet anget-hostname port (telnet localhost 44444)

Flume 简单案例

1. 采集目录到 HDFS

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

就需要把文件采集到 HDFS 中去

根据需求,首先定义以下 3 大要素

 采集源,即 source——监控文件目录 : spooldir

 下沉目标,即 sink——HDFS 文件系统 : hdfs sink

 source 和 sink 之间的传递通道——channel,可用 file channel 也可以用

内存 channel

配置文件编写:

# 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.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

Channel 参数解释:

capacity:默认该通道中最大的可以存储的 event 数量

trasactionCapacity:每次最大可以从 source 中拿到或者送到 sink 中的 event

数量