在一个完整的大数据处理系统中, 除了hdfs+mapreduce(或spark)+hive组成分析系统的核心以外,还须要数据采集、结果数据导出、任务调度等不可或缺的辅助系统, 而这些辅助工具在hadoop生态体系中都有便捷的开源框架,如图所示:
==Source到Channel到Sink之间传递数据的形式是Event事件:Event事件是一个数据流单元==java
a)Source:采集源,用于,用于跟数据源对接,以获取数据 b)Sink:下沉地,采集数据传送的目的地,用于往下一级agent传递数据或者往最终存储系统传递数据 c)Channel:agent内部的数据传输通道,用于从source将数据传递到sink
单个agent采集数据
多级agent之间串联
Flume的体系结构分红三个部分:数据源、Flume、目的地node
数据源种类有不少:能够来自directory、http、kafka等,flume提供了source组件用来采集数据源。linux
source种类:数据库
一、spooling directory source:采集目录中的日志apache
二、htttp source:采集http中的日志segmentfault
三、kafka source:采集kafka中的日志缓存
……安全
采集到的日志须要进行缓存,flume提供了channel组件用来缓存数据。
channel种类:bash
一、memory channel:缓存到内存中(最经常使用)服务器
二、本地文件
三、JDBC channel:经过JDBC缓存到关系型数据库中
四、kafka channel:缓存到kafka中
……
例如: #描述和配置channel组件,此处使用是内存缓存的方式 a1.channels.c1.type=memory #默认该通道中最大的能够存储的event数量 a1.channels.c1.capacity=1000 #每次最大能够从source中拿到或者送到sink中的event数量 a1.channels.c1.transactionCapacity=100 -------------------------------------------------------- #对于channel的配置描述 使用文件作数据的临时缓存 这种的安全性要高 a1.channels.c1.type = file a1.channels.c1.checkpointDir = /home/uplooking/data/flume/checkpoint a1.channels.c1.dataDirs = /home/uplooking/data/flume/data
==生产中通常用的是memory==
缓存的数据最终须要进行保存,flume提供了sink组件用来保存数据。
sink种类:
一、HDFS sink:保存到HDFS中
二、HBase sink:保存到HBase中
三、Hive sink:保存到Hive中
四、kafka sink:保存到kafka中
……
一、Flume的安装很是简单,只须要解压便可,固然,前提是已有hadoop环境。上传安装包到数据源所在节点上 而后解压 tar -zxvf apache-flume-1.6.0-bin.tar.gz 而后进入flume的目录,修改conf下的flume-env.sh,在里面配置JAVA_HOME 二、根据数据采集的需求配置采集方案,在配置文件中进行描述(文件名可任意自定义) 三、指定采集方案配置文件,在相应的节点上启动flume agent
先用一个最简单的例子来测试一下程序环境是否正常 一、先在flume的conf目录下新建一个文件 vi netcat-logger.conf
#定义这个agent中各组件的名字 a1.sources=r1 a1.sinks=k1 a1.channels=c1 #描述和配置source组件:r1 a1.sources.r1.type=netcat #这里若是填的是localhost回环地址,那么只有本机能够访问。若是填写的是server1,其余机器就能够访问了 a1.sources.r1.bind=localhost a1.sources.r1.port=8888 #描述和配置sink组件:k1 a1.sinks.k1.type=logger #描述和配置channel组件,此处使用是内存缓存的方式 a1.channels.c1.type=memory #默认该通道中最大的能够存储的event数量 a1.channels.c1.capacity=1000 #每次最大能够从source中拿到或者送到sink中的event数量 a1.channels.c1.transactionCapacity=100 #描述和配置source,channel,sink之间的链接关系。注意,这里的sources的channel有s。不要漏了 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.conf 指定咱们所描述的采集方案 -n a1 指定咱们这个agent的名字 3.测试 先要往agent采集监听的端口上发送数据,让agent有数据可采 随便在一个能跟agent节点联网的机器上
telnet agent-hostname port
==题外话:常常有人问到linux中硬连接和软连接的区别:只需记得硬连接实际上只是一个引用,就跟java中的对应同样。而软件连接其实是一个文件,当咱们用rm -rf去删除一个使用了软件连接的文件时,会把该文件真正删掉==
采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就采集 根据需求,首先定义一下3大要素
编写配置文件
# 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 = /home/hadoop/flumespool #表示在flume读取数据以后,是否在封装出来的event中将文件名添加到event的header中。 a1.sources.r1.fileHeader = true # Describe the sink a1.sinks.k1.type = logger # channel以缓存的方式 a1.channels.c1.type = memory #channel中最多能够缓存1000个event a1.channels.c1.capacity = 1000 #100个event会传输到channel或指定目的地 a1.channels.c1.transactionCapacity = 100 # Bind the source and sink to the channel a1.sources.r1.channels = c1 a1.sinks.k1.channel = c1
启动
bin/flume-ng agent -c conf -f conf/spoodir-logger.conf -n a1 -Dflume.root.logger=INFO,console
采集需求:好比业务系统使用log4j生成的日志,日志内容不断增长,须要把追加到日志文件中的数据实时采集到hdfs 根据需求,首先定义如下3大要素
1.配置文件编写
# Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 #exec 指的是命令 # Describe/configure the source a1.sources.r1.type = exec #F根据文件名追中, f根据文件的nodeid追中 a1.sources.r1.command = tail -F /home/hadoop/log/test.log a1.sources.r1.channels = c1 # Describe the sink #下沉目标 a1.sinks.k1.type = hdfs a1.sinks.k1.channel = c1 #指定目录, flum帮作目的替换 a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/ #文件的命名, 前缀 a1.sinks.k1.hdfs.filePrefix = events- #10 分钟就改目录 a1.sinks.k1.hdfs.round = true a1.sinks.k1.hdfs.roundValue = 10 a1.sinks.k1.hdfs.roundUnit = minute #文件滚动以前的等待时间(秒) a1.sinks.k1.hdfs.rollInterval = 3 #文件滚动的大小限制(bytes) a1.sinks.k1.hdfs.rollSize = 500 #写入多少个event数据后滚动文件(事件个数)。也就是说写入20个event或者文件满500字节或者等待3秒,该文件就会滚动一次。 a1.sinks.k1.hdfs.rollCount = 20 #5个事件就往里面写入(flush到hdfs) a1.sinks.k1.hdfs.batchSize = 5 #用本地时间格式化目录 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
2.仿照日志生成脚本
#!/bin/bash while true do echo iamkris >> /home/hadoop/log/test.log sleep 1 done
3.启动
bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1 ./makelog.sh
当咱们有多个agent,多个agent之间的通讯能够经过配置avro实现 1.编写avro客户端配置文件
# 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 /home/hadoop/log/test.log a1.sources.r1.channels = c1 # Describe the sink #绑定的不是本机, 是另一台机器的服务地址, sink端的avro是一个发送端, avro的客户端, 往server2这个机器上发 a1.sinks = k1 a1.sinks.k1.type = avro a1.sinks.k1.channel = c1 a1.sinks.k1.hostname = server2 a1.sinks.k1.port = 4141 a1.sinks.k1.batch-size = 2 # 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.编写avro服务端配置文件
a1.sources=r1 a1.sinks=k1 a1.channels=c1 #avro服务端 a1.sources.r1.type=avro #绑定本机的任何地址进行接收 a1.sources.r1.bind=0.0.0.0 a1.sources.r1.port=4141 a1.sinks.k1.type=hdfs a1.sinks.k1.hdfs.path=/flume/avrotohdfs/%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=60 a1.sinks.k1.hdfs.rollSize=500 a1.sinks.k1.hdfs.rollCount=20 a1.sinks.k1.hdfs.batchSize=5 a1.sinks.k1.hdfs.useLocalTimeStamp=true a1.sinks.k1.hdfs.fileType=DataStream a1.channels.c1.type=memory a1.channels.c1.capacity=1000 a1.channels.c1.transactionCapacity=100 a1.sources.r1.channels=c1 a1.sinks.k1.channel=c1
3.启动每一个agent
#avro服务端 bin/flume-ng agent -c conf -f conf/avro-hdfs.conf -n a1 #avro客户端 bin/flume-ng agent -c conf -f conf/tail-avro.conf -n a1
a1.sources=r1 a1.channels=c1 a1.sinks=k1 a1.sources.r1.type=exec a1.sources.r1.command=tail -F /export/servers/logs/data/data.log a1.channels.c1.type=memory a1.channels.c1.capacity=1000 a1.channels.c1.transationCapacity=100 a1.sinks.k1.type=org.apache.flume.sink.kafka.KafkaSink a1.sinks.k1.topic=flumetokafka a1.sinks.k1.brokerList=server1:9092 a1.sinks.k1.requiredAcks=1 a1.sources.r1.channels=c1 a1.sinks.k1.channel=c1
bin/flume-ng agent -n a1 -c conf -f conf/catdata.conf -Dflume.root.logger=INFO,console
喜欢就关注公众号:喜讯XiCent