我有一個鍵、值對,我需要按值降序回傳前10個元素。正如你在下面的實際輸出中所看到的,它給我的是按鍵而不是按值(在這種情況下是按ascii字符代碼)排列的前值。
例如:
//Input:。
(的,5)。)
(是,10)。
(我,1)
//預期輸出:。
(is, 10) 。
(the, 5)。
(我,1)
//Aactual Output:實際輸出:。
(the, 5),
(我,1)。
(is, 10)
我的函式:
def getActiveTaxis( taxiLines: RDD[Array[String]])。) Array[(String, Int) ] = {
//為簡潔起見洗掉了設定代碼。
val counts = keys.map(x => (x, 1)
val sortedResult = counts.reduceByKey((a, b) => a b).sortBy(_._2, false)
sortedResult.top(10)
}
uj5u.com熱心網友回復:
take()將回傳前N個元素,而top()將在根據指定的implicit Ordering[T]對RDD進行排序后回傳前N個元素。
你可以參考top()的實作這里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/309101.html
標籤:
