flume應(yīng)用案例

flume應(yīng)用案例

1 flume用法

flume的使用非常簡(jiǎn)單,只需書寫一個(gè)配置文件,在配置文件中描述source,channel和sink的具體實(shí)現(xiàn),然后運(yùn)行一個(gè)agent的實(shí)例赊窥,在運(yùn)行agent實(shí)例的過程中會(huì)讀取配置文件中的內(nèi)容,這樣flume就會(huì)采集數(shù)據(jù)了狸页。

配置文件編寫規(guī)則

1 整體描述agent中的sources、sink芍耘、channel:

#其中a1為agent的名字腹侣,r1為source名稱,k1為sink名稱齿穗,c1為channel名稱
a1.sources = r1
a1.sinks = k1
a1.channels = c1

2 agent中的source傲隶、sink和channel的具體實(shí)現(xiàn)

需要指定source的類型,即source是接收文件還是接受http的還是接受thrift的窃页;對(duì)于sink同理跺株,需要指定結(jié)果是輸出到hdfs還是和base上等复濒。對(duì)于channel需要指定使用內(nèi)存還是文件進(jìn)行緩存。

#描述和配置source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

#描述和配置sink
a1.sinks.k1.type = logger

#描述和配置channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

3 通過channel將source與sink連接起來

#通過channel將source與sink連接起來
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

4 在shell中啟動(dòng)agent
$ bin/flume-ng agent -n $agent_name -c conf -f conf/flume-conf.properties.template -Dflume.root.logger=DEBUG,console

參數(shù)說明
-n : 制定agent的名稱(與配置文件中的agent名字一樣)
-c : 指定了flume中配置文件的目錄
-f : 制定配置文件
-Dflume.root.logger=DEBUG,console : 設(shè)置日志等級(jí)

2 案例一

案例描述

監(jiān)聽一個(gè)指定的網(wǎng)絡(luò)端口乒省,即只要應(yīng)用程序向這個(gè)端口寫入數(shù)據(jù)的時(shí)候巧颈,這個(gè)source組建就可以獲取到信息。三個(gè)組建的類型如下:

source  : NetCat
sink    : logger
channel : memory

配置文件

# 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 = 192.168.32.130
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
a1.channels.c1.capacity = 1000
a1.channels.c1.transctionCapacity = 100

#Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

啟動(dòng)flume agent a1服務(wù)端

$flume-ng agent -n a1 -c ../conf -f ../conf/netcat.conf -Dflume.root.logger=DEBUG,console

在另一個(gè)終端下使用netCat發(fā)送數(shù)據(jù)

$nc localhost 44444
然后在下面隨意輸入內(nèi)容進(jìn)行測(cè)試袖扛。

3 案例二

案例描述

監(jiān)聽一個(gè)指定的網(wǎng)絡(luò)端口砸泛,然后將數(shù)據(jù)寫入hdfs上。三個(gè)組建的類型如下:

source  : NetCat
sink    : hdfs
channel : file

配置文件

# 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 = 192.168.32.130
a1.sources.r1.port = 44444

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop200:9000/dataoutput
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.rollInterval = 10
a1.sinks.k1.hdfs.rollSize = 0
a1.sinks.k1.hdfs..rollCount = 0
a1.sinks.k1.hdfs.filePrefix = %Y-%m-%d-%H-%M-%S
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in file
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /usr/flume/checkpoint
a1.channels.c1.dataDirs = /usr/flume/data

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

啟動(dòng)flume agent a1服務(wù)端

$flume-ng agent -n a1 -c ../conf -f ../conf/netcat.conf -Dflume.root.logger=DEBUG,console

在另一個(gè)終端下使用netCat發(fā)送數(shù)據(jù)

$nc localhost 44444
然后在下面隨意輸入內(nèi)容進(jìn)行測(cè)試蛆封,測(cè)試結(jié)果如下圖:

4 案例三

案例描述

監(jiān)聽一個(gè)指定的目錄唇礁,只要應(yīng)用程序向這個(gè)目錄中添加新的文件,source組件就可以獲取到消息惨篱,并解析該文件的內(nèi)容盏筐,然后寫入到channel。寫入完成后砸讳,標(biāo)記該文件已完成或者刪除該文件琢融。三個(gè)組建的類型如下:

source  : Spooling Directory
sink    : logger
channel : memory

Spooling Directory Source注意事項(xiàng):

1.正在拷貝的文件不能再進(jìn)行編輯,否則flume就會(huì)在log中報(bào)錯(cuò)并停掉進(jìn)程簿寂。
2.不能將具有相同文件名的文件拷貝到這個(gè)目錄漾抬,否則flume就會(huì)在log中報(bào)錯(cuò)并停掉進(jìn)程。

