Flume篇---Flume安裝配置與相關使用

LHBlog發表於2018-01-17

一.前述

Copy過來一段介紹Apache Flume 是一個從可以收集例如日誌,事件等資料資源,並將這些數量龐大的資料從各項資料資源中集中起來儲存的工具/服務,或者數集中機制。flume具有高可用,分散式,配置工具,其設計的原理也是基於將資料流,如日誌資料從各種網站伺服器上彙集起來儲存到HDFS,HBase等集中儲存器中。官網:http://flume.apache.org/FlumeUserGuide.html

二.架構

1.基本架構

介紹:

Source:(相當於一個來源)

   從資料發生器接收資料,並將接收的資料以Flume的event格式傳遞給一個或者多個通道channal,Flume提供多種資料接收的方式,比如Avro,Thrift,twitter1%等

Channel:(相當於一箇中轉)

 channal是一種短暫的儲存容器,它將從source處接收到的event格式的資料快取起來,直到它們被sinks消費掉,它在source和sink間起著一共橋樑的作用,channal是一個完整的事務,這一點保證了資料在收發的時候的一致性. 並且它可以和任意數量的source和sink連結. 支援的型別有: JDBC channel , File System channel , Memort channel等.

sink:(相當於最後的寫出)

  sink將資料儲存到集中儲存器比如Hbase和HDFS,它從channals消費資料(events)並將其傳遞給目標地. 目標地可能是另一個sink,也可能HDFS,HBase.

2.延伸架構

  2.1利用AVRO中轉

2.2一般多個來源時可以配置這樣

ps:

Avro([ævrə])是Hadoop的一個子專案,由Hadoop的創始人Doug Cutting(也是Lucene,Nutch等專案的創始人)牽頭開發Avro是一個資料序列化系統,設計用於支援大批量資料交換的應用。它的主要特點有:支援二進位制序列化方式,可以便捷,快速地處理大量資料;動態語言友好,Avro提供的機制使動態語言可以方便地處理Avro資料。
三。具體實施
3.1 安裝
1、上傳
2、解壓
3、修改conf/flume-env.sh  檔案中的JDK目錄
 注意:JAVA_OPTS 配置  如果我們傳輸檔案過大 報記憶體溢位時 需要修改這個配置項
4、驗證安裝是否成功  ./flume-ng version
5、配置環境變數
    export FLUME_HOME=/home/apache-flume-1.6.0-bin


3.2 Source、Channel、Sink有哪些型別
    Flume Source
    Source型別                   | 說明
    Avro Source                 | 支援Avro協議(實際上是Avro RPC),內建支援
    Thrift Source               | 支援Thrift協議,內建支援
    Exec Source                 | 基於Unix的command在標準輸出上生產資料
    JMS Source                   | 從JMS系統(訊息、主題)中讀取資料
    Spooling Directory Source | 監控指定目錄內資料變更
    Twitter 1% firehose Source|    通過API持續下載Twitter資料,試驗性質
    Netcat Source               | 監控某個埠,將流經埠的每一個文字行資料作為Event輸入
    Sequence Generator Source | 序列生成器資料來源,生產序列資料
    Syslog Sources               | 讀取syslog資料,產生Event,支援UDP和TCP兩種協議
    HTTP Source                 | 基於HTTP POST或GET方式的資料來源,支援JSON、BLOB表示形式
    Legacy Sources               | 相容老的Flume OG中Source(0.9.x版本)

    Flume Channel
    Channel型別       說明
    Memory Channel                | Event資料儲存在記憶體中
    JDBC Channel                  | Event資料儲存在持久化儲存中,當前Flume Channel內建支援Derby
    File Channel                  | Event資料儲存在磁碟檔案中
    Spillable Memory Channel   | Event資料儲存在記憶體中和磁碟上,當記憶體佇列滿了,會持久化到磁碟檔案
    Pseudo Transaction Channel | 測試用途
    Custom Channel                | 自定義Channel實現

    Flume Sink
    Sink型別     說明
    HDFS Sink             | 資料寫入HDFS
    Logger Sink           | 資料寫入日誌檔案
    Avro Sink             | 資料被轉換成Avro Event,然後傳送到配置的RPC埠上
    Thrift Sink           | 資料被轉換成Thrift Event,然後傳送到配置的RPC埠上
    IRC Sink              | 資料在IRC上進行回放
    File Roll Sink         | 儲存資料到本地檔案系統
    Null Sink             | 丟棄到所有資料
    HBase Sink             | 資料寫入HBase資料庫
    Morphline Solr Sink | 資料傳送到Solr搜尋伺服器(叢集)
    ElasticSearch Sink     | 資料傳送到Elastic Search搜尋伺服器(叢集)
    Kite Dataset Sink     | 寫資料到Kite Dataset,試驗性質的
    Custom Sink           | 自定義Sink實現



