簡體   English   中英

如何從 RDD Spark Scala 中刪除最后一行

[英]How to remove last line from RDD Spark Scala

我想使用.mapPartitionsWithIndex function 從 RDD 中刪除最后一行。

我試過下面的代碼

val withoutFooter = rdd.mapPartitionsWithIndex { (idx, iter) =>     
     if (idx == noOfTotalPartitions) {
         iter.drop(size - 1)
     }
     else iter 
}

但無法得到正確的結果。

drop將刪除前 n 個元素並返回剩余的元素

在這里閱讀更多https://stackoverflow.com/a/51792161/6556191

下面的代碼對我有用

val rdd = sc.parallelize(Array(1,2,3,4,5,6,7,8,9),4)

val lastPartitionIndex = rdd.getNumPartitions - 1

rdd.mapPartitionsWithIndex { (idx, iter) => 
    var reti = iter
    if (idx == lastPartitionIndex) {
        var lastPart = iter.toArray
        reti = lastPart.slice(0, lastPart.length-1).toIterator
    }
    reti
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM