flume傳輸日志文件到HDFS過程講解


 

Flume定義:

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

 

為什么選用Flume

  主要作用: 實時讀取服務器本地磁盤的數據, 將數據寫入到HDFS

 

 Flume的組織架構

  最簡單的組織架構, 單agent

 

   Flume流式處理過程

 

 

  說明:

    source: 數據輸入端

      常見類型: spooling directory, exec, syslog, avro, netcat等

    channel:位於source和sink之間的緩沖區

      memory: 基於內存緩存, 允許數據有丟失

      file: 持久化channel, 系統宕機不會丟失數據

    sink: 數據輸出端

      常見的目的地有: HDFS, Kafka, logger, avro, File, 自定義

    Put事務流程:

      doPut: 將批數據寫入臨時緩沖區putList

      doCommit: 檢查channel內存隊列是否足夠合並

      doRollback: 內存隊列空間不足, 回滾數據

    Take事務流程:

      doTake: 將批數據提取到臨時緩沖區takeList

      doCommit: 如果數據全部發送成功, 則清空臨時緩沖區takeList

      doRollback: 如果數據發送過程中出現異常, 則將臨時緩沖區takeList中數據返還給channel

 

  多agent

  

 

 

 

 

  事件流多路傳輸到一個或多個目標, 通過定義一個流復用器來實現.

 

 

 

 

 

  多路日志收集型

 

 

 

 

Flume安裝

  Flume官網地址: http://flume.apache.org/

  文檔地址: http://flume.apache.org/FlumeUserGuide.html

  下載地址: http://flume.apache.org/download.html

  安裝步驟:

  1、將apache-flume-1.7.0-bin.tar.gz上傳到/opt/software目錄下

  2、解壓到/opt/module目錄下

    # tar -zxf apache-flume-1.7.0-bin.tar.gz -C /opt/module/

  3、將目錄apache-flume-1.7.0-bin改為名flume

    # mv apache-flume-1.7.0-bin flume

  4、將flume-env.sh.template改名為flume-env.sh, 並修改其配置

    # mv flume/conf/flume-env.sh.template flume/conf/flume-env.sh

    # vi flume/conf/flume-env.sh

      export JAVA_HOME=/opt/module/jdk1.8.0_161

  5、驗證安裝是否成功 

  # flume/bin/flume-ng version
  Flume 1.7.0
  Source code repository: https://git-wip-us.apache.org/repos/asf/flume.git
  Revision: 511d868555dd4d16e6ce4fedc72c2d1454546707
  Compiled by bessbd on Wed Oct 12 20:51:10 CEST 2016
  From source with checksum 0d21b3ffdc55a07e1d08875872c00523

 

實時讀取本地文件到HDFS

  1、flume必須持有hadoop相關的包才能將數據輸出到hdfs, 將如下包上傳到flume/lib下

    涉及到的包如下, 以hadoop-2.9.2為例:

    commons-configuration-1.6.jar

    commons-io-2.4.jar

    hadoop-auth-2.9.2.jar

    hadoop-common-2.9.2.jar

    hadoop-hdfs-2.9.2.jar

    hadoop-hdfs-client-2.9.2.jar

    htrace-core4-4.1.0-incubating.jar

    stax2-api-3.1.4.jar

    woodstox-core-5.0.3.jar

  2、修改/etc/hosts, 加入hadoop的地址

    10.xx.xx.xx hadoo

  3、創建配置文件flume-file-hdfs.conf

    # cd flume/

    # mkdir jobs

    # touch jobs/flume-file-hdfs.conf

  4、添加內容如下

    

# 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
a1.sources.r1.command = tail -F /opt/module/flume/logs/flume.log
#執行shell腳本的絕對路徑
a1.sources.r1.shell = /bin/bash -c

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop:9000/flume/%Y%m%d/%H
#上傳文件的前綴
a1.sinks.k1.hdfs.filePrefix = logs-
#是否按照時間滾動文件
a1.sinks.k1.hdfs.round = true
#多少時間單位創建一個新文件夾
a1.sinks.k1.hdfs.roundValue = 1
#時間單位
a1.sinks.k1.hdfs.roundUnit = hour
#是否使用本地時間戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#積攢多少個event才flush一次hdfs
a1.sinks.k1.hdfs.batchSize = 100
#文件類型, 是否壓縮
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一個新文件
a1.sinks.k1.hdfs.rollInterval = 60
#每個文件的大小
a1.sinks.k1.hdfs.rollSize = 10240
#文件的滾動與event數量無關
a1.sinks.k1.hdfs.rollCount = 0
#最小冗余
a1.sinks.k1.hdfs.minBlockReplicas = 1

# 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

 5、啟動flume

  # bin/flume-ng agent --conf conf/ --conf-file jobs/flume-file-hdfs.conf --name a1  -Dflume.root.logger=DEBUG,console 

 6、查看hdfs系統文件是否接收到文件

  # hdfs dfs -ls /flume
  Found 1 items
  drwxr-xr-x - root supergroup 0 2020-04-02 22:37 /flume/20200402

 

 flume傳輸日志文件到hdfs成功了。

  

友情提示:

  hadoop偽分布部署: https://www.cnblogs.com/kongzhagen/p/6872297.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM