Flume
一、Flume简介
1) Flume 提供一个分布式的,可靠的,对大数据量的日志进行高效收集、聚集、移动的服务,
Flume 只能在 Unix 环境下运行。
2) Flume 基于流式架构,容错性强,也很灵活简单。
3) Flume、Kafka 用来实时进行数据收集,Spark、Storm 用来实时处理数据,impala 用来实
时查询。
二、Flume角色
Source
用于采集数据,Source是产生数据流的地方,同时Source会将产生的数据流传输到Channel,这个有点类似于Java IO部分的Channel
Channel
用于桥接Sources和Sinks,类似于一个队列。
Sink
从Channel收集数据,将数据写到目标源(可以是下一个Source,也可以是HDFS或者HBase)
Event
传输单元,Flume数据传输的基本单元,以事件的形式将数据从源头送至目的地
三、传输过程
source监控某个文件,文件产生新的数据,拿到该数据后,将数据封装在一个Event中,并put到channel后commit提交,channel队列先进先出,sink去channel队列中拉取数据,然后写入到hdfs或者HBase中。
四、Flume部署及应用
1、文件配置
#flume-env.sh 涉及修改项: export JAVA_HOME=/opt/module/jdk1.8.0_161/ #帮助命令: bin/flume-ng
2、案例
2.1、案例一、Flume监听端口,输出端口数据。
2.1.1创建Flume Agent配置文件job_flume_netcat.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 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.1.2、安装telnet工具
2.1.3、首先判断44444端口是否被占用
netstat -an | grep 44444
2.1.4、先开启flume先听端口
bin/flume-ng agent --conf conf/ --name a1 --conf-file job/job_flume_netcat.conf -Dflume.root.logger==INFO,console
2.1.5、使用telnet工具向本机的44444端口发送内容。
telnet localhost 44444
2.2、案例二:监听上传Hive日志文件到HDFS
2.2.1 拷贝Hadoop相关jar到Flume的lib目录下
cp ../hadoop-2.7.2/share/hadoop/common/lib/hadoop-auth-2.7.2.jar lib/ cp ../hadoop-2.7.2/share/hadoop/common/lib/commons-configuration-1.6.jar lib/ share/hadoop/mapreduce1/lib/hadoop-hdfs-2.5.0-cdh5.3.6.jar share/hadoop/common/hadoop-common-2.5.0-cdh5.3.6.jar
2.2.2 创建flume-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 /opt/module/hive/logs/hive.log a2.sources.r2.shell = /usr/bin/bash -c # Describe the sink a2.sinks.k2.type = hdfs a2.sinks.k2.hdfs.path = hdfs://hadoop102:9000/flume/%Y%m%d/%H #上传文件的前缀 a2.sinks.k2.hdfs.filePrefix = events-hive- #是否按照时间滚动文件夹 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 = 600 #设置每个文件的滚动大小 a2.sinks.k2.hdfs.rollSize = 134210000 #文件的滚动与Event数量无关 a2.sinks.k2.hdfs.rollCount = 0 #最小冗余数 a2.sinks