(1)、flume提供分布式,可靠的,对海量的日志进行搞笑收集,聚集,移动的服务,flume只能在unix环境下运行;
(2)、flume基于流式架构,容错性强,也很灵活简单;
(3)、flume,kafka用来进行实时数据收集,spark、storm用来实时处理数据,impala用来实时查询;
2.1、source
用于采集收据,source是产生数据流的地方官,同时source会将产生的数据流传输到Channel。
2.2、channel
用于桥接source和sinks,类似于一个队列。
2.3、sinks
从channel收集数据,将数据写到目标源(可以是下一个source,也可以是hdfs或者hbase)。
2.4、event
是flume数据传输的基本单元,以时间的形式讲过数据从源头传送到目的地。
source监控某个文件或者数据流,数据源产生过新的数据,拿到该数据后,将数据封装在一个event中,并put到channel后commit提交,channel队列,先进先出,sink主动去channel队列中拉去数据,然后写到hdfs。
4.1、安装 略
文件配置:修改flume-env.sh,export JAVA_HOME=/usr/local/jdk1.8
4.2、案例
(1)案例一:监控端口数据
目标:flume监控一端Console,另一端Console发消息,使被监控端实时显示。
分布实现:
1、安装telnet工具
rpm -ivh xinetd-2.3.14-40.el6.x86_64.rpm
rpm -ivh telnet-0.17-48.el6.x86_64.rpm
rpm -ivh telnet-server-0.17-48.el6.x86_64.rpm
2、创建flume agent配置文件 flume-telnet.conf
#给agent的组件命名
a1.sources=r1
a1.sinks=k1
a1.channels=c1
#配置source
a1.sources.r1.type=netcat
a1.sources.r1.bind=localhost
a1.sources.r1.port=44444
#配置sink
a1.sinks.type=logger
#配置channel
a1.channel.c1.type=memory
a1.channel.c1.capacity=1000
a1.channel.c1.transactionCapacity=100
#配置channel与source和sink连接
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1 //注意,这里的channel不带s,否则报错
3、判断44444端口是否被占用
netstat -tunlp|grep 44444
4、先开启flume监听端口
bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-telnet.conf
-Dflume.root.logger==INFO,console
(2)案例二:实时读取本地文件到hdfs
目标:实时监控hive日志,并上传到hdfs中
分布实现:
1、拷贝hadoop相关jar包到flume的lib下
cp share/hadoop/common/lib/hadoop-auth-2.5.0-cdh5.3.6.jar ./lib/
cp share/hadoop/common/lib/commons-configuration-1.6.jar ./lib/
cp share/hadoop/mapreduce1/lib/hadoop-hdfs-2.5.0-cdh5.3.6.jar ./lib/
cp share/hadoop/common/hadoop-common-2.5.0-cdh5.3.6.jar ./lib/
cp share/hadoop/hdfs/lib/htrace-core-3.1.0-incubating.jar ./lib/
cp share/hadoop/hdfs/lib/commons-io-2.4.jar ./lib/
提示:最后两个jar包为1.99版本flume必须引用的jar包
2、创建flume-hivelog2hdfs.conf
a2.sources = r2
a2.sinks = k2
a2.channel = c1
a2.sourcers.r2.type = exec
a2.sources.r2.command = tail -F /usr/local/hive2.3/hive.log
a2.sources.r2.shell = /bin/bash -c
a2.sinks.k2 = hdfs
a2.sinks.k2.hdfs.path = hdfs://ns1:8020/flume/%Y%m%d%H
#上传文件的前缀
a2.sinks.k2.hdfs.filePrefix = logs-
#是否按照文件的前缀滚动文件夹
a2.sinks.k2.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.rollInteval = 600
#设置每个文件的滚动大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与event数量无关
a2.sinks.k2.hdfs.rollCount = 0
#最小冗余数
a2.sinks.k2.hdfs.mainBlockReplicas = 1
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2
3、执行监控配置
bin/flume-ng agent --conf conf/ --namea2 --conf-file job/flume-hivelog2hdfs.conf
(3)案例三:实时读取目录文件到hdfs
目标:使用flume监听整个目录的文件
分步实现:
1、创建配置文件 flume-dir2hdfs.conf
a3.sources = r3
a3.sinks = k3
a3.channels = c3
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /root/testdata
# 扫描完成的文件加一个后缀
a3.sources.r2.fileSuffix = .COMPLETED
a3.sources.r2.fileHeader = true
#忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop01:8020/flume/testdata/%Y%m%d/%H
...其余配置同案例2
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3
2、执行测试
bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir.conf
(4)案例四:Flume与Flume之间数据传递:单flume多channel、sink
目标:使用flume-1监控文件变动,分别flume-1将变动内容传给flume-2,flume2负责存储到hdfs;同时flume-1将变动传递给flume-3,flume-3负责输出到local filesystem
分步实现:
1、创建flume-1.conf,用于监控hive.log文件的变动,同时产生两个channel和两个sink分别输送给flume-2和flume-3:
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# 将数据流复制给多个 channel
a1.sources.r1.selector.type = replicating
# source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /usr/loca/hive2.3/hive.log
a1.sources.r1.shell = /bin/bash -c
#sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop01
a1.sinks.k1.port = 4141
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop01
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
2、创建flume-2.conf,用于接收flume-1的event,同时产生一个channel和1个sink,将数据传输给hdfs
a2.sources = r1
a2.sinks = k1
a2.channels = c1
#source
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop01
a2.sources.r1.port = 4141
#sink
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://ns1:8020/flume/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k1.hdfs.filePrefix = flume2-
...
#channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
3、创建flume-3.conf,用于接收flume-1的event,同时产生1个channel和1个sink,将数据传送到本地目录
a3.sources = r1
a3.sinks = k1
a3.channels = c1
#source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop01
a3.sources.r1.port = 4142
#sink
a3.sinks.k1.type = file_roll
a3.sinks.k1.sink.directory = /root/testdata/flume3
#channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1
注意:输出的本地目录必须是已经存在的目录,如果不存在,不会自动创建
4、执行测试:分别开启对应flume-job(依次启动flume-3、flume-2、flume-3)
bin/flume-ng agent --conf conf/ --name a3 --conf-file job/group-job1/flume-3.conf
bin/flume-ng agent --conf conf/ --name a2 --conf-file job/group-job1/flume-2.conf
bin/flume-ng agent --conf conf/ --name a1 --conf-file job/group-job1/flume-1.conf
(5)案例5:flume与flume之间数据传递,多flume汇总数据到单flume
目标:flume-1监控hive.log,flume-2监控某一个端口的数据流,flume-1和flume-2将数据发送给flume-3,flume-3将数据写入hdfs
分步实现:
1、创建flume-1.conf,用于监控hive.log,同时sink数据到flume-3
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /usr/loca/hive2.3/hive.log
a1.sources.r1.shell = /bin/bash -c
#sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop01
a1.sinks.k1.port = 4141
#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、创建flume-2.conf,用于监控44444数据流,同时sink到flume-3
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# source
a2.sources.r1.type = netcat
a2.sources.r1.bind = hadoop01
a2.sources.r1.port = 44444
# sink
a2.sinks.k1.type = avro
a2.sinks.k1.hostname =hadoop01
a2.sinks.k1.port = 4141
# 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
3、创建flume-3.conf用于接收flume-1和flume-2发送的数据,并sink到hdfs
a3.sources = r1
a3.sinks = k1
a3.channels = c1
# source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop01
a3.sources.r1.port = 4141
#sink
a3.sinks.k1.type = hdfs
a3.sinks.k1.hdfs.path = hdfs://hadoop01:8020/flume/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k1.hdfs.filePrefix = flume3-
#是否按照时间滚动文件夹
a3.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k1.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k1.hdfs.rollInterval = 600
#设置每个文件的滚动大小大概是 128M
a3.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a3.sinks.k1.hdfs.rollCount = 0
#最小冗余数
a3.sinks.k1.hdfs.minBlockReplicas = 1
# 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
4、执行测试:分别开启flume-job(依次启动flume-3,flume-2,flume-1)
5.1、Ganglia的安装与部署
(1)安装httpd服务与php
# yum -y install httpd php
(2)安装依赖
# yum -y install rrdtool perl-rrdtool rrdtool-devel
# yum -y install apr-devel
(3)安装ganglia (这里安装centos6 64位版本)
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# yum -y install ganglia-gmetad
# yum -y install ganglia-web
# yum install -y ganglia-gmond
(4)修改配置文件
# vi /etc/httpd/conf.d/ganglia.conf 修改为
Order deny,allow
Deny from all
Allow from all
# Allow from 127.0.0.1
# Allow from ::1
# Allow from .example.com
# vi /etc/ganglia/gmetad.conf 修改为 data_source "linux" ip地址
# vi /etc/ganglia/gmond.conf 修改为
cluster {
name = "linux"
增加两处 udp_send_channel {
host = ip地址
udp_recv_channel {
bind = ip地址
# vi /etc/selinux/config 修改为 SELINUX=disabled
提示:selinux重启生效,或者临时省生效 setenfore 0
(5)启动ganglia
service httpd start
service gmetad star
service gmond start
(6)打开网页浏览ganglia页面
http://hadoop01/gganglia
提示:如果出现权限不足,修改/var/lib/ganglia目录的权限
sudo chmod -R 777 /var/lib/ganglia
5.2、操作flume测试监控
(1)修改flume-env.sh 配置
JAVA_OPTS="-Dflume.monitoring.type=ganglia
-Dflume.monitoring.hosts=你的ip:8649
-Xms100m
-Xmx200m"
(2)启动flume任务
bin/flume-ng agent \
--conf conf/ \
--name a1 \
--conf-file job/group-job0/flume-telnet.conf \
-Dflume.root.logger==INFO,console \
-Dflume.monitoring.type=ganglia \
-Dflume.monitoring.hosts=你的ip:8649
(3)发送数据观察ganglia检测图
telnet localhost 44444