hadoop之flume(三)
四、单flume对应多个Channel
案例1:使用flume1监控文件变化,flume1将变动内容传递给flume2,flume2负责存储到HDFS,同时flume1将变动内容传递给flume3,flume3负责输出到local
1.创建flume1.conf配置文件,用于监控/home/f1文件的变化,同时产生2个channel和2个sink,2个sink分别输送给flume2,flume3
#定义agent
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
#将数据流复制给多个channel
a1.sources.r1.selector.type = replicating
#定义source r1
a1.sources.r1.type = exec
a1.sources.r1.command = tail -f /home/f1
a1.sources.r1.shell = /bin/bash -c
#定义sink k1
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hdnode1
a1.sinks.k1.port = 2000
#定义sink k2
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hdnode1
a1.sinks.k2.port = 2001
#定义channel c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#定义channel c2
a1.channels.c2.type = memory
a1.channels.c2.capacity = 1000
a1.channels.c2.transactionCapacity = 100
#绑定sources sinks channels
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2
2.创建flume2.conf配置文件,用于接收flume1的event,同时产生1个channel和1个sink,将数据输送到hdfs中
#定义agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
#定义source r1
a2.sources.r1.type = avro
a2.sources.r1.bind = hdnode1
a2.sources.r1.port = 2000
#定义sink k1
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://hdnode1:9000/flume/%Y%m%d%H
#上传文件的前缀
a2.sinks.k1.hdfs.filePrefix = flume2-
#是否按照时间滚动文件夹
a2.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k1.hdfs.roundVlaue = 1
#定义时间单位
a2.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a2.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a2.sinks.k1.hdfs.batchSize = 1000
#设置文件类型,可以支持压缩
a2.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新文件
a2.sinks.k1.hdfs.rollInterval = 600
#设置每个文件的滚动大小
a2.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a2.sinks.k1.hdfs.roolCount = 0
#最小副本数
a2.sinks.k1.hdfs.minBlockReplicas = 1
#定义channel c1
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
#绑定sources sinks channels
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
3.创建flume3.conf配置文件,用于接收flume1的event,同时产生1个channel和1个sink,将数据输送到本地目录
#定义agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
#定义source r1
a3.sources.r1.type = avro
a3.sources.r1.bind = hdnode1
a3.sources.r1.port = 2001
#3 sink k1
a3.sinks.k1.type = file_roll
#文件夹需要提前先创建好
a3.sinks.k1.sink.directory = /home/tmpdir
#定义channel c1
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
#绑定sources sinks channels
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1
4.启动flume
5.运行结果
五、多flume对应单个Channel
案例:flume1监控文件/home/f1变化,flume2监控端口数据流,flume1和flume2将数据发送给flume3,flume3将最终数据写入hdfs
1.创建flume1,监控文件/home/f1变化,并且结果发送到flume2
#定义agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#定义source r1
a1.sources.r1.type = exec
a1.sources.r1.command = tail -f /home/f1
a1.sources.r1.shell = /bin/bash -c
#定义sink k1
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hdnode1
a1.sinks.k1.port = 2000
#定义channel c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#绑定sources sinks channels
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2.创建flume2,监听端口数据流,发送给flume3
#定义agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
#定义source r1
a2.sources.r1.type = netcat
a2.sources.r1.bind = hdnode1
a2.sources.r1.port = 44445
#定义sink k1
a2.sinks.k1.type = avro
a2.sinks.k1.hostname = hdnode1
a2.sinks.k1.port = 2000
#定义channel c1
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
#绑定sources sinks channels
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
3.创建flume3,上报数据到hdfs
#定义Agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
#定义source
a3.sources.r1.type = avro
a3.sources.r1.bind = hdnode1
a3.sources.r1.port = 2000
# 定义sink
a3.sinks.k1.type = hdfs
a3.sinks.k1.hdfs.path = hdfs://hdnode1:9000/flume/%Y%m%d%H
#上传文件的前缀
a3.sinks.k1.hdfs.filePrefix = flume-
#是否按照时间滚动文件夹
a3.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k1.hdfs.roundVlaue = 1
#定义时间单位
a3.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a3.sinks.k1.hdfs.batchSize = 1000
#设置文件类型,可以支持压缩
a3.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新文件
a3.sinks.k1.hdfs.rollInterval = 600
#设置每个文件的滚动大小
a3.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a3.sinks.k1.hdfs.roolCount = 0
#最小副本数
a3.sinks.k1.hdfs.minBlockReplicas = 1
# 定义memory
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
# 双向链接
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1
4.启动flume
启动flume3
/opt/module/flume-1.8.0/bin/flume-ng agent --conf /opt/module/flume1.8.0/conf/ --name a3 --conf-file /opt/module/flume-1.8.0/jobconf/flume3.conf -Dflume.root.logger==INFO,console
启动flume2
/opt/module/flume-1.8.0/bin/flume-ng agent --conf /opt/module/flume1.8.0/conf/ --name a2 --conf-file /opt/module/flume-1.8.0/jobconf/flume2.conf -Dflume.root.logger==INFO,console
启动flume1
/opt/module/flume-1.8.0/bin/flume-ng agent --conf /opt/module/flume1.8.0/conf/ --name a1 --conf-file /opt/module/flume-1.8.0/jobconf/flume1.conf -Dflume.root.logger==INFO,console
5.运行结果