Flume-实时监控多个文件的追加内容(TailDir Source)

分手后的思念是犯贱 2023-01-23 13:59 543阅读 0赞

实时监控目录下的多个追加文件(断点续传)

exec-source适用于监控一个实时追加的文件,但不能保证数据不丢失;spooldir-source能够保证数据不丢失,且能够实现断点续传,但延迟略高,不能实时监控;而taildir-source即能够实现断电续传,又可以保证数据不丢失,还能够实时监控,并可以同时监控多个文件夹中的多个文件。

分析:使用Flume监控整个目录的实时追加文件,并打印到控制台输出

在这里插入图片描述

先创建好需要监控的文件。

配置文件如下:

  1. # Name the components on this agent
  2. a1.sources = r1
  3. a1.sinks = k1
  4. a1.channels = c1
  5. # Describe/configure the source
  6. a1.sources.r1.type = TAILDIR
  7. # 监控的文件夹可以配置多个,文件也可以配置多个
  8. # 也可以使用正则表达式(注意不会匹配文件系统的目录)只是用来匹配文件名
  9. # 这里如果都写f1的话下面的会覆盖上面的
  10. a1.sources.r1.filegroups = f1 f2
  11. a1.sources.r1.filegroups.f1 = /home/bd/tmp/tailDirTest/log1
  12. a1.sources.r1.filegroups.f2 = /home/bd/tmp/tailDirTest/log2
  13. # 正则表达式方式如下
  14. # a1.sources.r1.filegroups = f1
  15. # a1.sources.r1.filegroups.f1 = /home/bd/tmp/tailDirTest/log.*
  16. # 记录已完成的文件的位置信息,是一个json文件
  17. a1.sources.r1.positionFile = /home/bd/tmp/tailDirPosition/position.json
  18. # Describe the sink
  19. a1.sinks.k1.type = logger
  20. # Use a channel which buffers events in memory
  21. a1.channels.c1.type = memory
  22. a1.channels.c1.capacity = 1000
  23. a1.channels.c1.transactionCapacity = 100
  24. # Bind the source and sink to the channel
  25. a1.sources.r1.channels = c1
  26. a1.sinks.k1.channel = c1

运行flume,初始情况下两个文件中均没有数据,我们查看position内容如下

  1. [{"inode":115,"pos":0,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":0,"file":"/home/bd/tmp/tailDirTest/log2"}]

为log1加入hello,为log2加入hello

在这里插入图片描述

此时position中的内容如下:

  1. [{"inode":115,"pos":6,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":6,"file":"/home/bd/tmp/tailDirTest/log2"}]

然后将flume关闭,关闭后向log1中加入word,log2中加入hello和word,再开启flume,结果如下:

在这里插入图片描述

  1. [{"inode":115,"pos":11,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":17,"file":"/home/bd/tmp/tailDirTest/log2"}]

可以看到flume重新连接之后,实现了断点续传。

发表评论

表情:
评论列表 (有 0 条评论,543人围观)

还没有评论,来说两句吧...

相关阅读