案例1、 A simple example
    http://flume.apache.org/FlumeUserGuide.html#a-simple-example
    
    配置檔案
    ############################################################
    # 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 = localhost
    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.transactionCapacity = 100

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

啟動flume
flume-ng agent -n a1 -c conf -f simple.conf -Dflume.root.logger=INFO,console 指定配置目錄
flume-ng agent -n a1 -f op5 -Dflume.root.logger=INFO,console 不用指定配置目錄,將上訴source,channel,sink的檔案起名為a1,同時指定這個檔案在哪

安裝telnet
yum install telnet
退出 ctrl+]  quit

Memory Chanel 配置
  capacity:預設該通道中最大的可以儲存的event數量是100,
  trasactionCapacity:每次最大可以source中拿到或者送到sink中的event數量也是100
  keep-alive:event新增到通道中或者移出的允許時間
  byte**:即event的位元組量的限制,只包括eventbody



案例2、兩個flume做叢集(第一個agent的sink作為第二個agent的source)

    node01伺服器中,配置檔案
    ############################################################
    # 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 = node1
    a1.sources.r1.port = 44444

    # Describe the sink
    # a1.sinks.k1.type = logger
    a1.sinks.k1.type = avro
    a1.sinks.k1.hostname = node2
    a1.sinks.k1.port = 60000

    # 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
    ############################################################
    
    node02伺服器中,安裝Flume(步驟略)
    配置檔案
    ############################################################
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = avro
    a1.sources.r1.bind = node2
    a1.sources.r1.port = 60000

    # 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
    ############################################################
    
    先啟動node02的Flume
    flume-ng agent  -n a1 -c conf -f avro.conf -Dflume.root.logger=INFO,console
    
    再啟動node01的Flume
    flume-ng agent  -n a1 -c conf -f simple.conf2 -Dflume.root.logger=INFO,console
    
    開啟telnet 測試  node02控制檯輸出結果


案例3、Exec Source(監聽一個檔案)
        http://flume.apache.org/FlumeUserGuide.html#exec-source
        
    配置檔案
    ############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = exec
    a1.sources.r1.command = tail -F /home/flume.exec.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-ng agent -n a1 -c conf -f exec.conf -Dflume.root.logger=INFO,console
    
    建立空檔案演示 touch flume.exec.log
    迴圈新增資料
    for i in {1..50}; do echo "$i hi flume" >> flume.exec.log ; sleep 0.1; done
        
案例4、Spooling Directory Source(監聽一個目錄)
        http://flume.apache.org/FlumeUserGuide.html#spooling-directory-source
    配置檔案
    ############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /home/logs
    a1.sources.r1.fileHeader = true

    # 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-ng agent -n a1 -c conf -f spool.conf -Dflume.root.logger=INFO,console

    拷貝檔案演示
    mkdir logs
    cp flume.exec.log logs/


案例5、hdfs sink
        http://flume.apache.org/FlumeUserGuide.html#hdfs-sink
    
        配置檔案
    ############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /home/logs
    a1.sources.r1.fileHeader = true

    # Describe the sink
    ***只修改上一個spool sink的配置程式碼塊 a1.sinks.k1.type = logger
    a1.sinks.k1.type=hdfs
    a1.sinks.k1.hdfs.path=hdfs://sxt/flume/%Y-%m-%d/%H%M
    
    ##每隔60s或者檔案大小超過10M的時候產生新檔案
    # hdfs有多少條訊息時新建檔案,0不基於訊息個數
    a1.sinks.k1.hdfs.rollCount=0
    # hdfs建立多長時間新建檔案,0不基於時間
    a1.sinks.k1.hdfs.rollInterval=60
    # hdfs多大時新建檔案,0不基於檔案大小
    a1.sinks.k1.hdfs.rollSize=10240
    # 當目前被開啟的臨時檔案在該引數指定的時間(秒)內,沒有任何資料寫入,則將該臨時檔案關閉並重新命名成目標檔案
    a1.sinks.k1.hdfs.idleTimeout=3
    
    a1.sinks.k1.hdfs.fileType=DataStream
   #時間引數一定要帶上 true
    a1.sinks.k1.hdfs.useLocalTimeStamp=true
    
    ## 每五分鐘生成一目錄:
    # 是否啟用時間上的”捨棄”,這裡的”捨棄”,類似於”四捨五入”,後面再介紹。如果啟用,則會影響除了%t的其他所有時間表示式
    a1.sinks.k1.hdfs.round=true
    # 時間上進行“捨棄”的值;
    a1.sinks.k1.hdfs.roundValue=5
    # 時間上進行”捨棄”的單位,包含:second,minute,hour
    a1.sinks.k1.hdfs.roundUnit=minute

    # 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(將source,channel,sink關聯)
    ############################################################
    建立HDFS目錄
    hadoop fs -mkdir /flume
    
    啟動Flume
    flume-ng agent -n a1 -c conf -f hdfs.conf -Dflume.root.logger=INFO,console

    檢視hdfs檔案
    hadoop fs -ls /flume/...
    hadoop fs -get /flume/...


