需求
- flume-1监控hive.log日志,flume-1的数据传送给flume-2,flume-2将数据追加到本地文件,同时将数据传输到flume-3。
- flume-4监控本地另一个自己创建的文件any.txt,并将数据传送给flume-3。
- flume-3将汇总数据写入到HDFS。
实现步骤
- 准备工作
在/opt/module/flume/job目录下创建group4文件夹
在/opt/module/datas目录下创建group4文件夹
在/opt/module/datas目录下创建any.txt - 编写flume1.conf,flume2.conf,flume3.conf,flume4.conf四个配置文件
flume1.conf
# 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 the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop-100
a1.sinks.k1.port = 14141
# 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
flume2.conf
# Name the components on this agent
a2.sources = r1
a2.sinks = k1 k2
a2.channels = c1 c2
a1.sources.r1.selector.type = replicating
# Describe/configure the source
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop-100
a2.sources.r1.port = 14141
# Describe the sink
a2.sinks.k1.type = file_roll
a2.sinks.k1.sink.directory = /opt/module/datas/group4/
a2.sinks.k2.type = avro
a2.sinks.k2.hostname = hadoop-100
a2.sinks.k2.port = 14142
# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1 c2
a2.sinks.k1.channel = c1
a2.sinks.k2.channel = c2
flume3.conf
# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop-100
a3.sources.r1.port = 14142
# Describe the sink
a3.sinks.k1.type = hdfs
a3.sinks.k1.hdfs.path = hdfs://hadoop-100:9000/flume4/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k1.hdfs.filePrefix = flume4-
#是否按照时间滚动文件夹
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
# Describe the channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1
flume4.conf
# Name the components on this agent
a4.sources = r1
a4.sinks = k1
a4.channels = c1
# Describe/configure the source
a4.sources.r1.type = exec
a4.sources.r1.command = tail -F /opt/module/datas/any.txt
a4.sources.r1.shell = /bin/bash -c
# Describe the sink
a4.sinks.k1.type = avro
a4.sinks.k1.hostname = hadoop-100
a4.sinks.k1.port = 14142
# Use a channel which buffers events in memory
a4.channels.c1.type = memory
a4.channels.c1.capacity = 1000
a4.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a4.sources.r1.channels = c1
a4.sinks.k1.channel = c1
- 启动hadoop、yarn、hive
- 进入flume目录,依次执行
bin/flume-ng agent -n a1 -c conf/ -f job/group4/flume1.conf
bin/flume-ng agent -n a2 -c conf/ -f job/group4/flume2.conf
bin/flume-ng agent -n a3 -c conf/ -f job/group4/flume3.conf
bin/flume-ng agent -n a4 -c conf/ -f job/group4/flume4.conf - 查看结果