Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8010#discussion_r36512629
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/InsertIntoHadoopFsRelation.scala
 ---
    @@ -0,0 +1,179 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.execution.datasources
    +
    +import java.io.IOException
    +import java.util.{Date, UUID}
    +
    +import scala.collection.JavaConversions.asScalaIterator
    +
    +import org.apache.hadoop.fs.Path
    +import org.apache.hadoop.mapreduce._
    +import org.apache.hadoop.mapreduce.lib.output.{FileOutputCommitter => 
MapReduceFileOutputCommitter, FileOutputFormat}
    +import org.apache.spark._
    +import org.apache.spark.mapred.SparkHadoopMapRedUtil
    +import org.apache.spark.mapreduce.SparkHadoopMapReduceUtil
    +import org.apache.spark.sql._
    +import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.expressions.codegen.GenerateProjection
    +import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Project}
    +import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
    +import org.apache.spark.sql.execution.{RunnableCommand, SQLExecution}
    +import org.apache.spark.sql.sources._
    +import org.apache.spark.sql.types.StringType
    +import org.apache.spark.util.{Utils, SerializableConfiguration}
    +
    +
    +/**
    + * A command for writing data to a [[HadoopFsRelation]].  Supports both 
overwriting and appending.
    + * Writing to dynamic partitions is also supported.  Each 
[[InsertIntoHadoopFsRelation]] issues a
    + * single write job, and owns a UUID that identifies this job.  Each 
concrete implementation of
    + * [[HadoopFsRelation]] should use this UUID together with task id to 
generate unique file path for
    + * each task output file.  This UUID is passed to executor side via a 
property named
    + * `spark.sql.sources.writeJobUUID`.
    + *
    + * Different writer containers, [[DefaultWriterContainer]] and 
[[DynamicPartitionWriterContainer]]
    + * are used to write to normal tables and tables with dynamic partitions.
    + *
    + * Basic work flow of this command is:
    + *
    + *   1. Driver side setup, including output committer initialization and 
data source specific
    + *      preparation work for the write job to be issued.
    + *   2. Issues a write job consists of one or more executor side tasks, 
each of which writes all
    + *      rows within an RDD partition.
    + *   3. If no exception is thrown in a task, commits that task, otherwise 
aborts that task;  If any
    + *      exception is thrown during task commitment, also aborts that task.
    + *   4. If all tasks are committed, commit the job, otherwise aborts the 
job;  If any exception is
    + *      thrown during job commitment, also aborts the job.
    + */
    +private[sql] case class InsertIntoHadoopFsRelation(
    +    @transient relation: HadoopFsRelation,
    +    @transient query: LogicalPlan,
    +    mode: SaveMode)
    +  extends RunnableCommand {
    +
    +  override def run(sqlContext: SQLContext): Seq[Row] = {
    +    require(
    +      relation.paths.length == 1,
    +      s"Cannot write to multiple destinations: 
${relation.paths.mkString(",")}")
    +
    +    val hadoopConf = sqlContext.sparkContext.hadoopConfiguration
    +    val outputPath = new Path(relation.paths.head)
    +    val fs = outputPath.getFileSystem(hadoopConf)
    +    val qualifiedOutputPath = outputPath.makeQualified(fs.getUri, 
fs.getWorkingDirectory)
    +
    +    val pathExists = fs.exists(qualifiedOutputPath)
    +    val doInsertion = (mode, pathExists) match {
    +      case (SaveMode.ErrorIfExists, true) =>
    +        throw new AnalysisException(s"path $qualifiedOutputPath already 
exists.")
    +      case (SaveMode.Overwrite, true) =>
    +        Utils.tryOrIOException {
    +          if (!fs.delete(qualifiedOutputPath, true /* recursively */)) {
    +            throw new IOException(s"Unable to clear output " +
    +              s"directory $qualifiedOutputPath prior to writing to it")
    +          }
    +        }
    +        true
    +      case (SaveMode.Append, _) | (SaveMode.Overwrite, _) | 
(SaveMode.ErrorIfExists, false) =>
    +        true
    +      case (SaveMode.Ignore, exists) =>
    +        !exists
    +      case (s, exists) =>
    +        throw new IllegalStateException(s"unsupported save mode $s 
($exists)")
    +    }
    +    // If we are appending data to an existing dir.
    +    val isAppend = pathExists && (mode == SaveMode.Append)
    +
    +    if (doInsertion) {
    +      val job = new Job(hadoopConf)
    +      job.setOutputKeyClass(classOf[Void])
    +      job.setOutputValueClass(classOf[InternalRow])
    +      FileOutputFormat.setOutputPath(job, qualifiedOutputPath)
    +
    +      // We create a DataFrame by applying the schema of relation to the 
data to make sure.
    +      // We are writing data based on the expected schema,
    +
    +      // For partitioned relation r, r.schema's column ordering can be 
different from the column
    +      // ordering of data.logicalPlan (partition columns are all moved 
after data column). We
    +      // need a Project to adjust the ordering, so that inside 
InsertIntoHadoopFsRelation, we can
    +      // safely apply the schema of r.schema to the data.
    +
    +      // TODO: this belongs in the analyzer.
    +      val project = Project(
    +        relation.schema.map(field => new 
UnresolvedAttribute(Seq(field.name))), query)
    +      val queryExecution = DataFrame(sqlContext, project).queryExecution
    +
    +      SQLExecution.withNewExecutionId(sqlContext, queryExecution) {
    +        val df = sqlContext.internalCreateDataFrame(queryExecution.toRdd, 
relation.schema)
    +        val partitionColumns = relation.partitionColumns.fieldNames
    +
    +        // Some pre-flight checks.
    +        require(
    +          df.schema == relation.schema,
    +          s"""DataFrame must have the same schema as the relation to which 
is inserted.
    +             |DataFrame schema: ${df.schema}
    +              |Relation schema: ${relation.schema}
    +          """.stripMargin)
    --- End diff --
    
    Nit: Indentation is off.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to