我正在尝试订购列表,并获取列表中最大的5个元组,然后将其打印出来,这是我一直在使用的代码:
import scala.io.Codec.string2codec
import scala.io.Source
import scala.reflect.io.File
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf
object SimpleWordCount {
def main(args: Array[String]) {
val conf = new SparkConf().setAppName("Simple Word Count")
val sc = new SparkContext(conf)
val test = scala.io.Source.fromFile("/home/cloudera/Books/book1.txt").getLines
val wordCount =
test
.flatMap(_.split("\\W+"))
.foldLeft(Map.empty[String, Int]) {
(count, word) =>
count + (word -> (count.getOrElse(word, 0) + 1))
}
val formatteWordCount =
filtered
.map(tuple => s"${tuple._1} -> ${tuple._2}")
.mkString("\n", "\n", "\n")
当尝试启动代码时,以下几行会出现错误: scala.math.Ordering [B]类型的离散隐式展开 从对象订购中的Tuple9方法开始
.sortBy(x => (x._2))
我还尝试使用.stableSort(k,(x,y)=> x._2
println(s"Final Word Count: $formatteWordCount")
}