Flume——開發案例

Flume——開發案例

監控端口數據發送到控制檯

source:netcat

channel:memory

sink:logger

# 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/configure the channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Describe/configure the sink
a1.sinks.k1.type = logger

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

實時讀取本地文件到HDFS

方案一:(存在重複數據的風險)

source:exec

channel:file

sink:hdfs

# 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 /opt/module/hive/logs/hive.log
a1.sources.r1.shell = /bin/bash -c

# Describe/configure the channel
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /mnt/flume/checkpoint
a1.channels.c1.dataDirs = /mnt/flume/data

# Describe/configure the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://ip:host/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 = 1000
#設置文件類型,可支持壓縮
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一個新的文件
a1.sinks.k1.hdfs.rollInterval = 600
#設置每個文件的滾動大小
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滾動與 Event 數量無關
a1.sinks.k1.hdfs.rollCount = 0
#最小冗餘數
a1.sinks.k1.hdfs.minBlockReplicas = 1

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

方案二:(安全,不丟數據)

source:TAILDIR

channel:file

sink:hdfs

# Name the components on this agent
a1.sources = r1
a1.channels = c1
a1.sinks = k1

# Describe/configure the source
a1.sources.r1.type = TAILDIR
a1.sources.r1.positionFile = /var/log/flume/taildir_position.json
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /var/log/test1/example.log
a1.sources.r1.filegroups.f2 = /var/log/test2/.*log.*
a1.sources.r1.fileHeader = true
a1.sources.ri.maxBatchCount = 1000

# Describe/configure the channel
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /mnt/flume/checkpoint
a1.channels.c1.dataDirs = /mnt/flume/data

# Describe/configure the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/%S
a1.sinks.k1.hdfs.filePrefix = FlumeData-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute

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

實時讀取目錄下文件到HDFS

source:spooldir

channel:file

sink:hdfs

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /opt/module/flume/upload
a1.sources.r1.fileSuffix = .COMPLETED
a1.sources.r1.fileHeader = true

# Describe/configure the channel
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /mnt/flume/checkpoint
a1.channels.c1.dataDirs = /mnt/flume/data

# Describe/configure the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://ip:host/flume/upload/%Y%m%d/%H
#上傳文件的前綴
a1.sinks.k1.hdfs.filePrefix = upload-
#是否按照時間滾動文件夾
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 = 600
#設置每個文件的滾動大小大概是 128M
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滾動與 Event 數量無關
a1.sinks.k1.hdfs.rollCount = 0
#最小冗餘數
a1.sinks.k1.hdfs.minBlockReplicas = 1

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

實時讀取本地文件到Kafka(重點)

場景:所有埋點數據統一發送到NG服務器,經過負載均衡後,均勻發送到3臺服務器(數量自行配置),再由每臺服務器上Flume將數據採集到Kafka。整體架構如圖:

在這裏插入圖片描述

source:TAILDIR

channel:file

sink:kafka

# Name the components on this agent
a1.sources = r1
a1.channels = c1
a1.sinks = k1

# Describe/configure the source
a1.sources.r1.type = TAILDIR
a1.sources.r1.positionFile = /var/log/flume/taildir_position.json
a1.sources.r1.filegroups = f1 f2

a1.sources.r1.filegroups.f1 = /var/log/test1/example.log
a1.sources.r1.headers.f1.topic = topic_a

a1.sources.r1.filegroups.f2 = /var/log/test2/.*log.*
a1.sources.r1.headers.f1.topic = topic_b

a1.sources.r1.fileHeader = false
a1.sources.r1.maxBatchCount = 1000

# Describe/configure the channel
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /mnt/flume/checkpoint
a1.channels.c1.dataDirs = /mnt/flume/data

# Describe/configure the sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.bootstrap.servers = list[ip:host]
a1.sinks.k1.kafka.flumeBatchSize = 100
a1.sinks.k1.kafka.producer.acks = 1
a1.sinks.k1.kafka.producer.compression.type = snappy

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

單數據源多出口(Sink組)——故障轉移

在這裏插入圖片描述

Flume-1爲單source,單channel,多Sink,即Sink組。在Flume-2或者Flume-3發生故障時,可實現故障轉移。其中,Flume-1的配置可參考:

# Name the components on this agent
a1.sources = r1
a1.channels = c1
a1.sinks = k1 k2
a1.sinkgroups = g1

# Describe/configure the source
a1.sources.r1.type = TAILDIR
a1.sources.r1.positionFile = /var/log/flume/taildir_position.json
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /var/log/test1/example.log
a1.sources.r1.filegroups.f2 = /var/log/test2/.*log.*
a1.sources.r1.fileHeader = true
a1.sources.ri.maxBatchCount = 1000

# Describe/configure the channel
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /mnt/flume/checkpoint
a1.channels.c1.dataDirs = /mnt/flume/data

# Describe/configure the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop1
a1.sinks.k1.port = 4141

a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop1
a1.sinks.k2.port = 4142

# Describe/configure the sink groups
a1.sinkgroups.g1.sinks = k1 k2
# "failover"即故障轉移策略
a1.sinkgroups.g1.processor.type = failover
# 爲k1、k2分配權重(優先發k2)
a1.sinkgroups.g1.processor.priority.k1 = 5
a1.sinkgroups.g1.processor.priority.k2 = 10
a1.sinkgroups.g1.processor.maxpenalty = 10000

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

單數據源多出口(Sink組)——負載均衡

在這裏插入圖片描述

Flume-1的配置可參考:(只需要修改上述代碼中的“sink group”部分)

# Describe/configure the sink groups
a1.sinkgroups.g1.sinks = k1 k2
# "failover"即故障轉移策略
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true
# 發送策略:round_robin, random
a1.sinkgroups.g1.processor.selector = random
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章