Flume学习笔记 - Flume监控指定端口,指定文件, 指定目录总结

文章目录

      • 1.1 监控指定端口的数据流 :book:
      • 1.2 实时读取本地文件到HDFS
      • 1.3 实施监控指定目录下的多个文件

1.1 监控指定端口的数据流

使用 Flume 监控节点 cm5 上的 8989 端口,将数据输出到控制台上

配置 :

  1. 创建配置文件

    # 配置Agent的sources,sinks,channels 别名
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    # 配置Source输入源 地址和端口
    a1.sources.r1.type = netcat
    a1.sources.r1.bind = cm5
    a1.sources.r1.port = 8989
    # 配置sink输出目的地的类型为控制台日志
    a1.sinks.k1.type = logger
    # 配置Channels : Channels 容量为1000 可以容纳1000个event,每100个event就提交事务(清空缓存)
    a1.channels.c1.type = memory
    a1.channels.c1.capacity = 1000
    a1.channels.c1.transactionCapacity = 100
    # 将数据源sources 缓存区channels 输出源 sinks 连接
    a1.sources.r1.channels = c1
    a1.sinks.k1.channel = c1
    
  2. 启动监控端口

    flume-ng agent \
    --conf ../conf \ # flume 的配置文件目录
    --conf-file ./flume-netcat.conf \
    --name a1 \
    -Dflume.root.logger==INFO,console
    

1.2 实时读取本地文件到HDFS

监控本地指定的文件, 产生新的内容就添加到HDFS中, 并在控制台显示

配置 :

  1. 创建配置文件
  # Name the components on this agent
   a2.sources = r2
   a2.sinks = k2
   a2.channels = c2
   # Describe/configure the source
   a2.sources.r2.type = exec
   a2.sources.r2.command = tail -F /root/lns/hive.log
   
   Describe the sink
   a2.sinks.k2.type = hdfs
   a2.sinks.k2.hdfs.path = hdfs://cm1:8020/flume/%Y%m%d/%H
   #上传文件的前缀
   a2.sinks.k2.hdfs.filePrefix = logs-
   #是否按照时间滚动文件夹
   a2.sinks.k2.hdfs.round = true
   #多少时间单位创建一个新的文件夹
   a2.sinks.k2.hdfs.roundValue = 1
   #重新定义时间单位
   a2.sinks.k2.hdfs.roundUnit = hour
   #是否使用本地时间戳
   a2.sinks.k2.hdfs.useLocalTimeStamp = true
   #积攒多少个Event才flush到HDFS一次
   a2.sinks.k2.hdfs.batchSize = 1000
   #设置文件类型,可支持压缩
   a2.sinks.k2.hdfs.fileType = DataStream
   #多久生成一个新的文件
   a2.sinks.k2.hdfs.rollInterval = 30
   #设置每个文件的滚动大小
   a2.sinks.k2.hdfs.rollSize = 134217700
   #文件的滚动与Event数量无关
   a2.sinks.k2.hdfs.rollCount = 0
   
   # Use a channel which buffers events in memory
   a2.channels.c2.type = memory
   a2.channels.c2.capacity = 1000
   a2.channels.c2.transactionCapacity = 1000
   
   # Bind the source and sink to the channel
   a2.sources.r2.channels = c2
   a2.sinks.k2.channel = c2

注意 :

对于所有与时间相关的转义序列,Event Header 中必须存在以 “timestamp”的 key(除非

hdfs.useLocalTimeStamp 设置为 true,此方法会使用 TimestampInterceptor 自动添加

timestamp)。

总结 : hdfs.useLocalTimeStamp 最好设置成 True

  1. 执行命令监控日志
  • 注意 : 如果是CDH版的 则不需要拷贝任何jar包, 如果是 Apache版本的可能需要拷贝下面的jar包

    如果无法成功上传到hdfs的情况下

    commons-configuration-1.6.jar、
    hadoop-auth-2.7.2.jar、
    hadoop-common-2.7.2.jar、
    hadoop-hdfs-2.7.2.jar、
    commons-io-2.4.jar、
    htrace-core-3.1.0-incubating.jar拷贝到/opt/module/flume/lib 文件夹下。
    
  • 运行Flume

    flume-ng agent \
    --conf ../conf \
    --conf-file ./flume-exec.conf \
    --name a1 \
    -Dflume.root.logger==INFO,console
    
    
  • 使用 echo 生成一些信息到监控的文件中

    执行 echo "hahha" >> hive.log

  • 执行 tail -F /var/log/flume-ng/flume.log 查看flume的日志

  • 打开 HDFS 的WEB页面就可以看到生成的文件

1.3 实施监控指定目录下的多个文件

需求 : 使用Flume监控指定目录下的新移动进的文件并上传到HDFS

参考 : spooling-directory-source

配置 :

  1. 创建配置文件
# Name the components on this agent
a3.sources = r3
a3.sinks = k3
a3.channels = c3

# 设置数据源类型为 监控
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /root/lns
# 这个 header 是每次有文件变化的时候, 在 flume 监控界面都会有 一个 header{ } 这种的, 一般默认是false 
# header{} 都是空的, 如果设置为true 就是  header{/root/lns}  
a3.sources.r3.fileHeader = true
# 为移入监控目录的文件增加一个后缀
# 比如 原本是 1.txt => 移入 /root/lns 变成 1.txt.log
a3.sources.r3.fileSuffix = .log
#忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)

# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://cm1:8020/flume-dir/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = logs-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a3.sinks.k3.hdfs.batchSize = 1000
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 30
#设置每个文件的滚动大小
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a3.sinks.k3.hdfs.rollCount = 0

# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 1000

# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3
  1. 执行命令 开启监控
flume-ng agent \
--conf ../conf \
--conf-file ./dir-flume-hdfs.conf \
--name a3
  • 首先创建一系列的文件

    vim 1.txt 
    > 添加 java python
    > 保存
    cp 1.txt 2.txt
    cp 1.txt 3.txt
    
  • 将文件移入监控目录

  • 查看 hdfs 指定的目录即可查看

  1. 补充

    说明:在使用 Spooling Directory Source 时

    不要在监控目录中创建并持续修改文件

    上传完成的文件会默认以.COMPLETED 结尾(可以自己设置)

    被监控文件夹每 500 毫秒扫描一次文件变动

  • 将文件移入监控目录

  • 查看 hdfs 指定的目录即可查看

  1. 补充

    说明:在使用 Spooling Directory Source 时

    不要在监控目录中创建并持续修改文件

    上传完成的文件会默认以.COMPLETED 结尾(可以自己设置)

    被监控文件夹每 500 毫秒扫描一次文件变动

你可能感兴趣的:(大数据学习笔记)