Flume + Kafka基本是日志實時采集的標(biāo)準(zhǔn)搭檔了。
本篇文章基于Flume-ng-1.6.0-cdh5.7.0
+ CentOS6.7
+ JDK1.6+
-
下載,安裝JDK
1.解壓到 ~/app
2.將java配置系統(tǒng)環(huán)境變量中: vi ~/.bash_profile
export JAVA_HOME=/home/hadoop/app/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
3.source ~/.bash_profile
下讓其配置生效,
4.檢測是否安裝JDK成功:java -version
-
下載,安裝Flume
1.下載,解壓到 ~/app
2.將flume配置到系統(tǒng)環(huán)境變量中: ~/.bash_profile
export FLUME_HOME=/home/hadoop/app/apache-flume-1.6.0-cdh5.7.0-bin
export PATH=$FLUME_HOME/bin:$PATH
3.source ~/.bash_profile
讓其配置生效,
4.修改flume-env.sh
的配置:
cd $FLUME_HOME/conf
cp flume-env.sh.template flume-env.sh
vi flume-env.sh
export JAVA_HOME=/home/hadoop/app/jdk1.8.0_144
5.檢測: flume-ng version
:
Flume 1.6.0-cdh5.7.0
Source code repository: https://git-wip-us.apache.org/repos/asf/flume.git
Revision: 8f5f5143ae30802fe79f9ab96f893e6c54a105d1
Compiled by jenkins on Wed Mar 23 11:38:48 PDT 2016
From source with checksum 50b533f0ffc32db9246405ac4431872e
-
從指定的網(wǎng)絡(luò)端口采集數(shù)據(jù)輸出到控制臺
A single-node Flume configuration
1.使用Flume的關(guān)鍵就是寫配置文件
A) 配置Source
B) 配置Channel
C) 配置Sink
D) 把以上三個組件串起來
a1: agent名稱
r1: source的名稱
k1: sink的名稱
c1: channel的名稱
2.下面是一個簡單的配置文件范例,該例子通過netcat
產(chǎn)生日志,
持續(xù)輸出到console
中。
-
example.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 = hadoop
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
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
1個source可以指定多個channels,而1個sink只能接收來自1個channel的數(shù)據(jù)。
3.啟動agent
flume-ng agent \
--name a1 \
--conf $FLUME_HOME/conf \
--conf-file $FLUME_HOME/conf/example.conf \
-Dflume.root.logger=INFO,console
4.另開窗口,使用telnet進行測試: telnet hadoop 44444
5.輸入測試文字,在flume-ng agent
啟動窗口看到telnet
窗口輸入的文字,以Event
形式顯示:
Event: { headers:{} body: 68 65 6C 6C 6F 0D hello. }
Event
是FLume
數(shù)據(jù)傳輸?shù)幕締卧?br>
Event = 可選的header + byte array
-
監(jiān)控一個文件實時采集增量數(shù)據(jù)輸出到控制臺
1.首先新增exec-memory-logger.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 /home/feiyue/data/flume-data.log
a1.sources.r1.shell=/bin/sh -c
# Describe the sink
a1.sinks.k1.type = logger
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2.啟動agent
flume-ng agent \
--name a1 \
--conf $FLUME_HOME/conf \
--conf-file $FLUME_HOME/conf/exec-memory-logger.conf \
-Dflume.root.logger=INFO,console
3.新開窗口echo hello >> flume-data.log,在flume-ng agent
窗口看到監(jiān)視的日志文件內(nèi)容。
-
監(jiān)控一個文件實時采集增量數(shù)據(jù)輸出到HDFS
與上面的做法類似,只是配置信息有些變化:
type – The component type name, needs to be hdfs
hdfs.path – HDFS directory path (eg hdfs://namenode/flume/webdata/)
-
服務(wù)器A(Web APP)上的日志實時采集到服務(wù)器B(HDFS)上
1.技術(shù)選型:
日志服務(wù)器A:exec source + memory channel + avro sink
HDFS服務(wù)器B:avro source + memory channel + logger sink
跨節(jié)點以 avro
文件形式傳輸較為普遍。
2.新增配置文件exec-memory-avro.conf
并修改內(nèi)容
exec-memory-avro.sources = exec-source
exec-memory-avro.sinks = avro-sink
exec-memory-avro.channels = memory-channel
exec-memory-avro.sources.exec-source.type = exec
exec-memory-avro.sources.exec-source.command = tail -F /home/hadoop/data/data.log
exec-memory-avro.sources.exec-source.shell = /bin/sh -c
exec-memory-avro.sinks.avro-sink.type = avro
# send to the configured hostname/port pair
exec-memory-avro.sinks.avro-sink.hostname = 192.168.199.151
exec-memory-avro.sinks.avro-sink.port = 44444
exec-memory-avro.channels.memory-channel.type = memory
exec-memory-avro.sources.exec-source.channels = memory-channel
exec-memory-avro.sinks.avro-sink.channel = memory-channel
3.新增配置文件avro-memory-logger.conf
并修改內(nèi)容
avro-memory-logger.sources = avro-source
avro-memory-logger.sinks = logger-sink
avro-memory-logger.channels = memory-channel
avro-memory-logger.sources.avro-source.type = avro
# Listens on Avro IP and port
avro-memory-logger.sources.avro-source.bind = 192.168.199.151
avro-memory-logger.sources.avro-source.port = 44444
avro-memory-logger.sinks.logger-sink.type = logger
avro-memory-logger.channels.memory-channel.type = memory
avro-memory-logger.sources.avro-source.channels = memory-channel
avro-memory-logger.sinks.logger-sink.channel = memory-channel
4.先啟動avro-memory-logger
,注意順序
flume-ng agent \
--name avro-memory-logger \
--conf $FLUME_HOME/conf \
--conf-file $FLUME_HOME/conf/avro-memory-logger.conf \
-Dflume.root.logger=INFO,console
5.再啟動exec-memory-avro
,注意順序
flume-ng agent \
--name exec-memory-avro \
--conf $FLUME_HOME/conf \
--conf-file $FLUME_HOME/conf/exec-memory-avro.conf \
-Dflume.root.logger=INFO,console
6.在服務(wù)器A上開啟一個窗口,人工模擬往日志文件里輸入內(nèi)容
? echo ccccc >> flume-data.log
? echo 123456789 >> flume-data.log
7.在服務(wù)器B的flume-ng agent
窗口將會看到Event
形式的內(nèi)容輸出,可能略有延遲(內(nèi)存緩存有關(guān))
參考:
http://archive.cloudera.com/cdh5/cdh/5/flume-ng-1.6.0-cdh5.7.0/FlumeUserGuide.html