[英]Getting the largest value in sorted rdd using scala/spark
我有类似的日志:
ERROR: Error fetching remote repo 'origin'
...
ERROR: SVN Problem
..
ERROR: Error fetching remote repo 'origin'
ERROR: Error fetching remote repo 'origin'
我编写了以下函数,根据错误发生的次数对错误进行排序:
val getErrorLines=lines.filter(value=>value.startsWith("ERROR"))
val mappedErrors=getErrorLines.map((s => {
val substrings = s.split(":")
(substrings(1), substrings(0))
})).map(value=>(value,1)).reduceByKey(_+_).sortBy(_._2, false, 1)
我能够根据错误发生的次数获取错误的排序列表:
(( Error fetching remote repo 'origin',ERROR),5)
(( SVN Problem,ERROR),1)
但是我只希望出现的最高错误是:
(( Error fetching remote repo 'origin',ERROR),5)
我通过管道传递了top()函数,但它仍然给了我:
(( SVN Problem,ERROR),1)
是否有其他函数可以根据该值使我出现最多?
最简单的解决方案是swap
val substrings = sc.parallelize(Seq(
(("Error fetching remote repo 'origin'", "ERROR"), 5),
(("SVN Problem", "ERROR"), 1)
))
substrings.map(_.swap).top(1)
// Array[(Int, (String, String))] = Array((5,(Error fetching remote repo 'origin',ERROR)))
我会first
使用:
val mostFrequentError =
s.split(":")(substrings(1), substrings(0))}))
.map(value=>(value,1))
.reduceByKey(_+_)
.sortBy(_._2, false, 1)
.first()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.