Flume的簡單介紹
Flume是一個分布式、可靠、和高可用的海量日志采集、聚合和傳輸的系統。
Flume可以采集文件,socket數據包等各種形式源數據,又可以將采集到的數據輸出到HDFS、hbase、hive、kafka等眾多外部存儲系統中
Flume的運行機制
1、Flume分布式系統中最核心的角色是agent,flume采集系統就是由一個個agent所連接起來形成
2、每一個agent相當于一個數據傳遞員Source 到 Channel 到 Sink之間傳遞數據的形式是Event事件;Event事件是一個數據流單元。
內部有三個組件:
a)Source:采集源,用于跟數據源對接,以獲取數據
b)Sink:下沉地,采集數據的傳送目的,用于往下一級agent傳遞數據或者往最終存儲系統傳遞數據
c)Channel:angent內部的數據傳輸通道,用于從source將數據傳遞到sink
單個agent采集數據
多個agent直接串聯采集數據
Flume的安裝與使用
安裝Flume之前確保安裝了hadoop,假設是安裝了hadoop的前提下,介紹Flume的安裝,其實解壓下就行了。
我這里使用的是apache-flume-1.6.0-bin.tar.gz,上傳到linux,解壓到指定包即可,我這里解壓到了apps這個包,里面放了我解壓的hadoo、hive和zookeeper,安裝就完了。
注:其實在conf目錄里面需要配置JAVA_HOME,但是我沒配置使用也沒問題的。如果出現問題了再配置看。
Flume的使用(一)
這里打算做的是,接收網絡傳輸的數據。也就是flume(安裝在mini1)的作用是,然后在mini2這臺機器上,發送數據,mini1上能采集到,可以下沉到hdfs(為了方便,這里暫時打印在控制臺)
注:為了方便我這里就在mini1這條機器打開兩個窗口來進行發送和采集數據了。
進入到flume的conf目錄下,創建文件,進行配置
[root@mini1 ~]# cd apps/apache-flume-1.6.0-bin/conf/
[root@mini1 conf]# ll
總用量 28
-rw-r--r--. 1 501 games 1661 5月 9 2015 flume-conf.properties.template
-rw-r--r--. 1 501 games 1110 5月 9 2015 flume-env.ps1.template
-rw-r--r--. 1 501 games 1214 5月 9 2015 flume-env.sh.template
-rw-r--r--. 1 501 games 3107 5月 9 2015 log4j.properties
-rw-r--r--. 1 root root 487 10月 19 14:34 netcat-logger.conf
-rw-r--r--. 1 root root 507 10月 19 01:57 spool-logger.conf
-rw-r--r--. 1 root root 1271 10月 19 15:11 tail-hdfs.conf
[root@mini1 conf]# vi netcat-logger.conf
# example.conf: A single-node Flume configuration
# Name the components on this agent
#給那三個組件取個名字
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
#類型, 從網絡端口接收數據,本機mini1, type=spoolDir采集目錄源,目錄里有就采
a1.sources.r1.type = netcat
a1.sources.r1.bind = mini1
a1.sources.r1.port = 44444
# Describe the sink 日志下沉到log4j,打印在屏幕上
a1.sinks.k1.type = logger
# Use a channel which buffers events in memory
#下沉的時候是一批一批的, 下沉的時候是一個個event
Channel參數解釋:
#capacity:默認該通道中最大的可以存儲的event數量 1000條數據(1000個event,source拿到的數據是封裝成event事件的)
#trasactionCapacity:每次最大可以從source中拿到或者送到sink中的event數量
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
配置好了就可以啟動了
[root@mini1 apache-flume-1.6.0-bin]# bin/flume-ng agent --conf conf --conf-file conf/netcat-logger.conf --name a1 -Dflume.root.logger=INFO,console
Warning: JAVA_HOME is not set!
...
2017-10-20 05:00:13,317 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:173)] Starting Sink k1
2017-10-20 05:00:13,318 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:184)] Starting Source r1
2017-10-20 05:00:13,320 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:150)] Source starting
2017-10-20 05:00:13,350 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:164)] Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/192.168.25.127:44444]
在mini1上重開一個窗口(或者其它機器),發送數據進行測試
[root@mini1 ~]# telnet mini1 44444
Trying 192.168.25.127...
Connected to mini1.
Escape character is '^]'.
jinbingmin
OK
haha
OK
oyasumi
OK
發送了三句話。
注:如果沒有telnet命令,做法如下
rpm -qa telnet-server 查看有沒有安裝,沒有輸出的話,那么執行
yum install telnet-server
rpm -qa telnet 來查看telnet-server 安裝包有沒有安裝,如果沒有輸出,那么執行安裝
yum install telnet
退出的話使用ctrl+],接著quit
再次查看服務端有沒有采集到
2017-10-20 05:00:59,699 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 6A 69 6E 62 69 6E 67 6D 69 6E 0D jinbingmin. }
2017-10-20 05:01:14,704 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 68 61 68 61 0D haha. }
2017-10-20 05:01:19,421 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 6F 79 61 73 75 6D 69 0D oyasumi. }
發現已經采集到了打印到了控制臺
使用Flume監聽日志變化
進入到flume的conf目錄下,創建文件,進行配置
[root@mini1 ~]# cd apps/apache-flume-1.6.0-bin/conf/
[root@mini1 conf]# ll
總用量 28
-rw-r--r--. 1 501 games 1661 5月 9 2015 flume-conf.properties.template
-rw-r--r--. 1 501 games 1110 5月 9 2015 flume-env.ps1.template
-rw-r--r--. 1 501 games 1214 5月 9 2015 flume-env.sh.template
-rw-r--r--. 1 501 games 3107 5月 9 2015 log4j.properties
-rw-r--r--. 1 root root 487 10月 19 14:34 netcat-logger.conf
-rw-r--r--. 1 root root 507 10月 19 01:57 spool-logger.conf
-rw-r--r--. 1 root root 1271 10月 19 15:11 tail-hdfs.conf
[root@mini1 conf]# vi netcat-logger.conf
# example.conf: A single-node Flume configuration
# Name the components on this agent
#給那三個組件取個名字
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
## exec表示flume回去調用給的命令,然后從給的命令的結果中去拿數據
a1.sources.r1.type = exec
## 使用tail這個命令來讀數據
a1.sources.r1.command = tail -F /opt/test.log
a1.sources.r1.channels = c1
# Describe the sink 日志下沉到log4j,打印在屏幕上
a1.sinks.k1.type = logger
# Use a channel which buffers events in memory
#下沉的時候是一批一批的, 下沉的時候是一個個event
Channel參數解釋:
#capacity:默認該通道中最大的可以存儲的event數量 1000條數據(1000個event,source拿到的數據是封裝成event事件的)
#trasactionCapacity:每次最大可以從source中拿到或者送到sink中的event數量
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
配置好了就可以啟動了
[root@mini1 apache-flume-1.6.0-bin]# bin/flume-ng agent --conf conf --conf-file conf/netcat-logger.conf --name a1 -Dflume.root.logger=INFO,console
Warning: JAVA_HOME is not set!
...
2017-10-20 05:00:13,317 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:173)] Starting Sink k1
2017-10-20 05:00:13,318 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:184)] Starting Source r1
2017-10-20 05:00:13,320 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:150)] Source starting
2017-10-20 05:00:13,350 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:164)] Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/192.168.25.127:44444]
通過寫一個死循環往test.log中寫數據的方式模式日志文件增長
編寫shell腳本,模擬日志增長變化。
[root@hadoop1 flumedata]# cd /home/tuzq/software/flumedata
[root@hadoop1 flumedata]# while true
>do
> date >> test.log
> sleep 2
> done
查看日志變化
[root@hadoop1 ~]# cd /home/tuzq/software/flumedata/
[root@hadoop1 flumedata]# ls
access.log error.log test.log
[root@hadoop1 flumedata]# tail -f test.log
2017年 06月 13日 星期二 22:02:22 CST
2017年 06月 13日 星期二 22:02:24 CST
2017年 06月 13日 星期二 22:02:26 CST
2017年 06月 13日 星期二 22:02:28 CST
2017年 06月 13日 星期二 22:02:30 CST
2017年 06月 13日 星期二 22:02:32 CST
通過上面的文件,可以看到test.log在不停的追加數據。
Flume的使用(三)
采集數據到hdfs
這里要添加的配置文件里面的采集源和下沉地就都有變化了。
[root@mini1 conf]# vi tail-hdfs.conf
# 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 /opt/test.log
a1.sources.r1.channels = c1
# Describe the sink
#下沉目標
a1.sinks.k1.type=hdfs
a1.sinks.k1.channel=c1
a1.sinks.k1.hdfs.useLocalTimeStamp=true
a1.sinks.k1.hdfs.path=/flume/testout9/
a1.sinks.k1.hdfs.filePrefix=cmcc
a1.sinks.k1.hdfs.minBlockReplicas=1
a1.sinks.k1.hdfs.fileType=DataStream
a1.sinks.k1.hdfs.writeFormat=Text
a1.sinks.k1.hdfs.rollInterval=0
a1.sinks.k1.hdfs.rollSize=10240
a1.sinks.k1.hdfs.rollCount=0
a1.sinks.k1.hdfs.idleTimeout=0
# 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
flume+kafka
spooldir.sources= eventDir
spooldir.channels= memoryChannel
spooldir.sinks= eventHDFS
spooldir.channels.memoryChannel.type= memory
spooldir.channels.memoryChannel.capacity= 10000
spooldir.channels.memoryChannel.transactioncapacity= 1000000
spooldir.sources.eventDir.type=exec
spooldir.sources.eventDir.command=tail -F /opt/log.txt
spooldir.sinks.eventHDFS.type = org.apache.flume.sink.kafka.KafkaSink
spooldir.sinks.eventHDFS.topic = orderMq
spooldir.sinks.eventHDFS.brokerList = hadoop01:9092,hadoop02:9092,hadoop03:9092
spooldir.sources.eventDir.channels= memoryChannel
spooldir.sinks.eventHDFS.channel= memoryChannel
參考
https://blog.csdn.net/zengmingen/article/details/65444823