scala - 如何在 Spark Scala 中按值排序
问题描述
我有一个键值对,我需要按值按降序返回前 10 个元素。正如您从下面的实际输出中看到的那样,它通过键而不是值(在本例中为 ascii 字符代码)为我提供了最高值。
例如:
//Input:
(the, 5),
(is, 10),
(me, 1)
//Expected Output:
(is, 10),
(the, 5),
(me, 1)
//Actual Output:
(the, 5),
(me, 1),
(is, 10)
我的功能:
def getActiveTaxis(taxiLines: RDD[Array[String]]): Array[(String, Int)] = {
// Removing set up code for brevity
val counts = keys.map(x => (x, 1))
val sortedResult = counts.reduceByKey((a, b) => a + b).sortBy(_._2, false)
sortedResult.top(10)
}
解决方案
推荐阅读
- powershell - 如何从非集群节点远程捕获集群信息
- jquery - 如何通过单击数据表中每个(行)的第一个元素来打开模式
- excel - 将相同的字符串或变量写入同一工作表中的多个单元格
- plsql - 尝试回答挑战时出现错误
- python - 在 Python (pywin32) 中发送带有附件的 Outlook 电子邮件
- flutter - 如何在 StaggeredGridView 中将磁贴居中
- simulation - 泵入 ClosedVolume 的 MassFlowSource 对 Dymola 中的体积压力和温度没有影响,为什么?
- json - RestTemplate 运行补丁方法时如何删除未使用的属性?
- bash - 将函数从 zsh 导出到 bash 脚本
- java - 如何检索 GL2 实例?