转自:
http://www.cnblogs.com/lxf20061900/p/4014281.html
Flume-NG中的hdfs sink的路径名(对应参数"hdfs.path",不允许为空)以及文件前缀(对应参数"hdfs.filePrefix")支持正则解析时间戳自动按时间创建目录及文件前缀。
在实际使用中发现Flume内置的基于正则的解析方式非常耗时,有非常大的提升空间。如果你不需要配置按时间戳解析时间,那这篇文章对你用处不大,hdfs sink对应的解析时间戳的代码位于org.apache.flume.sink.hdfs.HDFSEventSink的process()方法中,涉及两句代码:
1 // reconstruct the path name by substituting place holders 2 String realPath = BucketPath.escapeString(filePath, event.getHeaders(), 3 timeZone, needRounding, roundUnit, roundValue, useLocalTime); 4 String realName = BucketPath.escapeString(fileName, event.getHeaders(), 5 timeZone, needRounding, roundUnit, roundValue, useLocalTime);
其中,realPath是正则解析时间戳之后的完整路径名,filePath参数就是配置文件中的"hdfs.path";realName就是正则解析时间戳之后的文件名前缀,fileName参数就是配置文件中的"hdfs.filePrefix"。其他参数都相同,event.getHeaders()是一个Map里面有时间戳(可以通过interceptor、自定义、使用hdfs sink的useLocalTimeStamp参数三种方式来设置),其他参数是时区、是否四舍五入以及时间单位等。
BucketPath.escapeString这个方法就是正则解析时间戳所在,具体代码我们不再分析,现在我们编写一个程序测试一下BucketPath.escapeString这个方法的性能,运行这个测试类要么在源码中:
public class Test { public static void main(String[] args) { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("timestamp", Long.toString(System.currentTimeMillis())); String filePath = "hdfs://xxxx.com:8020/data/flume/%Y-%m-%d"; String fileName = "%H-%M"; long start = System.cur