配置文件

# 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 = /usr/local/datainput
a1.sources.r1.fileHeader = true
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp

# 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
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

啟動(dòng)flume agent a1服務(wù)端

$flume-ng agent -n a1 -c ../conf -f ../conf/netcat.conf -Dflume.root.logger=DEBUG,console

測(cè)試

向指定的文件夾/usr/local/datainput添加數(shù)據(jù)觀察控制臺(tái)的變化以及該目錄文件的變化常遂。

5 案例四

案例描述

監(jiān)聽一個(gè)指定的目錄纳令,只要應(yīng)用程序向這個(gè)目錄中添加新的文件,source組件就可以獲取到消息烈钞,并解析該文件的內(nèi)容泊碑,然后寫入到channel坤按。寫入完成后毯欣,標(biāo)記該文件已完成或者刪除該文件。三個(gè)組建的類型如下:

source  : Spooling Directory
sink    : hdfs
channel : file

配置文件

# 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 = /usr/local/datainput
a1.sources.r1.fileHeader = true
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop200:8020/dataoutput
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.roolInterval = 10
a1.sinks.k1.hdfs.roolSize = 0
a1.sinks.k1.hdfs.roolCount = 0
a1.sinks.k1.hdfs.filePrefix = %Y-%m-%d-%H-%M-%S
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers in file
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /usr/flume/checkpoint
a1.channels.c1.dataDirs = /usr/flume/data

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

啟動(dòng)flume agent a1服務(wù)端

$flume-ng agent -n a1 -c ../conf -f ../conf/netcat.conf -Dflume.root.logger=DEBUG,console

測(cè)試

向指定的文件夾/usr/local/datainput添加數(shù)據(jù)臭脓,然后通過web觀察hdfs的變化以及該目錄文件的變化酗钞。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市来累,隨后出現(xiàn)的幾起案子砚作,更是在濱河造成了極大的恐慌,老刑警劉巖嘹锁,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件葫录,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡领猾,警方通過查閱死者的電腦和手機(jī)米同,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門骇扇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人面粮,你說我怎么就攤上這事少孝。” “怎么了熬苍?”我有些...
    開封第一講書人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵稍走,是天一觀的道長。 經(jīng)常有香客問我柴底,道長婿脸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任似枕,我火速辦了婚禮盖淡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凿歼。我一直安慰自己褪迟,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開白布答憔。 她就那樣靜靜地躺著味赃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪虐拓。 梳的紋絲不亂的頭發(fā)上心俗,一...
    開封第一講書人閱讀 51,698評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音蓉驹,去河邊找鬼城榛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛态兴,可吹牛的內(nèi)容都是我干的狠持。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼瞻润,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼喘垂!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起绍撞,我...
    開封第一講書人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤正勒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后傻铣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體章贞,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年非洲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鸭限。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片就谜。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖里覆,靈堂內(nèi)的尸體忽然破棺而出丧荐,到底是詐尸還是另有隱情,我是刑警寧澤喧枷,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布虹统,位于F島的核電站,受9級(jí)特大地震影響隧甚,放射性物質(zhì)發(fā)生泄漏车荔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一戚扳、第九天 我趴在偏房一處隱蔽的房頂上張望忧便。 院中可真熱鬧,春花似錦帽借、人聲如沸珠增。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蒂教。三九已至,卻和暖如春脆荷,著一層夾襖步出監(jiān)牢的瞬間凝垛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來泰國打工蜓谋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留梦皮,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓桃焕,卻偏偏與公主長得像剑肯,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子覆旭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 博客原文 翻譯作品退子,水平有限岖妄,如有錯(cuò)誤型将,煩請(qǐng)留言指正。原文請(qǐng)見 官網(wǎng)英文文檔 引言 概述 Apache Flume...
    rabbitGYK閱讀 11,469評(píng)論 13 34
  • Flume的官網(wǎng)地址:http://flume.apache.org/FlumeUserGuide.html#ex...
    24格的世界閱讀 906評(píng)論 0 1
  • 介紹 概述 Apache Flume是為有效收集聚合和移動(dòng)大量來自不同源到中心數(shù)據(jù)存儲(chǔ)而設(shè)計(jì)的可分布荐虐,可靠的七兜,可用...
    ximengchj閱讀 3,525評(píng)論 0 13
  • Linux-Server-Notes PMS /home/softwareluke/圖片/2017-09-11 0...
    燕京博士閱讀 574評(píng)論 0 1
  • 1. Flume簡(jiǎn)介 Apache Flume是一個(gè)分布式的、可靠的福扬、可用的腕铸,從多種不同的源收集惜犀、聚集、移動(dòng)大量日...
    奉先閱讀 4,490評(píng)論 2 5