如何排序我的Spark元组结果使用值降序 [英] how to order my tuple of spark results descending order using value

查看:327
本文介绍了如何排序我的Spark元组结果使用值降序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spark和Scala的新手.我需要将我的结果计数元组排序为(课程,计数),降序排列.我放在下面

I am new to spark and scala. I need to order my result count tuple which is like (course, count) into descending order. I put like below

 val results = ratings.countByValue()
 val sortedResults = results.toSeq.sortBy(_._2)

但仍然无法正常工作.以上述方式,它将按计数以升序对结果进行排序.但是我需要按降序排列.有人可以帮我吗?

But still its't working. In the above way it will sort the results by count with ascending order. But I need to have it in descending order. Can anybody please help me.

结果将如下所示

(History, 12100),
(Music, 13200),
(Drama, 143000)

但是我需要像下面这样显示

But I need to display it like below

(Drama, 143000),
(Music, 13200),
(History, 12100)

谢谢

推荐答案

您几乎完成了!您需要为降序添加其他参数,如

You have almost done it! you need add additional parameter for descending order as RDD sortBy() method arrange elements in ascending order by default.

val results = ratings.countByValue()
val sortedRdd = results.sortBy(_._2, false)

//Just to display results from RDD
println(sortedRdd.collect().toList)

这篇关于如何排序我的Spark元组结果使用值降序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