Flume-实时监控多个文件的追加内容(TailDir Source) 分手后的思念是犯贱 2023-01-23 13:59 425阅读 0赞 ## 实时监控目录下的多个追加文件(断点续传) ## exec-source适用于监控一个实时追加的文件,但不能保证数据不丢失;spooldir-source能够保证数据不丢失,且能够实现断点续传,但延迟略高,不能实时监控;而taildir-source即能够实现断电续传,又可以保证数据不丢失,还能够实时监控,并可以同时监控多个文件夹中的多个文件。 分析:使用Flume监控整个目录的实时追加文件,并打印到控制台输出 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NsZWFybHhq_size_16_color_FFFFFF_t_70] 先创建好需要监控的文件。 配置文件如下: # Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = TAILDIR # 监控的文件夹可以配置多个,文件也可以配置多个 # 也可以使用正则表达式(注意不会匹配文件系统的目录)只是用来匹配文件名 # 这里如果都写f1的话下面的会覆盖上面的 a1.sources.r1.filegroups = f1 f2 a1.sources.r1.filegroups.f1 = /home/bd/tmp/tailDirTest/log1 a1.sources.r1.filegroups.f2 = /home/bd/tmp/tailDirTest/log2 # 正则表达式方式如下 # a1.sources.r1.filegroups = f1 # a1.sources.r1.filegroups.f1 = /home/bd/tmp/tailDirTest/log.* # 记录已完成的文件的位置信息,是一个json文件 a1.sources.r1.positionFile = /home/bd/tmp/tailDirPosition/position.json # 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 运行flume,初始情况下两个文件中均没有数据,我们查看position内容如下 [{"inode":115,"pos":0,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":0,"file":"/home/bd/tmp/tailDirTest/log2"}] 为log1加入hello,为log2加入hello ![在这里插入图片描述][202107151459297.png] 此时position中的内容如下: [{"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,结果如下: ![在这里插入图片描述][20210715145935275.png] [{"inode":115,"pos":11,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":17,"file":"/home/bd/tmp/tailDirTest/log2"}] 可以看到flume重新连接之后,实现了断点续传。 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NsZWFybHhq_size_16_color_FFFFFF_t_70]: /images/20221004/670180bbf8f349b7b0f5c7fa4600e54d.png [202107151459297.png]: /images/20221004/4dac8977c02e4579bf8046411c57ae15.png [20210715145935275.png]: /images/20221004/33a72864466f486cb27c9d8c86f5cfd1.png
还没有评论,来说两句吧...