Skip to content

Commit f4785f5

Browse files
Andrew Orrxin
authored andcommitted
[SPARK-9232] [SQL] Duplicate code in JSONRelation
Author: Andrew Or <[email protected]> Closes apache#7576 from andrewor14/clean-up-json-relation and squashes the following commits: ea80803 [Andrew Or] Clean up duplicate code
1 parent 63f4bcc commit f4785f5

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

sql/core/src/main/scala/org/apache/spark/sql/json/JSONRelation.scala

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package org.apache.spark.sql.json
1919

2020
import java.io.IOException
2121

22-
import org.apache.hadoop.fs.Path
22+
import org.apache.hadoop.fs.{FileSystem, Path}
2323

2424
import org.apache.spark.rdd.RDD
2525
import org.apache.spark.sql.AnalysisException
@@ -87,20 +87,7 @@ private[sql] class DefaultSource
8787
case SaveMode.Append =>
8888
sys.error(s"Append mode is not supported by ${this.getClass.getCanonicalName}")
8989
case SaveMode.Overwrite => {
90-
var success: Boolean = false
91-
try {
92-
success = fs.delete(filesystemPath, true)
93-
} catch {
94-
case e: IOException =>
95-
throw new IOException(
96-
s"Unable to clear output directory ${filesystemPath.toString} prior"
97-
+ s" to writing to JSON table:\n${e.toString}")
98-
}
99-
if (!success) {
100-
throw new IOException(
101-
s"Unable to clear output directory ${filesystemPath.toString} prior"
102-
+ s" to writing to JSON table.")
103-
}
90+
JSONRelation.delete(filesystemPath, fs)
10491
true
10592
}
10693
case SaveMode.ErrorIfExists =>
@@ -195,20 +182,7 @@ private[sql] class JSONRelation(
195182

196183
if (overwrite) {
197184
if (fs.exists(filesystemPath)) {
198-
var success: Boolean = false
199-
try {
200-
success = fs.delete(filesystemPath, true)
201-
} catch {
202-
case e: IOException =>
203-
throw new IOException(
204-
s"Unable to clear output directory ${filesystemPath.toString} prior"
205-
+ s" to writing to JSON table:\n${e.toString}")
206-
}
207-
if (!success) {
208-
throw new IOException(
209-
s"Unable to clear output directory ${filesystemPath.toString} prior"
210-
+ s" to writing to JSON table.")
211-
}
185+
JSONRelation.delete(filesystemPath, fs)
212186
}
213187
// Write the data.
214188
data.toJSON.saveAsTextFile(filesystemPath.toString)
@@ -228,3 +202,21 @@ private[sql] class JSONRelation(
228202
case _ => false
229203
}
230204
}
205+
206+
private object JSONRelation {
207+
208+
/** Delete the specified directory to overwrite it with new JSON data. */
209+
def delete(dir: Path, fs: FileSystem): Unit = {
210+
var success: Boolean = false
211+
val failMessage = s"Unable to clear output directory $dir prior to writing to JSON table"
212+
try {
213+
success = fs.delete(dir, true /* recursive */)
214+
} catch {
215+
case e: IOException =>
216+
throw new IOException(s"$failMessage\n${e.toString}")
217+
}
218+
if (!success) {
219+
throw new IOException(failMessage)
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)