Python takeOrdered 降序 Pyspark
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30787635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
takeOrdered descending Pyspark
提问by arj
i would like to sort K/V pairs by values and then take the biggest five values. I managed to do this with reverting K/V with first map, sort in descending order with FALSE, and then reverse key.value to the original (second map) and then take the first 5 that are the bigget, the code is this:
我想按值对 K/V 对进行排序,然后取最大的五个值。我设法通过使用第一张地图恢复 K/V 来做到这一点,使用 FALSE 按降序排序,然后将 key.value 反转到原始(第二张地图),然后取前 5 个是最大的,代码是这样的:
RDD.map(lambda x:(x[1],x[0])).sortByKey(False).map(lambda x:(x[1],x[0])).take(5)
i know there is a takeOrdered action on pySpark, but i only managed to sort on values (and not on key), i don't know how to get a descending sorting:
我知道在 pySpark 上有一个 takeOrdered 操作,但我只设法对值(而不是键)进行排序,我不知道如何进行降序排序:
RDD.takeOrdered(5,key = lambda x: x[1])
采纳答案by aatishk
Sort by keys (ascending):
按键排序(升序):
RDD.takeOrdered(5, key = lambda x: x[0])
Sort by keys (descending):
按键排序(降序):
RDD.takeOrdered(5, key = lambda x: -x[0])
Sort by values (ascending):
按值排序(升序):
RDD.takeOrdered(5, key = lambda x: x[1])
Sort by values (descending):
按值排序(降序):
RDD.takeOrdered(5, key = lambda x: -x[1])