Spark Streaming實時流處理項目1 —— 分佈式日誌收集框架Flume的學習

前言:實時流處理架構和技術選型

一、簡介

Flume的使用基本上就是寫配置文件!

Agent component diagram

特點:分佈式、高可用、高可靠

一個Flume服務器就理解爲一個Agent。

三大組件:Source、Channel、Sink

Source:數據源,一般要和Web server對接上(收集);

Channel:通道,數據收集到暫時緩存的地方(聚集),類似於一個數據緩存池;

Sink:把Channel裏的數據輸出、寫入、下沉到HDFS或其他文件系統中(輸出)。

一句話總結Flume的作用就是:將日誌從A地方收集、聚合、搬運到B地方去!

Flume支持很多種數據源,使用Java語言開發。

多Agent架構:

二、Flume環境部署

前置條件:

  1. Java1.7及以上
  2. 足夠的內存(channel到內存中的)
  3. 磁盤空間也要足夠
  4. 權限,由於涉及到文件操作,所以要對文件有讀寫權限

安裝:

1、下載、上傳、解壓

 tar -xzvf apache-flume-1.6.0-bin.tar.gz 

vi /etc/profile
export FLUME_HOME=/soft/flume
export PATH=$PATH:$FLUME_HOME/bin

source /etc/profile

2、配置

進入conf文件夾,複製一份配置模板flume-env.sh.template爲 flume-env.sh,修改裏面的JAVA_HOME爲本機的。

3、檢測是否安裝成功

進入bin目錄下,運行命令 flume-ng version

可能會提示如下錯誤:

錯誤: 找不到或無法加載主類 org.apache.flume.tools.GetJavaProperty
Flume 1.6.0
Source code repository: https://git-wip-us.apache.org/repos/asf/flume.git
Revision: 2561a23240a71ba20bf288c7c2cda88f443c2080
Compiled by hshreedharan on Mon May 11 11:15:44 PDT 2015
From source with checksum b29e416802ce9ece3269d34233baf43f

原因:
1、jdk衝突
2、安裝了hbase就會報着個錯
解決:
1、卸載openjdk
2、安裝jdk7.
3、將hbase的hbase.env.sh的一行配置註釋掉
# Extra Java CLASSPATH elements. Optional.
#export HBASE_CLASSPATH=/home/hadoop/hbase/conf
4、或者將HBASE_CLASSPATH改爲JAVA_CLASSPATH,配置如下
# Extra Java CLASSPATH elements. Optional.
export JAVA_CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar


三、Flume實戰

需求1:從指定端口採集數據並輸出到控制檯

使用Flume的關鍵就是寫配置文件,配置文件的構成:

1、配置source

2、配置channel

3、配置sink

4、把以上三個組件串起來

下面爲一個配置文件實例:

# example.conf: A single-node Flume configuration
# Name the components on this agent
a1.sources = r1 
a1.sinks = k1
a1.channels = c1
a1表示的是agent的名稱,啓動的時候是需要指定agent的名稱的。a1.sources指定數據源(source),可以爲多個,這裏就只定一個r1;同理,k1就是sink的名稱,c1就是channel的名稱。

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = hadoop0
a1.sources.r1.port = 44444
配置數據源r1的各項屬性,type=netcat表示數據源的類型(source),此類型可以用於監聽某個端口,當然了還有其他的類型,bind表示綁定的主機名或者IP地址,port爲端口號

# Describe the sink
a1.sinks.k1.type = logger
配置sink,此agent中sink只用到了一個就是k1.此處類型是logger

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
配置channel,類型是memory,還有容量和其他等。

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1   把以上三個組件串起來。注意:一個source可以輸出到多個channel,所以a1.sources.r1.channels 這裏使用了複數。但是,一個channel輸出到的sink只能是一個(一個sink只能有一個渠道)。

把上面的配置語句寫進配置文件,一般情況下可以把配置文件放進conf目錄下面。

配置完成之後,啓動agent。

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

--name後面跟agent的名字;

--conf 後面是flume安裝目錄下的conf文件夾

--conf-file 後面是自己寫的配置文件路徑

-Dflume.root.logger=INFO,console Java配置,結果輸出到控制檯

啓動起來之後使用telnet進行測試。

 yum install telnet

telnet hadoop0 44444

輸入內容,回車後內容就會打印在控制檯。

2019-02-16 16:19:26,169 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 68 65 6C 6C 6F 0D                               hello. }

Event是flume數據傳輸的基本單元,Event=可選的header+byte array,一條記錄就是一個Event。

需求2:監控一個文件,實時採集新增的數據,並輸出到控制檯

Agent選型:

source選擇Exec ,channel還是memory,sink還是選擇logger

配置文件和需求一的大同小異,直接複製過來,修改,紅色爲修改部分。

# 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 /soft/flume/data/data.log
a1.sources.r1.shell = /bin/sh -c
監控/soft/flume/data/data.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

配置文件名是exec-memory-logger.conf ,還放在conf文件下。運行

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

需求3:將A服務器上的日誌實時採集到B服務器(重點)

生產中假設日誌在web Server中,如何把實時產生的日誌收集到數據分析服務器(Spark集羣)上呢。這種需求使用的最多,重點掌握。

如下圖示例:

技術選型:

機器A : exec source + memory channel + avro sink

機器B:avro source + memory channel + logger sink(輸出控制檯)

機器A(hadoop0)配置文件exec-memory-avro.conf配置內容:

exec-memory-avro.sources = exec-source
exec-memory-avro.sinks = avro-sink
exec-memory-avro.channels = memory-channel
agent名稱設置爲exec-memory-avro

exec-memory-avro.sources.exec-source.type = exec
exec-memory-avro.sources.exec-source.command= tail -F /soft/flume/data/data.log
exec-memory-avro.sources.exec-source.shell= /bin/sh -c

exec-memory-avro.sinks.avro-sink.type = avro
exec-memory-avro.sinks.avro-sink.hostname= hadoop1
exec-memory-avro.sinks.avro-sink.port= 44444

exec-memory-avro.channels.memory-channel.type = memory

exec-memory-avro.sources.exec-source.channels = memory-channel
exec-memory-avro.sinks.avro-sink.channel = memory-channel

機器B(hadoop1)配置文件avro-memory-logger.conf配置內容:

avro-memory-logger.sources = avro-source
avro-memory-logger.sinks = logger-sink
avro-memory-logger.channels = memory-channel
agent名稱設置爲exec-memory-avro

avro-memory-logger.sources.avro-source.type = avro
avro-memory-logger.sources.avro-source.bind= hadoop1
avro-memory-logger.sources.avro-source.port= 44444

avro-memory-logger.sinks.logger-sink.type = logger

avro-memory-logger.channels.memory-channel.type = memory

avro-memory-logger.sources.avro-source.channels = memory-channel
avro-memory-logger.sinks.logger-sink.channel = memory-channel

啓動的時候一定要先啓動avro-memory-logger.conf(機器B)

$ bin/flume-ng agent --name avro-memory-logger --conf $FLUME_HOME/conf --conf-file $FLUME_HOME/conf/avro-memory-logger.conf -Dflume.root.logger=INFO,console

再啓動exec-memory-avro.conf(機器A)

$ bin/flume-ng agent --name exec-memory-avro --conf $FLUME_HOME/conf --conf-file $FLUME_HOME/conf/exec-memory-avro.conf -Dflume.root.logger=INFO,console

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