Flume 的应用1(exec source、memory channel、hdfs sink)

Flume 之exec source、memory channel、hdfs sink

2.2 实时监控单个追加文件

1)案例需求:实时监控 Hive 日志,并上传到HDFS中

  • 注意tail -f (失败后不重试) tail -F (失败后重试一般3次)

2)需求分析

在这里插入图片描述

3)实现步骤

  • (1)Flume 需要依赖 Hadoop 相关的 jar包,才能将数据输出到 HDFS 上
    • 检查/etc/profile.d/my_env.sh文件,确认Hadoop和Java环境变量配置正确
  • (2)创建 flume-file-logger.conf,flume-file-hdfs1.conf,flume-file-hdfs2.conf 文件

案例2版本一

[xiaoxq@hadoop105 jobs]$ vim file-flume-logger.conf
  • :要想读取 Linux 系统中的文件,就得按照 Linux 命令的规则执行命令。由于 Hive 日志在 Linux 系统中所以读取文件的类型选择:exec 即 execute 执行的意思。表示执行 Linux 命令来读取文件。
# 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-3.1.2/logs/hive.log


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

# 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
[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent -c conf/ -f jobs/file-flume-logger.conf -n a1 -Dflume.root.logger=INFO,console

在这里插入图片描述

  • 在hive中进行操作时日志会发生滚动
  • 因为 /opt/module/hive-3.1.2/logs/hive.log 本身日志太长,可以选择自己创建在 /opt/module/datas/hive.log 自己通过 echo 命令追加到至文件中
    • 将日志上传到HDFS上

案例2版本二

(1) 创建配置文件

[xiaoxq@hadoop105 jobs]$ vim file-flume-hdfs1.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/datas/hive.log


# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume

# 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

(2) 运行Flume

[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent -c conf/ -f jobs/file-flume-hdfs1.conf -n a1

(3)开启 Hadoop 和 Hive 并操作 Hive 产生日志(或者自己追加)

  • 创建日志存放文件夹
[xiaoxq@hadoop105 module]$ mkdir datas
[xiaoxq@hadoop105 module]$ cd datas
[xiaoxq@hadoop105 datas]$ ll
总用量 0
[xiaoxq@hadoop105 datas]$ vim hive.log
[xiaoxq@hadoop105 datas]$ ll
总用量 0
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月   7 18:32 hive.log
  • 先自己追加一条信息到 hive.log 中
[xiaoxq@hadoop105 datas]$ ll
总用量 0
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月   7 18:32 hive.log

[xiaoxq@hadoop105 datas]$ echo '1111' >> hive.log 
[xiaoxq@hadoop105 datas]$ ll
总用量 4
-rw-rw-r--. 1 xiaoxq xiaoxq 5 8月   7 18:43 hive.log

(4)在HDFS上查看文件。

  • 查看 HDFS 上会出现 flume文件目录

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 出现乱码,官网上查看得知文件的类型默认是二进制格式(二进制格式,数据流格式,可压缩格式)

在这里插入图片描述

  • 需要设置添加文件类型DataStream,使其不乱码
    在这里插入图片描述

  • 重新启动,会默认打印10行日志(tail -F)

在这里插入图片描述

在这里插入图片描述

  • 再执行追加命令到日志(第一次启动会打印末尾的10行(tail -F))
[xiaoxq@hadoop105 datas]$ echo 'ababa' >> hive.log 
[xiaoxq@hadoop105 datas]$ echo 'wukong' >> hive.log 
[xiaoxq@hadoop105 datas]$ echo 'bajie' >> hive.log 

在这里插入图片描述

案例2版本三(企业应用版)

(1) 创建配置文件

[xiaoxq@hadoop105 jobs]$ vim file-flume-hdfs2.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/datas/hive.log


# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path =/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 = 100
#设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a1.sinks.k1.hdfs.rollInterval = 10
#设置每个文件的滚动大小
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a1.sinks.k1.hdfs.rollCount = 0

# 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

注意

  • 对于所有与时间相关的转义序列,Event Header 中必须存在以 “timestamp”的 key(除非hdfs.useLocalTimeStamp 设置为 true,此方法会使用 TimestampInterceptor 自动添加timestamp)。
  • a1.sinks.k1.hdfs.useLocalTimeStamp = true

(2) 运行Flume

[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent -c conf/ -f jobs/file-flume-hdfs2.conf -n a1

(3) 开启 Hadoop 和 Hive 并操作 Hive 产生日志(之前已经添加了内容,故不再操作)

(4)在HDFS上查看文件。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 对比

在这里插入图片描述

配置文件小结:

sink hdfs
hdfs 路径  /flume/%Y%m%d/%H   配置以时间的文件夹
fileType   Datastream  配置能看懂的格式

------滚动文件
rollInterval  按多久滚动一次hdfs文件  默认s为单位  在中小企业配成3600秒
rollSize      按多大滚动一次hdfs文件  默认字节为单位 在企业一般比块(128M)大小 小一些
rollCount     按事件个数滚动hdfs文件  默认单位是event 在企业常设为0
-----滚动文件夹
round 表示对时间戳取整  取整单位由下面两个参数决定  默认是flase
roundValue 表示滚动文件夹的值   默认1
roundUint  表示滚动文件夹的单位 默认s
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个flume的conf文件,请帮我逐行解释一下代码:“#定义三大组件的名称 a.sources = r a.sinks = k1 k2 k3 a.channels = c1 c2 c3 #将数据流复制给所有channel a.sources.r.selector.type = replicating  # 配置Source组件 a.sources.r.type = exec a.sources.r.command = cat /home/bit/novel/novel.csv # kafka a.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink a.sinks.k1.kafka.topic = data a.sinks.k1.kafka.bootstrap.servers = localhost:9092 a.sinks.k1.kafka.flumeBatchSize = 20 a.sinks.k1.kafka.producer.acks = 1 a.sinks.k1.kafka.producer.linger.ms = 1 a.sinks.k1.kafka.producer.compression.type = snappy a.channels.c1.type = memory a.channels.c1.capacity = 100000 a.channels.c1.transactionCapacity = 100 # mysql a.sinks.k2.type =com.us.flume.MysqlSink a.sinks.k2.hostname=localhost a.sinks.k2.port=3306 a.sinks.k2.databaseName=novel a.sinks.k2.tableName=table1 a.sinks.k2.user=bit a.sinks.k2.password=123456 a.channels.c2.type = memory a.channels.c2.capacity = 100000 a.channels.c2.transactionCapactiy = 2000 # hdfs a.sinks.k3.type = hdfs a.sinks.k3.hdfs.path = hdfs://localhost:9000/user/bit/novel #积攒多少个Event才flush到HDFS一次 a.sinks.k3.hdfs.batchSize = 100 #设置文件类型,可支持压缩 a.sinks.k3.hdfs.fileType = DataStream #多久生成一个新的文件 a.sinks.k3.hdfs.rollInterval = 5 a.channels.c3.type = memory a.channels.c3.capacity =100000 a.channels.c3.transactionCapacity = 100 # Bind the source and sink to the channel a.sources.r.channels = c1 c2 c3 a.sinks.k1.channel = c1 a.sinks.k2.channel = c2 a.sinks.k3.channel = c3”
最新发布
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值