http://flume.apache.org/

安裝
1、上傳
2、解壓
3、修改conf/flume-env.sh  檔案中的JDK目錄
 注意:JAVA_OPTS 配置  如果我們傳輸檔案過大 報記憶體溢位時 需要修改這個配置項
4、驗證安裝是否成功  ./flume-ng version
5、配置環境變數
    export FLUME_HOME=/home/apache-flume-1.6.0-bin


Source、Channel、Sink有哪些型別
    Flume Source
    Source型別                   | 說明
    Avro Source                 | 支援Avro協議(實際上是Avro RPC),內建支援
    Thrift Source               | 支援Thrift協議,內建支援
    Exec Source                 | 基於Unix的command在標準輸出上生產資料
    JMS Source                   | 從JMS系統(訊息、主題)中讀取資料
    Spooling Directory Source | 監控指定目錄內資料變更
    Twitter 1% firehose Source|    通過API持續下載Twitter資料,試驗性質
    Netcat Source               | 監控某個埠,將流經埠的每一個文字行資料作為Event輸入
    Sequence Generator Source | 序列生成器資料來源,生產序列資料
    Syslog Sources               | 讀取syslog資料,產生Event,支援UDP和TCP兩種協議
    HTTP Source                 | 基於HTTP POST或GET方式的資料來源,支援JSON、BLOB表示形式
    Legacy Sources               | 相容老的Flume OG中Source(0.9.x版本)

    Flume Channel
    Channel型別       說明
    Memory Channel                | Event資料儲存在記憶體中
    JDBC Channel                  | Event資料儲存在持久化儲存中,當前Flume Channel內建支援Derby
    File Channel                  | Event資料儲存在磁碟檔案中
    Spillable Memory Channel   | Event資料儲存在記憶體中和磁碟上,當記憶體佇列滿了,會持久化到磁碟檔案
    Pseudo Transaction Channel | 測試用途
    Custom Channel                | 自定義Channel實現

    Flume Sink
    Sink型別     說明
    HDFS Sink             | 資料寫入HDFS
    Logger Sink           | 資料寫入日誌檔案
    Avro Sink             | 資料被轉換成Avro Event,然後傳送到配置的RPC埠上
    Thrift Sink           | 資料被轉換成Thrift Event,然後傳送到配置的RPC埠上
    IRC Sink              | 資料在IRC上進行回放
    File Roll Sink         | 儲存資料到本地檔案系統
    Null Sink             | 丟棄到所有資料
    HBase Sink             | 資料寫入HBase資料庫
    Morphline Solr Sink | 資料傳送到Solr搜尋伺服器(叢集)
    ElasticSearch Sink     | 資料傳送到Elastic Search搜尋伺服器(叢集)
    Kite Dataset Sink     | 寫資料到Kite Dataset,試驗性質的
    Custom Sink           | 自定義Sink實現



案例1、 A simple example
    http://flume.apache.org/FlumeUserGuide.html#a-simple-example
    
    配置檔案
    ############################################################
    # 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 = localhost
    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.transactionCapacity = 100

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

啟動flume
flume-ng agent -n a1 -c conf -f simple.conf -Dflume.root.logger=INFO,console

安裝telnet
yum install telnet
退出 ctrl+]  quit

