Skip to content

Commit fa40db0

Browse files
committed
Add an Ordering for NullWritable to make the compiler generate same byte codes for RDD
1 parent 9804a75 commit fa40db0

File tree

1 file changed

+12
-0
lines changed
  • core/src/main/scala/org/apache/spark/rdd

1 file changed

+12
-0
lines changed

core/src/main/scala/org/apache/spark/rdd/RDD.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,14 @@ abstract class RDD[T: ClassTag](
11741174
* Save this RDD as a text file, using string representations of elements.
11751175
*/
11761176
def saveAsTextFile(path: String) {
1177+
// https://issues.apache.org/jira/browse/SPARK-2075
1178+
// NullWritable is a Comparable rather than Comparable[NullWritable] in Hadoop 1.+,
1179+
// so the compiler cannot find an implicit Ordering for it. It will generate different
1180+
// anonymous classes for `saveAsTextFile` in Hadoop 1.+ and Hadoop 2.+. Therefore, here we
1181+
// provide an Ordering for NullWritable so that the compiler will generate same codes.
1182+
implicit val nullWritableOrdering = new Ordering[NullWritable] {
1183+
override def compare(x: NullWritable, y: NullWritable): Int = 0
1184+
}
11771185
this.map(x => (NullWritable.get(), new Text(x.toString)))
11781186
.saveAsHadoopFile[TextOutputFormat[NullWritable, Text]](path)
11791187
}
@@ -1182,6 +1190,10 @@ abstract class RDD[T: ClassTag](
11821190
* Save this RDD as a compressed text file, using string representations of elements.
11831191
*/
11841192
def saveAsTextFile(path: String, codec: Class[_ <: CompressionCodec]) {
1193+
// https://issues.apache.org/jira/browse/SPARK-2075
1194+
implicit val nullWritableOrdering = new Ordering[NullWritable] {
1195+
override def compare(x: NullWritable, y: NullWritable): Int = 0
1196+
}
11851197
this.map(x => (NullWritable.get(), new Text(x.toString)))
11861198
.saveAsHadoopFile[TextOutputFormat[NullWritable, Text]](path, codec)
11871199
}

0 commit comments

Comments
 (0)