Flume

一、Flume 安装部署

(1)Flume 官网地址
(2)文档查看地址
(3)下载地址
(4)将 apache-flume-1.7.0-bin.tar.gz 上传到 linux 的/usr/local目录下并解压重命名为flume。
(5)将 flume/conf 下的 flume-env.sh.template 文件修改为 flume-env.sh,并配置 flume- env.sh 文件

mv flume-env.sh.template flume-env.sh
vi flume-env.sh
export JAVA_HOME=/usr/local/jdk8

二、Flume 入门案例

(一)案例需求:使用 Flume 监听一个端口,收集该端口数据,并打印到控制台。

(1)安装 netcat 工具

sudo yum install -y nc

测试使用 netcat 工具向本机的 44444 端口发送内容
在这里插入图片描述在这里插入图片描述
(2)判断 44444 端口是否被占用

sudo netstat -tunlp | grep 44444

(3)在 flume 目录下创建 job 文件夹并进入 job 文件夹并创建 Flume Agent 配置文件 flume-netcat-logger.conf(名字随便起)

cd /usr/local/flume
mkdir job 
cd job
vim flume-netcat-logger.conf

在 flume-netcat-logger.conf 文件中添加如下内容

# Name the components on this agent
#a1:表示agent的名称。r1:表示a1的Source的名称。k1:表示a1的Sink的名称。c1:表示a1的Channel的名称。
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
#type:表示a1的输入源类型为netcat端口。bind:表示a1的监听的主机。port:示a1的监听的端口号。
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# Describe the sink
#表示a1的输出目的地是控制台logger类型
a1.sinks.k1.type = logger

# Use a channel which buffers events in memory
#type:表示a1的channel类型是memory内存型。capacity:表示a1的channel总容量1000个event。transactionCapacity:表示a1的channel传输时收集到了100条event以后再去提交事务。
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
#表示将r1,k1和c1连接起来,注意:一个sink只能对应一个channel ,一个channel 能对应多个sink。
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

:配置文件来源于官方手册 http://flume.apache.org/releases/content/1.7.0/FlumeUserGuide.html
(4)进入 flume 目录下启动

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

此时 flume 作为服务端,则再启动一个114窗口作为客户端
在这里插入图片描述
参数说明

–conf/-c:表示配置文件存储在 conf/目录。

–name/-n:表示给 agent 起名为 a1。

–conf-file/-f:flume 本次启动读取的配置文件是在 job 文件夹下的 flume-netcat-logger.conf 文件。

-Dflume.root.logger=INFO,console :-D 表示 flume 运行时动态修改 flume.root.logger 参数属性值,并将控制台日志打印级别设置为 INFO 级别。

(二)案例需求:实时监控 Hive 日志,并上传到 HDFS 中

(1)flume 要想将数据输出到 HDFS,须持有 Hadoop 相关 jar 包,拷贝到 /usr/local/flume/lib 文件夹下。

commons-configuration-1.6.jar
hadoop-auth-2.7.2.jar
hadoop-common-2.7.2.jar
hadoop-hdfs-2.7.2.jar
commons-io-2.4.jar
htrace-core-3.1.0-incubating.jar

(2)在 job 文件夹下创建 flume-file-hdfs.conf 文件

vim flume-file-hdfs.conf 
# Name the components on this agent 
a2.sources = r2
a2.sinks = k2 
a2.channels = c2

# Describe/configure the source 
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /usr/local/hive-1.2.1/logs/hive.log 
a2.sources.r2.shell = /bin/bash -c

# Describe the sink 
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop114:9000/flume/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k2.hdfs.filePrefix = logs-
#是否按照时间滚动文件夹
a2.sinks.k2.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k2.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k2.hdfs.roundUnit = hour
#是否使用本地时间戳
a2.sinks.k2.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k2.hdfs.batchSize = 1000
#设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream
#多久生成一个新的文 件
a2.sinks.k2.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是 128M
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a2.sinks.k2.hdfs.rollCount = 0

# Use a channel which buffers events in memory 
a2.channels.c2.type = memory 
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel 
a2.sources.r2.channels = c2 
a2.sinks.k2.channel = c2

(3)进入 flume 目录下启动

