2017-05-19 17 views
0

同じパーティションがあるかもしれないし、そうでないかもしれない2つのrddを圧縮する必要があるので、再パーティション化の方法を探します。私はジッパーをしながら注文を維持する必要があり、私は一般的に再分割シャッフルを知っています。しかし、以下のコードはrepartiton(1)がrddをシャッフルしていないことを示しています。今の時だけですか、毎回保証することができますか?repartition(1)は常に注文を維持していますか?

再分割(1)は.collectと似ていますが、どちらもrddを単一ノードにするためですか?あなたが実際​​3210方法のジョブを実行している低い値できrepartition(1パーティションの最小可能数)

scala> var k = sc.parallelize((1 to 100),4) 
k: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:27 

scala> k.repartition(2) 
res0: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[4] at repartition at <console>:30 

scala> res0.collect 
res1: Array[Int] = Array(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99) 


scala> var l = sc.parallelize((1 to 100),4) 
l: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[11] at parallelize at <console>:27 

scala> l.repartition(1) 
res5: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[15] at repartition at <console>:30 

scala> .collect 
res6: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100) 

答えて

1

repartition方法の

ドキュメンテーション文字列(および実装)は、私が与えることができる任意の応答より明確になります。

/** 
* Return a new RDD that has exactly numPartitions partitions. 
* 
* Can increase or decrease the level of parallelism in this RDD. Internally, this uses 
* a shuffle to redistribute data. 
* 
* If you are decreasing the number of partitions in this RDD, consider using `coalesce`, 
* which can avoid performing a shuffle. 
*/ 
def repartition(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T] = withScope { 
    coalesce(numPartitions, shuffle = true) 
} 

しかし、あなたはzipに計画している場合、ビュンはとにかくシャッフルすることを検討してください。 実際にがパーティション化を制御したい場合は、手動でパーティションを再分割します(おそらく、PairRDDの場合はカスタムパーティショナーを使用します)。次に、パーティション化を維持することを指定するzipPartitionsを使用します。多くの場合

は、しかし、あなただけの次のzipデフォルトの実装、に固執することもできます。

/** 
* Zips this RDD with another one, returning key-value pairs with the first element in each RDD, 
* second element in each RDD, etc. Assumes that the two RDDs have the *same number of 
* partitions* and the *same number of elements in each partition* (e.g. one was made through 
* a map on the other). 
*/ 
def zip[U: ClassTag](other: RDD[U]): RDD[(T, U)] = withScope { 
    zipPartitions(other, preservesPartitioning = false) { (thisIter, otherIter) => 
    new Iterator[(T, U)] { 
     def hasNext: Boolean = (thisIter.hasNext, otherIter.hasNext) match { 
     case (true, true) => true 
     case (false, false) => false 
     case _ => throw new SparkException("Can only zip RDDs with " + 
      "same number of elements in each partition") 
     } 
     def next(): (T, U) = (thisIter.next(), otherIter.next()) 
    } 
    } 
} 

を見てわかるように、zipはすでに正確に何をしたいん。

+0

私の懸念事項は、パーティションの順序だけでなく、自分のrdd自体の順序です。私は再分割(1)(または合体(1))を使用すると、要素の順序は維持されますか?私が質問に入れたコードスニペットでは、再分割(1)の順序が維持されていますが、これが常に維持されることを保証できますか? –

+0

あなたは_shuffling_という意味で、配列_shufflingのようにそれを意味する印象を受けている場合がありますか? Spark/Hadoop用語では、_shuffling_はデータがネットワーク上を移動することを必要とする計算を指します。たとえば、キーで項目に還元を適用すると、同じキーを持つすべての項目が特定の区画に移動し、したがってシャッフリングされます。この文脈では、_shuffling_はソートを中断しませんが、それは一般的にローカルコレクションに適用されたものとは異なる意味で同じ用語です。 – stefanobaghino

関連する問題