Flume從入門到上手

第 1 章 Flume 概述 

      1.1 Flume 定義 :

Flume 是 Cloudera 提供的一個高可用的,高可靠的,分佈式的海量日誌採集、聚合和傳輸的系統。Flume 基於流式架構,靈活簡單。

 

1.2 Flume架構

 

1.2.1 Agent 
        Agent 是一個 JVM 進程,它以事件的形式將數據從源頭送至目的。 
        Agent 主要有 3 個部分組成,Source、Channel、Sink。 
1.2.2 Source 
       Source 是負責接收數據到 Flume Agent 的組件。Source 組件可以處理各種類型、各種
       格式的日誌數據,包括 avro、thrift、exec、jms、spooling directory、netcat、sequence 
       generator、syslog、http、legacy。 
1.2.3 Sink 
        Sink 不斷地輪詢 Channel 中的事件且批量地移除它們,並將這些事件批量寫入到存儲
        或索引系統、或者被髮送到另一個 Flume Agent。Sink 組件目的地包括 hdfs、logger、avro、thrift、ipc、file、HBase、              solr、自定義。 
1.2.4 Channel 
         Channel 是位於 Source 和 Sink 之間的緩衝區。因此,Channel 允許 Source 和 Sink 運
         作在不同的速率上。Channel 是線程安全的,可以同時處理幾個 Source 的寫入操作和幾個
         Sink 的讀取操作。 
         Flume 自帶兩種 Channel:Memory Channel 和 File Channel 以及 Kafka Channel。 
         Memory Channel 是內存中的隊列。Memory Channel 在不需要關心數據丟失的情景下適
        用。如果需要關心數據丟失,那麼 Memory Channel 就不應該使用,因爲程序死亡、機器宕
        機或者重啓都會導致數據丟失。 
        File Channel 將所有事件寫到磁盤。因此在程序關閉或機器宕機的情況下不會丟失數
       據。 
1.2.5 Event 
     傳輸單元,Flume 數據傳輸的基本單元,以 Event 的形式將數據從源頭送至目的地。
     Event 由 Header 和 Body 兩部分組成,Header 用來存放該 event 的一些屬性,爲 K-V 結構,
     Body 用來存放該條數據,形式爲字節數組。 

 

第 2 章 Flume 安裝與部署
     2.1  Flume官網 可以看看,文檔寫的簡單明瞭

          官網 :flume.apache.org  用戶文檔 版本1.8 : http://flume.apache.org/releases/content/1.8.0/FlumeUserGuide.html

    2.2  下載安裝

    linux上直接使用  wget http://archive.apache.org/dist/flume/1.8.0/apache-flume-1.8.0-bin.tar.gz 下載,解壓,安裝

           基本不用配置,jdk必須要1.8+,其他內存,磁盤,文件權限都應該沒啥問題,遇到問題自行修改就好了.

第一步 :解壓:tar -zxvf apache-flume-1.8.0-bin.tar.gz

第二步:  重命名 :mv apache-flume-1.8.0-bin flume-1.8.0

3.Flume入門例子

  3.1 使用Flume監聽本地一個端口,收集端口輸入的數據,打印到控制檯

         我們進入Flume的conf目錄下,編輯一個配置文件,自定命名  example1.conf

# example.conf: A single-node Flume configuration

# Name the components on this agent
a1代表agent,一個agent由source,channel,sink組成
a1.sources = r1                   #source的名稱
a1.sinks = k1                     #sink的名稱
a1.channels = c1                  #channel的名稱

# Describe/configure the source  配置source組件
a1.sources.r1.type = netcat           #source類型  
a1.sources.r1.bind = 192.168.1.4      #綁定的ip
a1.sources.r1.port = 16888            #綁定的端口

# Describe the sink                  配置sink組件
a1.sinks.k1.type = logger           #輸出日誌是到控制檯
 
# Use a channel which buffers events in memory     #配置channel管道,使用的是內存
a1.channels.c1.type = memory                   #管道收集數據的類型
a1.channels.c1.capacity = 1000                 #管道容量大小,一個代表一個event
a1.channels.c1.transactionCapacity = 100       #表示收到100Event再去提交事務

 #配置完需要把流程組件綁定起來,就是告訴source收集的數據給誰,sink找誰要數據
# Bind the source and sink to the channel           
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

配置完成後,我們保存退出,然後啓動一個本地端口.

我們先檢測本地有沒有要啓動端口

檢查端口並沒喲被使用.然後我們啓動一個本地端口

很明顯,我們沒有安裝nc組件 

yum install nc 安裝一下,不是root用戶需要授權一下

 bin/flume-ng  agent --conf conf/ --conf-file example1.conf --name a1 -Dflume.root.logger=INFO,console

參數說明: 
 --conf :表示配置文件存儲在 conf/目錄 
 --name:表示給 agent 起名爲 a1 
 --conf-file:flume 本次啓動讀取的配置文件是在 conf文件夾下的example1.conf文件。 
 -Dflume.root.logger=INFO,console :-D 表示 flume 運行時動態修改 flume.root.logger
參數屬性值,並將控制檯日誌打印級別設置爲 INFO 級別。日誌級別包括:log、info、warn、
error。 

然後我們輸入數據

 

 

至此,我們第一個例子完成了。

 

 

3.2 監控一個文本,實時監測動態發生的數據變化

    我們到Flume的conf下,複製一個conf,命名爲example2.log


# 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                 只修改這個source
a1.sources.r1.type = exec                 #(Exec source在啓動時運行一個給定的Unix命令,並期望該進程在標準輸出上不斷地生成數據(除非將屬性logStdErr設置爲true,否則將直接丟棄stderr)。如果進程出於任何原因退出,源也將退出,並且不會產生進一步的數據。這意味着像cat [named pipe]或tail - f [file]這樣的配置將產生預期的結果,而as date可能不會產生這種結果——前兩個命令將產生數據流,而後者將產生單個事件並退出。)                                         
a1.sources.r1.command = tail -F /usr/local/dynamic.log   #動態查看一個文本


# 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
其他的配置不動,然後我們啓動Flume,上次的Flume已經被我kill掉了,

沒有kill 的 ,使用 jps -ml監測flume進程  然後kill

在我們指定的路徑下創建文件,然後寫入數據

 

然後我們的入門案例到此結束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章