安装部署
离线安装
下载好安装文件传输到服务器,解压并配置
flume源文件下载地址http://archive.apache.org/dist/flume/
在线安装
在线下载文件,解压并配置
wget http://archive.apache.org/dist/flume/1.9.0/apache-flume-1.9.0-bin.tar.gz
解压
tar -zxvf apache-flume-1.9.0-bin.tar.gz
配置
- 将flume-env.sh.template 重命名为flume-env.sh,并追加JAVA_HOME
- 在etc/profile文件中追加flume的配置
mv flume-env.sh.template flume-env.sh
验证
查看当前安装flume的版本,出现以下提示即安装成功
flume-ng version
实战1(nc)-监听端口数据
使用netcat发送信息,在屏幕上显示接收到的信息
netcat安装
功能描述:netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息
基本语法:netstat [选项]
选项参数:
-t或--tcp:显示TCP传输协议的连线状况;
-u或--udp:显示UDP传输协议的连线状况;
-n或--numeric:直接使用ip地址,而不通过域名服务器;
-l或--listening:显示监控中的服务器的Socket;
-p或--programs:显示正在使用Socket的程序识别码(PID)和程序名称
sudo yum install -y nc
确认端口是否被使用
sudo netstat -tunlp | grep 44444
创建配置文件
vim flume-netcat-logger.conf
# a1 是agent的名称,在启动的时候指定
# Name the components on this agent
a1.sources = r1 # r1:数据输入源
a1.sinks = k1 # k1: 数据输出目的地
a1.channels = c1 # c1: 数据缓冲区
# Describe/configure the source
a1.sources.r1.type = netcat # 输入源类型为netcat端口类型
a1.sources.r1.bind = localhost # 输入源监听的主机
a1.sources.r1.port = 44444 # 输入源监听的端口
# Describe the sink
a1.sinks.k1.type = logger # 输出目的地的类型为logger类型
# Use a channel which buffers events in memory
a1.channels.c1.type = memory # 缓冲区类型是内存类型的
a1.channels.c1.capacity = 1000 # 缓冲区总容量是1000个event
a1.channels.c1.transactionCapacity = 100 # 缓冲区收集到100个event会提交一次事务
# Bind the source and sink to the channel
a1.sources.r1.channels = c1 # 将输入源和缓冲区连接起来
a1.sinks.k1.channel = c1 # 将输出目的地和还从去连接起来
启动命令
参数说明:
--conf conf/ :表示配置文件存储在conf/目录
--name a1 :表示给agent起名为a1
--conf-file job/flume-netcat.conf :flume本次启动读取的配置文件是在job文件夹下的flume-telnet.conf文件。
-Dflume.root.logger==INFO,console :-D表示flume运行时动态修改flume.root.logger参数属性值,并将控制台日志打印级别设置为INFO级别。日志级别包括:log、info、warn、error
# 方式1
bin/flume-ng agent --conf conf/ --name a1 --conf-file ../job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
#方式2
bin/flume-ng agent -c conf/ -n a1 -f ../job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
启动后效果如下:
使用nc命令发送数据
# 输入一下命令后回车,发送数据
nc localhost 44444
控制台显示为
实战2(hdfs)-实时读取本地文件数据到HDFS
从日志中时时获取内容推送到hdfs系统中
PS: 注意事项
- 依赖hadoop-3.1.3/share/hadoop/common/lib及hdfs中hadoop-hdfs-3.1.3.jar,都需要拷贝到flum中的lib中
- 同时需要将core-site.xml及hdfs-site.xml文件拷贝到flume/conf中(非必须添加)
- 需要配置/etc/hosts中的大数据ip映射关系
创建实时文件脚本
vi log-output.sh
# 每隔2秒钟输出当前时间
#!/bin/bash
while true
do
echo `date`
sleep 2
done
# 启动脚本并将输出指向test.log文件
sh log-output.sh > test.log
创建flume配置文件
vi hdfs-file.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/share/demo/flume/job/test.log
a2.sources.r2.shell = /bin/bash -c
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop-master: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 = 2
#设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream
#多久生成一个新的文件
a2.sinks.k2.hdfs.rollInterval = 60
#设置每个文件的滚动大小
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
启动命令
./bin/flume-ng agent --conf ./conf/ --name a2 --conf-file ../job/hdfs-file.conf
在hadoop主机9870端口上的文件查询系统中查看的结果如下
实战3(hdfs)-实时读取目录文件到HDFS
创建配置文件
vi hdfs-dir.conf
a3.sources = r3
a3.sinks = k3
a3.channels = c3
# Describe/configure the source
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /usr/local/share/demo/flume/job
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://hadoop-master:9000/flume/files/%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 = 100
#设置文件类型,可支持压缩
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
启动命令
bin/flume-ng agent --conf conf/ --name a3 --conf-file job/hdfs-dir.conf
启动之后在job文件下创建两个文件upload-test1.log、upload-test2.log
在页面上查看效果,发现文件已经同步过来了
实战4(单数据源多出口)-选择器
案例需求:使用Flume-1监控文件变动,Flume-1将变动内容传递给Flume-2,Flume-2负责存储到HDFS;同时Flume-1将变动内容传递给Flume-3,Flume-3负责输出到Local FileSystem。
PS:一共有三个flume客户端,flume1接收数据,其输出是flume2和flume3的数据源,然后flume2和flume3各自输出到自己的目标
创建配置
vi fisrt-flume.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1 k2 # 指定多个目标
a1.channels = c1 c2 # 指定多个缓冲区
# 将数据流复制给所有channel
a1.sources.r1.selector.type = replicating
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /usr/local/share/demo/flume/job/test.log
a1.sources.r1.shell = /bin/bash -c
# Describe the sink
# sink端的avro是一个数据发送者 绑定对应的主机和端口
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = localhost
a1.sinks.k1.port = 4141
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = localhost
a1.sinks.k2.port = 4142
# Describe the channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.channels.c2.type = memory
a1.channels.c2.capacity = 1000
a1.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2
vi hdfs-target.conf
# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source
# source端的avro是一个数据接收服务 -从上一个flume中接收数据
a2.sources.r1.type = avro
a2.sources.r1.bind = localhost
a2.sources.r1.port = 4141
# Describe the sink
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://hadoop-master:9000/flume/multiTarget/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k1.hdfs.filePrefix = flume2-
#是否按照时间滚动文件夹
a2.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a2.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a2.sinks.k1.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a2.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a2.sinks.k1.hdfs.rollInterval = 600
#设置每个文件的滚动大小大概是128M
a2.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a2.sinks.k1.hdfs.rollCount = 0
# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
vi dir-target.conf
# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c2
# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = localhost
a3.sources.r1.port = 4142
# Describe the sink
a3.sinks.k1.type = file_roll
a3.sinks.k1.sink.directory = /usr/local/share/demo/flume/job/group/out
# Describe the channel
a3.channels.c2.type = memory
a3.channels.c2.capacity = 1000
a3.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c2
a3.sinks.k1.channel = c2
启动命令
分三个窗口分别启动三个flume,另一个窗口启动每个两秒中生成一条时间日志
# 启动flume1
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a1 -f ./first-flume.conf
# 启动flume2
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a2 -f ./hdfs-target.conf
# 启动flume3
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a3 -f ./dir-target.conf
结果验证
本地系统中和hdfs系统中,都产生了对应的文件。也可以从flume本地的logs/flume.log中查看执行有没有报错
实战5(单数据源多出口负载均衡)-Sink组
案例需求:使用Flume-1监控文件变动,Flume-1将变动内容传递给Flume-2,Flume-2控制台输出;同时Flume-1将变动内容传递给Flume-3,Flume-3也控制台输出,形成高可用负载
创建配置
vi first-flume.conf
# Name the components on this agent
a1.sources = r1 # 指定数据源
a1.channels = c1 # 指定缓存
a1.sinkgroups = g1 # 指定分组
a1.sinks = k1 k2 # 指定输出
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# 分组之后进行负载
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true
a1.sinkgroups.g1.processor.selector = round_robin # round_robin 轮询 random 随机调度
a1.sinkgroups.g1.processor.selector.maxTimeOut=10000
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = localhost
a1.sinks.k1.port = 4141
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = localhost
a1.sinks.k2.port = 4142
# Describe the channel
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.sinkgroups.g1.sinks = k1 k2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c1
vi flume-console1.conf
# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source 绑定主机接收数据
a2.sources.r1.type = avro
a2.sources.r1.bind = localhost
a2.sources.r1.port = 4141
# Describe the sink
a2.sinks.k1.type = logger
# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
vi flume-console2.conf
# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c2
# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = localhost
a3.sources.r1.port = 4142
# Describe the sink
a3.sinks.k1.type = logger
# Describe the channel
a3.channels.c2.type = memory
a3.channels.c2.capacity = 1000
a3.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c2
a3.sinks.k1.channel = c2
启动命令
分三个窗口分别启动三个flume,另一个窗口使用nc localhost 44444 发送信息
# 启动flume1
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a1 -f ./first-flume.conf -Dflume.root.logger=INFO,console
# 启动flume2
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a2 -f ./flume-console1.conf -Dflume.root.logger=INFO,console
# 启动flume3
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a3 -f ./flume-console2.conf -Dflume.root.logger=INFO,console
PS:nc 发送了1~10这个几个数字,然后在控制台上接收到这些信息
实战6(多数据源汇总)
创建配置
# 数据源1,是通过模拟每秒往日志中追加一个时间字符串实现的
vi data-from1.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 /usr/local/share/demo/flume/job/test.log
a1.sources.r1.shell = /bin/bash -c
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = localhost
a1.sinks.k1.port = 4141
# Describe the channel
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 是通过netcat localhost 44444 发送的信息
vi data-from2.conf
# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source
a2.sources.r1.type = netcat
a2.sources.r1.bind = localhost
a2.sources.r1.port = 44444
# Describe the sink
a2.sinks.k1.type = avro
a2.sinks.k1.hostname = localhost
a2.sinks.k1.port = 4141
# Use a channel which buffers events in memory
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
# 汇总到第三个flume中在控制台输出
vi all-console.conf
# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
# Describe/configure the source 已经汇总到一下绑定的主机及端口上了
a3.sources.r1.type = avro
a3.sources.r1.bind = localhost
a3.sources.r1.port = 4141
# Describe the sink
# Describe the sink
a3.sinks.k1.type = logger
# Describe the channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1
启动命令
启动数据源1和2及flume3,并启动日志,然后使用nc localhost 44444发送信息
# 数据源1
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a1 -f ./data-from1.conf
# 数据源2
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a2 -f ./data-from2.conf
# 控制台输出
../../apache-flume-1.9.0-bin/bin/flume-ng agent -c ../../apache-flume-1.9.0-bin/conf/ -n a3 -f ./all-console.conf -Dflume.root.logger=INFO,console
遇到的问题
2022-06-14 11:25:01,351 (SinkRunner-PollingRunner-DefaultSinkProcessor) [WARN - org.apache.flume.sink.hdfs.HDFSEventSink.process(HDFSEventSink.java:454)] HDFS IO error
org.apache.hadoop.fs.UnsupportedFileSystemException: No FileSystem for scheme "hdfs"
at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:3281)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:3301)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:124)
at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:3352)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:3320)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:479)
at org.apache.hadoop.fs.Path.getFileSystem(Path.java:361)
at org.apache.flume.sink.hdfs.BucketWriter$1.call(BucketWriter.java:255)
at org.apache.flume.sink.hdfs.BucketWriter$1.call(BucketWriter.java:247)
at org.apache.flume.sink.hdfs.BucketWriter$8$1.run(BucketWriter.java:727)
at org.apache.flume.auth.SimpleAuthenticator.execute(SimpleAuthenticator.java:50)
at org.apache.flume.sink.hdfs.BucketWriter$8.call(BucketWriter.java:724)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
此时从hdfs中拷贝core-site.xml文件到flume的conf文件目录中,问题依然如故,在core-site.xml中间中追加以下配置,
fs.hdfs.impl org.apache.hadoop.hdfs.DistributedFileSystem 报错变成以下情况
2022-06-14 11:23:44,766 (SinkRunner-PollingRunner-DefaultSinkProcessor) [ERROR - org.apache.flume.SinkRunner$PollingRunner.run(SinkRunner.java:158)] Unable to deliver event. Exception follows.
org.apache.flume.EventDeliveryException: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.hdfs.DistributedFileSystem not found
at org.apache.flume.sink.hdfs.HDFSEventSink.process(HDFSEventSink.java:464)
at org.apache.flume.sink.DefaultSinkProcessor.process(DefaultSinkProcessor.java:67)
at org.apache.flume.SinkRunner$PollingRunner.run(SinkRunner.java:145)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.hdfs.DistributedFileSystem not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2638)
at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:3269)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:3301)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:124)
at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:3352)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:3320)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:479)
at org.apache.hadoop.fs.Path.getFileSystem(Path.java:361)
at org.apache.flume.sink.hdfs.BucketWriter$1.call(BucketWriter.java:255)
at org.apache.flume.sink.hdfs.BucketWriter$1.call(BucketWriter.java:247)
at org.apache.flume.sink.hdfs.BucketWriter$8$1.run(BucketWriter.java:727)
at org.apache.flume.auth.SimpleAuthenticator.execute(SimpleAuthenticator.java:50)
at org.apache.flume.sink.hdfs.BucketWriter$8.call(BucketWriter.java:724)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
... 1 more
Caused by: java.lang.ClassNotFoundException: Class org.apache.hadoop.hdfs.DistributedFileSystem not found
at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:2542)
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2636)
... 16 more
从错误来看主要原因是缺少包导致的
原因是 Class org.apache.hadoop.hdfs.DistributedFileSystem由原本的hadoop-hdfs.2.x.x.jar中迁移到了hadoop-hdfs-client-3.x.x.jar中了
所以将hadoop-hdfs-client-3.1.3.jar拷贝到flume的lib中,解决该问题
PS: 拷贝所有需要的jar之后,能成功运行,此时,反复进行实验之后,其实只要所有的jar都拷贝之后,对应的core-site.xml和hdfs-site.xml都是有默认的,不需要单独在flume的conf文件中增加,除非有特殊需要的配置