面對以上的問題,我們如何將這些日志移動到hdfs集群上尼????
第一種方案:使用shell腳本cp 文件,然后通過hdfs fs -put 源文件? hdfs目錄。
此方案可行,但是優缺點:如下
1)缺乏監控機制、如果某臺日志服務器宕機了怎么辦?
2)如果使用腳本,肯定要對腳本做定時觸發的機制,每隔一分鐘或者兩分鐘,這個時效性就會打折扣
3)文件都是存放在服務器磁盤上的,如果要移動日志文件,肯定會產生較大的磁盤IO,必須要壓縮傳輸
4)業務服務器一般都是多節點部署的,肯定會產生多份日志文件,如何把這些日志聚合在一起?
因此這時候就出現了一個較為可靠的解決方案Flume框架
Flume概述
官網地址:http://flume.apache.org/
介紹:他就是把我們的日志,從a地方搬運到b地方的服務框架
Source:收集
詳細介紹:http://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html#flume-sources
Channel:相當于通道,是數據臨時存放的地方,聚合、緩沖區
詳細介紹:http://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html#flume-sources
Sink:輸出
詳細介紹:http://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html#flume-sources
Flume三種流行的使用方案
第一種:
第二種:(最常用的)
第三種:
Flume部署
前置條件:
1)Java Runtime Environment - Java 1.8 or later
2)Memory - Sufficient memory for configurations used by sources, channels or sinks
3)Disk Space - Sufficient disk space for configurations used by channels or sinks
4)Directory Permissions - Read/Write permissions for directories used by agent
第一步:
下載jdk
安裝jdk
配置環境變量
第二步:
下載flume,地址為http://archive.cloudera.com/cdh5/cdh/5/
weget?http://archive.cloudera.com/cdh5/cdh/5/flume-ng-1.6.0-cdh5.7.0.tar.gz
解壓:tar -zvxf? flume-ng-1.6.0-cdh5.7.0.tar.gz? -C ~/usr/目錄下
配置環境變量:
1、vim /etc/profile
export FLUME_HOME=/usr/apache-flume-1.6.0-cdh5.7.0-bin
export PATH=$PATH:$FLUME_HOME/bin
2、//使環境變量配置生效
source /etc/profile
3、配置flume根目錄中conf目錄中文件flume-env.sh的配置參數,復制flume-env.sh.template成flume-env.sh在flume-env.sh中設置flume依賴的jdk。
4、檢測flume是否安裝成功,切換到flume的bin目錄/usr/apache-flume-1.6.0-cdh5.7.0-bin/bin?
執行flume-ng version命令查看flume的版本,如果出現下圖,則表示安裝成功
如何配置Agent?
# example.conf: A single-node Flume configuration
###自己的理解:使用Flume的關鍵就是寫配置文件
A)配置Source
B)配置Channel
C)配置Sink
D)把以上三個組件串聯起來
a1:agent的名稱
r1:source的名稱
k1:sink的名稱
c1:channel的名稱
# 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 ##綁定的IP地址
a1.sources.r1.port=44444? ##需要監聽的服務端口,如果這個端口的服務有內容輸出,就會被監控到,并把數據通過flume的事件發送過來。
解析如下:
# 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
###把source-channel-sink建立聯系
a1.sources.r1.channels=c1? ##一個agent的source可以指定多個channel,因此這里時channels
a1.sinks.k1.channel=c1 ##而一個agent的sink只能對應一個channel。
把這個配置內容保存在一個文件里面,存放在flume的根目錄conf文件夾下,我自己喜歡命名:
flume-setting.conf
啟動flume
bin/flume-ng agent --conf $FLUME_HOME/conf --conf-file $FLUME_HOME/conf/flume-setting.conf --name a1 -Dflume.root.logger=INFO,console
測試是否能正常監聽端口
使用telnet ip 監控的端口
上圖有Event內容,這里面有很多含義,咱們拿一個樣例來說下。
Event:{headers:{} body: 68 65 6C 6C 6F 0D? ? ? ?hello.}為例子。
Event:是Flume數據傳輸的基本單元
Event:是可選的headers+ byte array
至此,從指定的網絡端口采集日志數據輸出到控制臺已經完成了。
第二個需求
要實現這個需求,我們就需要再重flume中做適合該需求的Agent的選項
在官網查找下來,定下了這個選型:exec Source? +? memory Channel? +? logger Sink
我們把上面的需求的配置文件改動下:
# Name the components on this agent
a1.sources=r1
a1.sinks=k1
a1.channels=c1
# Describe/configure the? source
a1.sources.r1.type=exec? ? ##shell命令類型的
a1.sources.r1.command=tail -F /usr/test.log ##綁定監控的日志文件
a1.sources.r1.shell=/bin/bash -c? ? ##固定填寫
# 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
###把source-channel-sink建立聯系
a1.sources.r1.channels=c1? ##一個agent的source可以指定多個channel,因此這里時channels
a1.sinks.k1.channel=c1 ##而一個agent的sink只能對應一個channel。
把這個配置內容保存在一個文件里面,存放在flume的根目錄conf文件夾下,我自己喜歡命名:
exec-memory-logger.conf
然后啟動
bin/flume-ng agent --conf $FLUME_HOME/conf --conf-file?$FLUME_HOME/conf/exec-memory-logger.conf?--name a1 -Dflume.root.logger=INFO,console
這時候我們往/usr/test.log中添加點內容,看看是否能否被監控到
echo 你好嗎 >> test.log
echo yes >> test.log
echo 我非常好 >> test.log
我們把監控到的數據打印到控制臺,是沒有實際意義的,我們如果想把監控到的數據寫入到我們的hdfs上去,改怎么處理?
繼續查找flume的官方開發文檔:
既然是寫入到hdfs,我們就必須去設置agent的sink配置,來我們來找一下。
# Name the components on this agent
a1.sources=r1
a1.sinks=k1
a1.channels=c1
# Describe/configure the? source
a1.sources.r1.type=exec? ? ##shell命令類型的
a1.sources.r1.command=tail -F /usr/test.log ##綁定監控的日志文件
a1.sources.r1.shell=/bin/bash -c? ? ##固定填寫
# 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
###把source-channel-sink建立聯系
a1.sources.r1.channels=c1? ##一個agent的source可以指定多個channel,因此這里時channels
a1.sinks.k1.channel=c1? ? ? ?##而一個agent的sink只能對應一個channel。
a1.sinks.k1.hdfs.path = hdfs://flume/xcx/%y-%m-%d/%H%M/%S ###定義再hdfs的存儲目錄格式
a1.sinks.k1.hdfs.filePrefix = xcx-? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ###存儲文件前綴
a1.sinks.k1.hdfs.fileSuffix =.lzo????????????????????????????????????????????###存儲文件后綴
a1.sinks.k1.hdfs.codeC=lzo? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?###壓縮格式可選值gzip, bzip2, lzo, lzop, snappy
a1.sinks.k1.hdfs.writeFormat=Text? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ####存儲文件類型Text?or?Writable
a1.sinks.k1.hdfs.round = true? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?###是否根據時間間隔滾動產生文件
a1.sinks.k1.hdfs.roundValue = 10? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ###10
a1.sinks.k1.hdfs.roundUnit = minute? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?###單位? 可選值 second, minute or hour.
把這個配置內容保存在一個文件里面,存放在flume的根目錄conf文件夾下,我自己喜歡命名:
exec-memory-hdfs.conf
這里我有個疑問,flume是如何知道我的hdfs服務器位置的尼?拿到只用一個 a1.sinks.k1.hdfs.path = hdfs://flume/xcx/%y-%m-%d/%H%M/%S ###定義再hdfs的存儲目錄格式就可以做到了嗎?如果flume不跟hdfs部署在同一臺服務器,這種方式還有效嗎?
咱們就引出來了第三個需求:
我們的技術選型:這里的avro sink就是用來跨機器的傳輸格式
第一組:exec source + memory channel + avro sink
第二組:avro source + memory channel + logger sink
有幾組就要編寫幾個agent配置文件
第一組agent文件名:exec-memory-avro.conf
######begin######
# Name the components on this agent
exec-memory-avro.sources=exec-source
exec-memory-avro.sinks=avro-sink
exec-memory-avro.channels=memroy-channel
# Describe/configure the? source
exec-memory-avro.sources.exec-source.type=exec? ? ##shell命令類型的
exec-memory-avro.sources.exec-source.command=tail -F /usr/test.log ##綁定監控的日志文件
exec-memory-avro.sources.exec-source.shell=/bin/bash -c? ? ##固定填寫
exec-memory-avro.sinks.avro-sink.type=avro ##指定數據輸出形式
exec-memory-avro.sinks.avro-sink.hostname=192.168.32.129 ####數據需要寫入的目標服務器ip
exec-memory-avro.sinks.avro-sink.port=44444? ?####所在端口
exec-memory-avro.channels.memroy-channel.type=memory
exec-memory-avro.channels.memroy-channel.capacity=1000
exec-memory-avro.channels.memroy-channel.transactionCapacity=100
###把source-channel-sink建立聯系
exec-memory-avro.sources.exec-source.channels=memroy-channel ##一個agent的source可以指定多個channel,因此這里時channels
exec-memory-avro.sinks.avro-sink.channel=memroy-channel ##而一個agent的sink只能對應一個channel。
######end######
第二組agent文件名:avro-memory-logger.conf
######begin######
# Name the components on this agent
avro-memory-logger.sources=avro-source
avro-memory-logger.sinks=logger-sink
avro-memory-logger.channels=memroy-channel
# Describe/configure the? source
avro-memory-logger.sources.avro-source.type=avro?
avro-memory-logger.sources.avro-source.bind=192.168.32.72
avro-memory-logger.sources.avro-source.port=44444
avro-memory-logger.sinks.logger-sink.type=logger ##指定數據輸出形式
avro-memory-logger.channels.memroy-channel.type=memory
avro-memory-logger.channels.memroy-channel.capacity=1000
avro-memory-logger.channels.memroy-channel.transactionCapacity=100
###把source-channel-sink建立聯系
avro-memory-logger.sources.avro-source.channels=memroy-channel?##一個agent的source可以指定多個channel,因此這里時channels
avro-memory-logger.sinks.logger-sink.channel=memroy-channel?##而一個agent的sink只能對應一個channel。
######end######
啟動Flume
這里要由于有兩個agent,所以要有個啟動順序
需要先啟動:avro-memory-logger
bin/flume-ng agent --conf $FLUME_HOME/conf --conf-file?$FLUME_HOME/conf/avro-memory-logger.conf?--name avro-memory-logger -Dflume.root.logger=INFO,console
然后再啟動:exec-memory-avro
bin/flume-ng agent --conf $FLUME_HOME/conf --conf-file?$FLUME_HOME/conf/exec-memory-avro.conf?--name?exec-memory-avro?-Dflume.root.logger=INFO,console
關閉flume
ps -ef|grep flume,查出進程號,然后kill pid即可
注意:改動flume的配置之后,重啟flume,重啟前未被發出來的消息還依然會重發。