Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-9232] [SQL] Duplicate code in JSONRelation #7576

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql.json

import java.io.IOException

import org.apache.hadoop.fs.Path
import org.apache.hadoop.fs.{FileSystem, Path}

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.AnalysisException
Expand Down Expand Up @@ -87,20 +87,7 @@ private[sql] class DefaultSource
case SaveMode.Append =>
sys.error(s"Append mode is not supported by ${this.getClass.getCanonicalName}")
case SaveMode.Overwrite => {
var success: Boolean = false
try {
success = fs.delete(filesystemPath, true)
} catch {
case e: IOException =>
throw new IOException(
s"Unable to clear output directory ${filesystemPath.toString} prior"
+ s" to writing to JSON table:\n${e.toString}")
}
if (!success) {
throw new IOException(
s"Unable to clear output directory ${filesystemPath.toString} prior"
+ s" to writing to JSON table.")
}
JSONRelation.delete(filesystemPath, fs)
true
}
case SaveMode.ErrorIfExists =>
Expand Down Expand Up @@ -195,20 +182,7 @@ private[sql] class JSONRelation(

if (overwrite) {
if (fs.exists(filesystemPath)) {
var success: Boolean = false
try {
success = fs.delete(filesystemPath, true)
} catch {
case e: IOException =>
throw new IOException(
s"Unable to clear output directory ${filesystemPath.toString} prior"
+ s" to writing to JSON table:\n${e.toString}")
}
if (!success) {
throw new IOException(
s"Unable to clear output directory ${filesystemPath.toString} prior"
+ s" to writing to JSON table.")
}
JSONRelation.delete(filesystemPath, fs)
}
// Write the data.
data.toJSON.saveAsTextFile(filesystemPath.toString)
Expand All @@ -228,3 +202,21 @@ private[sql] class JSONRelation(
case _ => false
}
}

private object JSONRelation {

/** Delete the specified directory to overwrite it with new JSON data. */
def delete(dir: Path, fs: FileSystem): Unit = {
var success: Boolean = false
val failMessage = s"Unable to clear output directory $dir prior to writing to JSON table"
try {
success = fs.delete(dir, true /* recursive */)
} catch {
case e: IOException =>
throw new IOException(s"$failMessage\n${e.toString}")
}
if (!success) {
throw new IOException(failMessage)
}
}
}