1. 程式人生 > >Flume系列二之案例實戰

Flume系列二之案例實戰

Flume案例實戰

寫在前面 通過前面一篇文章http://blog.csdn.net/liuge36/article/details/78589505的介紹我們已經知道flume到底是什麼?flume可以用來做什麼?但是,具體怎麼做,這就是我們這篇文章想要介紹的。話不多說,直接來案例學習。

實戰一:實現官網的第一個簡單的小案例-從指定埠採集資料輸出到控制檯

如何開始呢? 看官網!!!! 地址:http://flume.apache.org/FlumeUserGuide.html#flume-sources

從官網的介紹中,我們知道需要new一個.conf檔案, 1.這裡我們就在flume的conf資料夾下新建一個test1.conf

2.把官網的A simple example拷貝進去,做簡單的修改

[[email protected] conf]$ vim test1.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 = hadoop000
 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

#不修改也是應該沒有什麼問題的
#:wq儲存退出

3.flume的agent啟起來之後,就可以開始測試啦:

[[email protected] data]$ telnet hadoop000 44444
Trying 192.168.1.57...
Connected to hadoop000.
Escape character is '^]'.
你好         
OK

這裡會發現,剛剛啟動的agent介面有輸出

這裡寫圖片描述

到這裡,就實現第一個簡單的flume案例,很簡單是吧

可以看出,使用Flume的關鍵就是寫配置檔案 1) 配置Source 2) 配置Channel 3) 配置Sink 4) 把以上三個元件串起來

簡單來說,使用flume,就是使用flume的配置檔案

實戰二:監控一個檔案實時採集新增的資料輸出到控制檯

思路?? 前面說到,做flume就是寫配置檔案 就面臨選型的問題 Agent選型,即source選擇什麼,channel選擇什麼,sink選擇什麼

這裡我們選擇 exec source memory channel logger sink

怎麼寫呢? 按照之前說的那樣1234步驟

從官網中,我們可以找到我們的選型應該如何書寫: 1) 配置Source exec source

Property Name   Default Description
channels    –    
type    –   The component type name, needs to be exec
command –   The command to execute
shell   –   A shell invocation used to run the command. e.g. /bin/sh -c. Required only for commands relying on shell features like wildcards, back ticks, pipes etc.

從官網的介紹中,我們知道我們的exec source得配置type=exec ,配置自己的command,shell也是建議配置上的,其餘的配置就不用配置了。是不是很簡單。我們這裡自己的配置就如下:

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/hadoop/data/data.log
a1.sources.r1.shell = /bin/sh -c

2) 配置Channel memory channel 官網介紹的是:


Property Name   Default Description
type    –   The component type name, needs to be memory

對應著寫自己的Channel:

a1.channels.c1.type = memory

3) 配置Sink logger sink 官網介紹的是:


Property Name   Default Description
channel –    
type    –   The component type name, needs to be logger

對應著寫自己的Sink:

a1.sinks.k1.type = logger

4) 把以上三個元件串起來

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

按照1.2.3.4這個固定的套路寫任何的agent都是沒有問題的

1.我們new一個檔案叫做test2.conf 把我們自己的程式碼貼進去:

[[email protected] conf]$ vim test2.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/hadoop/data/data.log
a1.sources.r1.shell = /bin/sh -c

a1.sinks.k1.type = logger

a1.channels.c1.type = memory

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1 

#:wq儲存退出

2.開啟我們的agent

flume-ng agent \
--name a1  \
--conf $FLUME_HOME/conf  \
--conf-file $FLUME_HOME/conf/test2.conf \
-Dflume.root.logger=INFO,console

3.開始測試資料 這裡寫圖片描述

export 到這裡,我相信你一定學會如何去寫flume了。強調一下,官網是一個好的學習資源,一定不要浪費。這裡,我就先簡單介紹這麼兩個小的案例實戰,後面還會繼續更新更多flume的使用...一起加油