Flume
一种分布式,可靠且可用的服务,用于有效地收集,聚合和移动大量日志数据。它具有基于流数据流的简单灵活的架构。它具有可靠的可靠性机制和许多故障转移和恢复机制,具有强大的容错性。它使用简单的可扩展数据模型,允许在线分析应用程序
Flume角色
(1)source
数据源,用户采集数据,source产生数据流,同时会把产生的数据流传输到channel
(2)channel
传输通道,用于桥接source和sink
(3)sink
下沉,用于手机channel传输的数据,将数据源传递到目标源
(4)event
在flume中使用事件作为传输的基本单元
Flume的安装
(1)上传flume压缩包apache-flume-1.8.0-bin.tar.gz到Linux中
(2)命令tar -zvxf解压flume压缩包
(3)修改conf文件夹下的flume-env.sh.template命名
(4)修改flume-env.sh文件的配置
Flume监听端口
(1)在conf文件夹下创建配置文件flumejob-telnet.conf
# example.conf: A single-node Flume configuration
# Name the components on this agent
# 定义变量方便调用,加s可以有多个此角色
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
# 描述source角色,进行内容定制
# 此配置属于tcp source必须是netcat类型
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# Describe the sink
# 输出日志文件
a1.sinks.k1.type = logger
# Use a channel which buffers events in memory
# 使用内存,总大小1000,每次传输100
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
# 一个source可以绑定多个channel
# 一个sinks可以智能绑定一个channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
(2)查找和安装telnet命令包
(3)开启flume
(4)telnet端口44444和发送时间给flume
Flume监听本地文件
(1)命令start-all.sh开启HDFS
(2)在conf文件夹下创建配置文件flumejob-hdfs.conf
# example.conf: A single-node Flume configuration
# Name the components on this agent
# 定义变量方便调用,加s可以有多个此角色
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
# 设置数据源监听本地文件配置
# exec执行一个命令的方式去查看文件 tail -F 实时查看
a1.sources.r1.type = exec
# 要执行的脚本command tail -F 默认10行 man tail 查看帮助
a1.sources.r1.command = tail -F /tmp/root/hive.log
# 执行这个command使用的是哪个脚本 -c 指定使用什么命令
# whereis bash
# bash: /usr/bin/bash /usr/share/man1/bash.1.gz
a1.sources.r1.shell = /usr/bin/bash -c
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop1:9000/flume/%Y%m%d/%H
# 上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = logs-
# 是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
# 多少时间单位创建一个新的文件夹 秒 (默认30s)
a1.sinks.k1.hdfs.roundValue = 1
# 重新定义时间单位(每小时滚动一个文件夹)
a1.sinks.k1.hdfs.roundUnit = minute
# 是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
# 积攒多少个Event才flush到HDFS一次
a1.sinks.k1.hdfs.batchSize = 500
# 设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
# 多久生成一个新的文件 秒
a1.sinks.k1.hdfs.roolInterval = 30
# 设置每个文件的滚动大小 字节(最好128M)
a1.sinks.k1.hdfs.rollSize = 134217700
# 文件的滚动与Event数量无关
a1.sinks.k1.hdfs.rollCount = 0
# 最小冗余数(备份输 生成滚动功能则生效roll hadoop本身有此功能 无需配置)1份 不冗余
a1.sinks.k1.hdfs.minBlockReplicas = 1
# Use a channel which buffers events in memory
# 使用内存,总大小1000,每次传输100
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
# 一个source可以绑定多个channel
# 一个sinks可以只能绑定一个channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
(3)将jar包放置到lib文件夹下
(4)开启flume
(5)监听本地文件采集到HDFS
Flume监听本地文件夹
(1)命令start-all.sh开启HDFS
(2)在conf文件夹下创建配置文件flumejob-dir.conf
# Name the components on this agent
# 定义变量方便调用,加s可以有多个此角色
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /usr/local/testdir
# 上传成功后显示后缀名
a1.sources.r1.fileSuffix = .COMPLETED
a1.sources.r1.fileHeader = true
#忽略所有以.tmp结尾的文件,不上传
a1.sources.r1.ignorePattern = ([^ ]*\.tmp)
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop1:9000/flume/testdir/%Y%m%d/%H
# 上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = testdir-
# 是否按照时间滚动文件夹
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
# 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
(3)开启flume
Flume监听日志文件下沉到HDFS和本地
(1)命令start-all.sh开启HDFS
(2)在hadoop1机器的conf文件夹下创建配置文件flumejob-01.conf
# name the components on this agent 别名设置
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# 将数据流复制给多个channel
a1.sources.r1.selector.type = replicating
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /tmp/root/hive.log
a1.sources.r1.shell = /bin/bash -c
# Describe the sink
# 分两个端口发送数据
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop2
a1.sinks.k1.port = 4141
a1.sinks.k2.type = avro
a1.sinks,k2.hostname = hadoop3
a1.sinks.k2.port = 4142
# Describe the channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
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
(3)在hadoop2机器的conf文件夹下创建配置文件flumejob-02.conf
# name the components on this agent 别名设置
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop2
a2.sources.r1.port = 4141
# Describe the sink
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://hadoop1:9000/flume/%Y%m%d/%H
# 上传文件的前缀
a2.sinks.k1.hdfs.filePrefix = flume-
# 是否按照时间滚动文件夹
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
# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1
(4)在hadoop3机器的conf文件夹下创建配置文件flumejob-03.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 = hadoop3
a3.sources.r1.port = 4142
# Describe the sink
a3.sinks.k1.type = file_roll
a3.sinks.k1.sink.directory = /root/flume
# 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
(5)启动Hive
(6)开启flume
(7)操作Hive
参考资料:https://blog.csdn.net/liuxiangke0210/article/details/71006148和https://408933325.iteye.com/blog/2217712
官网资料:http://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html