flume的安裝和使用

Flume的簡單介紹
Flume是一個分布式、可靠、和高可用的海量日志采集、聚合和傳輸?shù)南到y(tǒng)。
Flume可以采集文件,socket數(shù)據(jù)包等各種形式源數(shù)據(jù),又可以將采集到的數(shù)據(jù)輸出到HDFS、hbase、hive、kafka等眾多外部存儲系統(tǒng)中
Flume的運(yùn)行機(jī)制
1、Flume分布式系統(tǒng)中最核心的角色是agent,flume采集系統(tǒng)就是由一個個agent所連接起來形成
2、每一個agent相當(dāng)于一個數(shù)據(jù)傳遞員Source 到 Channel 到 Sink之間傳遞數(shù)據(jù)的形式是Event事件;Event事件是一個數(shù)據(jù)流單元。

內(nèi)部有三個組件:
a)Source:采集源,用于跟數(shù)據(jù)源對接,以獲取數(shù)據(jù)
b)Sink:下沉地,采集數(shù)據(jù)的傳送目的,用于往下一級agent傳遞數(shù)據(jù)或者往最終存儲系統(tǒng)傳遞數(shù)據(jù)
c)Channel:angent內(nèi)部的數(shù)據(jù)傳輸通道,用于從source將數(shù)據(jù)傳遞到sink
單個agent采集數(shù)據(jù)

image

多個agent直接串聯(lián)采集數(shù)據(jù)

image

Flume的安裝與使用
安裝Flume之前確保安裝了hadoop,假設(shè)是安裝了hadoop的前提下,介紹Flume的安裝,其實(shí)解壓下就行了。
我這里使用的是apache-flume-1.6.0-bin.tar.gz,上傳到linux,解壓到指定包即可,我這里解壓到了apps這個包,里面放了我解壓的hadoo、hive和zookeeper,安裝就完了。
注:其實(shí)在conf目錄里面需要配置JAVA_HOME,但是我沒配置使用也沒問題的。如果出現(xiàn)問題了再配置看。

3臺電腦安裝netcat

yum install -y nc.x86_64

hadoop03在9999端口監(jiān)聽

nc -l 9999

hadoop05連接hadoop03,發(fā)送數(shù)據(jù)

nc hadoop03 9999

Flume的使用(一)
這里打算做的是,接收網(wǎng)絡(luò)傳輸?shù)臄?shù)據(jù)。也就是flume(安裝在mini1)的作用是,然后在mini2這臺機(jī)器上,發(fā)送數(shù)據(jù),mini1上能采集到,可以下沉到hdfs(為了方便,這里暫時打印在控制臺)
注:為了方便我這里就在mini1這條機(jī)器打開兩個窗口來進(jìn)行發(fā)送和采集數(shù)據(jù)了。
進(jìn)入到flume的conf目錄下,創(chuàng)建文件,進(jìn)行配置

[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
#類型, 從網(wǎng)絡(luò)端口接收數(shù)據(jù),本機(jī)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參數(shù)解釋:
#capacity:默認(rèn)該通道中最大的可以存儲的event數(shù)量 1000條數(shù)據(jù)(1000個event,source拿到的數(shù)據(jù)是封裝成event事件的)
#trasactionCapacity:每次最大可以從source中拿到或者送到sink中的event數(shù)量
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]

flume內(nèi)部啟動netcat,監(jiān)聽44444端口,收到數(shù)據(jù),顯示到終端控制臺

在mini1上重開一個窗口(或者其它機(jī)器),發(fā)送數(shù)據(jù)進(jìn)行測試

[root@mini1 ~]# telnet mini1 44444
Trying 192.168.25.127...
Connected to mini1.
Escape character is '^]'.
jinbingmin
OK
haha
OK
oyasumi
OK

發(fā)送了三句話。
注:如果沒有telnet命令,做法如下

rpm -qa telnet-server 查看有沒有安裝,沒有輸出的話,那么執(zhí)行
yum install telnet-server  
rpm -qa telnet 來查看telnet-server 安裝包有沒有安裝,如果沒有輸出,那么執(zhí)行安裝
yum install telnet
退出的話使用ctrl+],接著quit

再次查看服務(wù)端有沒有采集到

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. }

發(fā)現(xiàn)已經(jīng)采集到了打印到了控制臺

使用Flume監(jiān)聽日志變化

進(jìn)入到flume的conf目錄下,創(chuàng)建文件,進(jìn)行配置

[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回去調(diào)用給的命令,然后從給的命令的結(jié)果中去拿數(shù)據(jù)
a1.sources.r1.type = exec
## 使用tail這個命令來讀數(shù)據(jù)
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參數(shù)解釋:
#capacity:默認(rèn)該通道中最大的可以存儲的event數(shù)量 1000條數(shù)據(jù)(1000個event,source拿到的數(shù)據(jù)是封裝成event事件的)
#trasactionCapacity:每次最大可以從source中拿到或者送到sink中的event數(shù)量
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]

通過寫一個死循環(huán)往test.log中寫數(shù)據(jù)的方式模式日志文件增長

編寫shell腳本,模擬日志增長變化。

#!/bin/bash
[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在不停的追加數(shù)據(jù)。

Flume的使用(三)

采集數(shù)據(jù)到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根據(jù)文件名追中, f根據(jù)文件的nodeid追中
a1.sources.r1.command = tail -F /opt/test.log
a1.sources.r1.channels = c1
# Describe the sink
#下沉目標(biāo)
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

https://blog.csdn.net/HG_Harvey/article/details/78358304

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 博客原文 翻譯作品,水平有限,如有錯誤,煩請留言指正。原文請見 官網(wǎng)英文文檔 引言 概述 Apache Flume...
    rabbitGYK閱讀 11,710評論 13 34
  • title: Flume構(gòu)建日志采集系統(tǒng)date: 2018-02-03 19:45tags: [flume,k...
    溯水心生閱讀 16,286評論 3 25
  • 面對以上的問題,我們?nèi)绾螌⑦@些日志移動到hdfs集群上尼???? 第一種方案:使用shell腳本cp 文件,然后通...
    機(jī)靈鬼鬼閱讀 1,520評論 1 1
  • 介紹 概述 Apache Flume是為有效收集聚合和移動大量來自不同源到中心數(shù)據(jù)存儲而設(shè)計(jì)的可分布,可靠的,可用...
    ximengchj閱讀 3,676評論 0 13
  • Flume的簡單介紹Flume是一個分布式、可靠、和高可用的海量日志采集、聚合和傳輸?shù)南到y(tǒng)。Flume可以采集文件...
    __元昊__閱讀 614評論 0 0

友情鏈接更多精彩內(nèi)容