take top N after groupBy and treat them as RDD

后端 未结 4 469
死守一世寂寞
死守一世寂寞 2020-12-10 08:09

I\'d like to get top N items after groupByKey of RDD and convert the type of topNPerGroup(in the below) to RDD[(String, Int)]

4条回答
  •  抹茶落季
    2020-12-10 08:23

    Your question is a little confusing, but I think this does what you're looking for:

    val flattenedTopNPerGroup = 
        topNPerGroup.flatMap({case (key, numbers) => numbers.map(key -> _)})
    

    and in the repl it prints out what you want:

    flattenedTopNPerGroup.collect.foreach(println)
    (foo,3)
    (foo,2)
    (bar,6)
    (bar,5)
    

提交回复
热议问题