(二十二)日誌採集框架Flume的介紹與案例

前言

在一個完整的大數據處理系統中,除了hdfs+mapreduce+hive組成分析系統的核心之外,還需要數據採集、結果數據導出、任務調度等不可或缺的輔助系統,而這些輔助工具在hadoop生態體系中都有便捷的開源框架

如圖所示:

1.Flume介紹

1.1 概述

  • Flume是一個分佈式、可靠、和高可用的海量日誌採集、聚合和傳輸的系統。
  • Flume可以採集文件,socket數據包等各種形式源數據,又可以將採集到的數據輸出到HDFS、hbase、hive、kafka等衆多外部存儲系統中
  • 一般的採集需求,通過對flume的簡單配置即可實現
  • Flume針對特殊場景也具備良好的自定義擴展能力,因此,flume可以適用於大部分的日常數據採集場景

1.2 運行機制

  1. Flume分佈式系統中最核心的角色是agent,flume採集系統就是由一個個agent所連接起來形成
  2. 每一個agent相當於一個數據傳遞員,內部有三個組件:Source:採集源,用於跟數據源對接,以獲取數據Sink:下沉地,採集數據的傳送目的,用於往下一級agent傳遞數據或者往最終存儲系統傳遞數據Channel:angent內部的數據傳輸通道,用於從source將數據傳遞到sink
  3.  

單個agent採集數據

多級agent之間串聯

2.Flume的安裝部署

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

上傳後解壓縮

tar -zxvf apache-flume-1.8.0-bin.tar.gz -C /usr/local/

改名:mv apache-flume-1.8.0-bin/ flume

2.2修改配置文件

進入conf目錄:

cp flume-env.sh.template flume-env.sh

在裏面配置JAVA_HOME

2.3配置環境變量

vim ~/.bashrc

中增加你的flume的安裝目錄路徑

source ~/.bashrc

2.4查看版本(到flume的bin目錄)

flume-ng version

3.Flume的三個案例

3.1、案例一

flume的官方文檔https://flume.apache.org/FlumeUserGuide.html

需求

從指定網絡端口採集數據輸出到控制檯

操作

使用flume的關鍵就是寫配置文件

  • 1)配置Source
  • 2)配置Channel
  • 3)配置Sink
  • 4)把以上三個組件串起來

在conf下增加配置文件

vim netcat-logger.conf

# 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
a1.sources.r1.type = netcat
a1.sources.r1.bind = Master
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

 

  • a1:agent名稱
  • r1:source名稱
  • k1:sink名稱
  • c1:channel名稱

啓動agent

netcat-logger.conf是自己剛剛創建的配置文件名字

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

正在監聽數據

如何操作(使用telnet進行操作)

打開新的控制檯,對端口號44444進行操作

telnet Master 44444

接收數據進行分析

Event: { headers:{} body: 68 65 6C 6C 6F 0D hello.}

Event是Flume數據傳輸的基本單元

Event=可選的header+byte array

一行記錄即爲一個Event

3.2、案例二

需求

監控一個文件實時採集新增的數據輸出到控制檯

Agent選型:exec source  +  memory channel + logger sink

1.在conf下創建新配置文件example.conf

# 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
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /usr/local/hadoop/data/data.log
a1.sources.r1.shell = /bin/sh -c

# 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

2.在/usr/local/hadoop中創建data文件夾,並創建data.log文件

mkdir data

vim data.log 保存並退出

2.啓動Agent

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

3.data.log增加內容

echo hello >> data.log

3、案例三

需求

將A服務器上的日誌實時採集到B服務器上

 

技術選型:

exec source  + memory channel + avro sink

 

avro source + memory channel + logger sink

啓動

 

 

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