Memory Chanel 配置
  capacity:預設該通道中最大的可以儲存的event數量是100,
  trasactionCapacity:每次最大可以source中拿到或者送到sink中的event數量也是100
  keep-alive:event新增到通道中或者移出的允許時間
  byte**:即event的位元組量的限制,只包括eventbody



案例2、兩個flume做叢集

    node01伺服器中,配置檔案
    ############################################################
    # 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 = node1
    a1.sources.r1.port = 44444

    # Describe the sink
    # a1.sinks.k1.type = logger
    a1.sinks.k1.type = avro
    a1.sinks.k1.hostname = node2
    a1.sinks.k1.port = 60000

    # 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
    ############################################################
    
    node02伺服器中,安裝Flume(步驟略)
    配置檔案
    ############################################################
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = avro
    a1.sources.r1.bind = node2
    a1.sources.r1.port = 60000

    # 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
    ############################################################
    
    先啟動node02的Flume
    flume-ng agent  -n a1 -c conf -f avro.conf -Dflume.root.logger=INFO,console
    
    再啟動node01的Flume
    flume-ng agent  -n a1 -c conf -f simple.conf2 -Dflume.root.logger=INFO,console
    
    開啟telnet 測試  node02控制檯輸出結果


案例3、Exec Source
        http://flume.apache.org/FlumeUserGuide.html#exec-source
        
    配置檔案
    ############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = exec
    a1.sources.r1.command = tail -F /home/flume.exec.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-ng agent -n a1 -c conf -f exec.conf -Dflume.root.logger=INFO,console
    
    建立空檔案演示 touch flume.exec.log
    迴圈新增資料
    for i in {1..50}; do echo "$i hi flume" >> flume.exec.log ; sleep 0.1; done
        
案例4、Spooling Directory Source
        http://flume.apache.org/FlumeUserGuide.html#spooling-directory-source
    配置檔案
    ############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /home/logs
    a1.sources.r1.fileHeader = true

    # 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-ng agent -n a1 -c conf -f spool.conf -Dflume.root.logger=INFO,console

    拷貝檔案演示
    mkdir logs
    cp flume.exec.log logs/


案例5、hdfs sink
        http://flume.apache.org/FlumeUserGuide.html#hdfs-sink
    
        配置檔案
    ############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /home/logs
    a1.sources.r1.fileHeader = true

    # Describe the sink
    ***只修改上一個spool sink的配置程式碼塊 a1.sinks.k1.type = logger
    a1.sinks.k1.type=hdfs
    a1.sinks.k1.hdfs.path=hdfs://sxt/flume/%Y-%m-%d/%H%M
    
    ##每隔60s或者檔案大小超過10M的時候產生新檔案
    # hdfs有多少條訊息時新建檔案,0不基於訊息個數
    a1.sinks.k1.hdfs.rollCount=0
    # hdfs建立多長時間新建檔案,0不基於時間
    a1.sinks.k1.hdfs.rollInterval=60
    # hdfs多大時新建檔案,0不基於檔案大小
    a1.sinks.k1.hdfs.rollSize=10240
    # 當目前被開啟的臨時檔案在該引數指定的時間(秒)內,沒有任何資料寫入,則將該臨時檔案關閉並重新命名成目標檔案
    a1.sinks.k1.hdfs.idleTimeout=3
    
    a1.sinks.k1.hdfs.fileType=DataStream
    a1.sinks.k1.hdfs.useLocalTimeStamp=true
    
    ## 每五分鐘生成一個目錄:
    # 是否啟用時間上的”捨棄”,這裡的”捨棄”,類似於”四捨五入”,後面再介紹。如果啟用,則會影響除了%t的其他所有時間表示式
    a1.sinks.k1.hdfs.round=true
    # 時間上進行“捨棄”的值;
    a1.sinks.k1.hdfs.roundValue=5
    # 時間上進行”捨棄”的單位,包含:second,minute,hour
    a1.sinks.k1.hdfs.roundUnit=minute

    # 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
    ############################################################
    建立HDFS目錄
    hadoop fs -mkdir /flume
    
    啟動Flume
    flume-ng agent -n a1 -c conf -f hdfs.conf -Dflume.root.logger=INFO,console

    檢視hdfs檔案
    hadoop fs -ls /flume/...
    hadoop fs -get /flume/...

作業:
1、flume如何收集java請求資料
2、專案當中如何來做? 日誌存放/log/目錄下 以yyyyMMdd為子目錄 分別存放每天的資料




相關文章