目錄
Flume的扇入與扇出
之前的案例都只有一個Flume的客戶端工作,其實Flume之間也可以進行數據傳遞,這樣的傳遞我們可以稱爲扇入和扇出。
案例四:單Flume多Channel、Sink(扇出)
目標:使用flume1監控文件變動,flume1將變動內容傳遞給flume-2,flume-2負責存儲到HDFS。同時flume1將變動內容傳遞給flume-3,flume-3負責輸出到local。由於整個流程圖的出口像“扇子”一樣,所以稱爲扇出。
實現過程:
(1)創建flume1.conf,用於監控某文件的變動,同時產生兩個channel和sink分別輸送給flume2和flume3。
# 1.agent source->channel對應關係1/n sink->channel對應關係1/1
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# 將數據流複製給多個channel
a1.sources.r1.selector.type = replicating
# 2.source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/stephenyou
a1.sources.r1.shell = /bin/bash -c
# 3.sink1
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = bigdata112
a1.sinks.k1.port = 4141
# sink2
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = bigdata113
a1.sinks.k2.port = 4141
# 4.channel—1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# 4.channel—2
a1.channels.c2.type = memory
a1.channels.c2.capacity = 1000
a1.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2
(2) 創建flume-2.conf,用於接收flume1的event,同時產生1個channel和1個sink,將數據輸送給hdfs:
# 1 agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# 2 source
a2.sources.r1.type = avro
a2.sources.r1.bind = bigdata112
a2.sources.r1.port = 4141
# 3 sink
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://bigdata111:9000/flume2/%H
#上傳文件的前綴
a2.sinks.k1.hdfs.filePrefix = flume2-
#是否按照時間滾動文件夾
a2.sinks.k1.hdfs.round = true
#多少時間單位創建一個新的文件夾
a2.sinks.k1.hdfs.roundValue = 1
#重新定義時間單位
a2.sinks.k1.hdfs.roundUnit = hour
#是否使用本地時間戳
a2.sinks.k1.hdfs.useLocalTimeStamp = true
#積攢多少個Event才flush到HDFS一次
a2.sinks.k1.hdfs.batchSize = 100
#設置文件類型,可支持壓縮
a2.sinks.k1.hdfs.fileType = DataStream
#多久生成一個新的文件
a2.sinks.k1.hdfs.rollInterval = 600
#設置每個文件的滾動大小大概是128M
a2.sinks.k1.hdfs.rollSize = 134217700
#文件的滾動與Event數量無關
a2.sinks.k1.hdfs.rollCount = 0
#最小副本數
a2.sinks.k1.hdfs.minBlockReplicas = 1
# 4 channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
#5 Bind
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
(3)創建flume-3.conf,用於接收flume1的event,同時產生1個channel和1個sink,將數據輸送給本地目錄:
#1 agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
# 2 source
a3.sources.r1.type = avro
a3.sources.r1.bind = bigdata113
a3.sources.r1.port = 4141
#3 sink
a3.sinks.k1.type = file_roll
#備註:此處的文件夾需要先創建好
a3.sinks.k1.sink.directory = /opt/flume3
# 4 channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
# 5 Bind
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1
(4)執行測試:分別開啓對應flume-job(依次啓動flume1,flume-2,flume-3),同時產生文件變動並觀察結果:
#bigadata111
flume-ng agent --conf conf/ --name a1 --conf-file myconf/flume1.conf -Dflume.root.logger==INFO,console
#bigdata112
flume-ng agent --conf conf/ --name a2 --conf-file myconf/flume2.conf
#bigdata113
flume-ng agent --conf conf/ --name a3 --conf-file myconf/flume3.conf
(5)結果
a.說明flume2的監控產生效果
b.查看flume3的記錄文件
案例五:多Flume彙總數據到單Flume
目標:flume11監控文件hive.log,flume-22監控某一個端口的數據流,flume11與flume-22將數據發送給flume-33,flume33將最終數據寫入到HDFS。
實現:
(1)創建flume11.conf,用於監控hive.log文件,同時sink數據到flume-33:
# 1 agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# 2 source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/stephenyou
a1.sources.r1.shell = /bin/bash -c
# 3 sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = bigdata113
a1.sinks.k1.port = 4141
# 4 channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# 5. Bind
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
(2)創建flume-22.conf,用於監控端口44444數據流,同時sink數據到flume-33:
# 1 agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# 2 source
a2.sources.r1.type = netcat
a2.sources.r1.bind = bigdata112
a2.sources.r1.port = 44444
#3 sink
a2.sinks.k1.type = avro
a2.sinks.k1.hostname = bigdata113
a2.sinks.k1.port = 4141
# 4 channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# 5 Bind
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
(3)創建flume33.conf,用於接收flume11與flume22發送過來的數據流,最終合併後sink到HDFS
# 1 agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
# 2 source
a3.sources.r1.type = avro
a3.sources.r1.bind = bigdata113
a3.sources.r1.port = 4141
# 3 sink
a3.sinks.k1.type = hdfs
a3.sinks.k1.hdfs.path = hdfs://bigdata111:9000/flume3/%H
#上傳文件的前綴
a3.sinks.k1.hdfs.filePrefix = flume3-
#是否按照時間滾動文件夾
a3.sinks.k1.hdfs.round = true
#多少時間單位創建一個新的文件夾
a3.sinks.k1.hdfs.roundValue = 1
#重新定義時間單位
a3.sinks.k1.hdfs.roundUnit = hour
#是否使用本地時間戳
a3.sinks.k1.hdfs.useLocalTimeStamp = true
#積攢多少個Event才flush到HDFS一次
a3.sinks.k1.hdfs.batchSize = 100
#設置文件類型,可支持壓縮
a3.sinks.k1.hdfs.fileType = DataStream
#多久生成一個新的文件
a3.sinks.k1.hdfs.rollInterval = 600
#設置每個文件的滾動大小大概是128M
a3.sinks.k1.hdfs.rollSize = 134217700
#文件的滾動與Event數量無關
a3.sinks.k1.hdfs.rollCount = 0
#最小冗餘數
a3.sinks.k1.hdfs.minBlockReplicas = 1
# 4 channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
# 5 Bind
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1