最近,要对每条log进行日期过滤,由于log日期是这种样式的“2018-08-03 00:00:00”,需要在过滤的时候将其转化成long类型进行比较筛选,这时候就要用到SimpleDateFormat了,但是,由于rdd有多个分区,各个分区并行执行,考虑到线程安全和资源问题,最后使用了第三方jar包的FastDateFormat
val dateFormat = FastDateFormat.getInstance("yyyy年MM月dd日,E,HH:mm:ss")
def filterByTime(fields: Array[String], startTime: Long, endTime: Long): Boolean = {
val time = fields(1)
val logTime = dateFormat.parse(time).getTime
logTime >= startTime && logTime < endTime
}
本文介绍如何使用FastDateFormat库优化对大规模日志数据的时间过滤过程,通过将日期字符串转化为long类型,实现高效的时间范围筛选,解决多分区并行执行时的线程安全和资源消耗问题。
2万+

被折叠的 条评论
为什么被折叠?