bin/flume-ng agent -c conf/ -f job/flume-file-hdfs.conf -n a2

(4)再打开一个114窗口开启Hadoop集群,114上启动HDFS,115上启动YARN

cd /usr/local/hadoop-2.7.2
sbin/start-dfs.sh
sbin/start-yarn.sh

(5)启动hive,执行查询可以看到每隔60秒出现一个日志

cd /usr/local/hive-1.2.1/
bin/hive

在这里插入图片描述

(三)案例需求:使用 Flume 监听整个目录的文件,并上传至 HDFS

(1)在 job 文件夹下创建配置文件 flume-dir-hdfs.conf

vim flume-dir-hdfs.conf

添加如下内容

# Name the components on this agent 
a3.sources = r3
a3.sinks = k3 
a3.channels = c3

# Describe/configure the source 
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /usr/local/flume/upload
a3.sources.r3.fileSuffix = .COMPLETED 
a3.sources.r3.fileHeader = true
#忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)

# Describe the sink 
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop114:9000/flume/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k3.hdfs.batchSize = 1000
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文 件
a3.sinks.k3.hdfs.rollInterval = 60 
#设置每个文件的滚动大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a3.sinks.k3.hdfs.rollCount = 0

# Use a channel which buffers events in memory 
a3.channels.c3.type = memory 
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100

# Bind the source and sink to the channel 
a3.sources.r3.channels = c3 
a3.sinks.k3.channel = c3

(2)flume 目录下创建 upload和data文件夹,在data文件夹下创建2个测试文件,分别为123,456

mkdir {upload,data}
cd data
vi test1.txt
vi test2.txt

(3)进入 flume 目录下启动

bin/flume-ng agent -c conf/ -f job/flume-dir-hdfs.conf -n a3

(4)先将data文件夹下的test1文件复制到upload文件夹下,然后看到

cp test1.txt /usr/local/flume/upload/

在这里插入图片描述
(5)再将data文件夹下的test2文件复制到upload文件夹下,然后看到
在这里插入图片描述
等tmp文件完成然后下载下来可以看到其中内容。
说明:在使用 Spooling Directory Source 时 不要在监控目录中创建并修改文件,也不能重复添加相同文件,上传完成的文件会以.COMPLETED 结尾,被监控文件夹每 500 毫秒扫描一次文件变动。

(四)案例需求:实时监控目录下的多个追加文件

说明:Taildir Source 既能够实现断点续传,又可以保证数据不丢失,还能够进行实时监控。
(1)在 job 文件夹下创建配置文件 flume-taildir-hdfs.conf

vim flume-taildir-hdfs.conf

添加如下内容

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

# Describe/configure the source,注意filegroups不能配置成相同的,比如两个filegroups.f1,下面会覆盖上面
a1.sources.r1.type = TAILDIR
a1.sources.r1.positionFile = /usr/local/flume/position/position.json 
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /usr/local/flume/files/file1.txt
a1.sources.r1.filegroups.f2 = /usr/local/flume/files/file2.txt

# 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

(2)flume 目录下创建files 文件夹,在files 文件夹下创建2个测试文件,file1.txt,file.txt

mkdir {files }
cd files
touch {file1.txt,file2.txt}

(3)进入 flume 目录下启动

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

(4)再启动一个窗口进入 files 目录下分别向文件内追加内容,成功之后关掉flume,然后再追加两条,然后启动flume,能实现断点续传。
在这里插入图片描述
(5)上传至HDFS测试

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

# Describe/configure the source,注意filegroups不能配置成相同的,比如两个filegroups.f1,下面会覆盖上面
a1.sources.r1.type = TAILDIR
a1.sources.r1.positionFile = /usr/local/flume/position/position.json 
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /usr/local/flume/files/file1.txt
a1.sources.r1.filegroups.f2 = /usr/local/flume/files/file2.txt

# Describe the sink 
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop114:9000/flume/%Y%m%d/%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a1.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a1.sinks.k1.hdfs.batchSize = 1000
#设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文 件
a1.sinks.k1.hdfs.rollInterval = 60 
#设置每个文件的滚动大小大概是 128M
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a1.sinks.k1.hdfs.rollCount = 0

# 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值