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

    https://github.com/apache/spark/pull/2226#discussion_r17625337
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/InsertIntoHiveTable.scala
 ---
    @@ -100,63 +102,139 @@ case class InsertIntoHiveTable(
           obj
       }
     
    +  /**
    +   * since we should get directory of dynamic partition from upstream RDD
    +   * reference the code "serializer.serialize(outputData, standardOI) -> 
dynamicPartPath"
    +   * So The type of the elment in RDD is (Writable, String)
    +   */
       def saveAsHiveFile(
    -      rdd: RDD[Writable],
    +      rdd: RDD[(Writable, String)],
           valueClass: Class[_],
           fileSinkConf: FileSinkDesc,
    -      conf: JobConf,
    -      isCompressed: Boolean) {
    +      conf: SerializableWritable[JobConf],
    +      isCompressed: Boolean,
    +      dynamicPartNum: Int) {
         if (valueClass == null) {
           throw new SparkException("Output value class not set")
         }
    -    conf.setOutputValueClass(valueClass)
    +    conf.value.setOutputValueClass(valueClass)
         if (fileSinkConf.getTableInfo.getOutputFileFormatClassName == null) {
           throw new SparkException("Output format class not set")
         }
         // Doesn't work in Scala 2.9 due to what may be a generics bug
         // TODO: Should we uncomment this for Scala 2.10?
         // conf.setOutputFormat(outputFormatClass)
    -    conf.set("mapred.output.format.class", 
fileSinkConf.getTableInfo.getOutputFileFormatClassName)
    +    conf.value.set("mapred.output.format.class",
    +      fileSinkConf.getTableInfo.getOutputFileFormatClassName)
         if (isCompressed) {
           // Please note that isCompressed, "mapred.output.compress", 
"mapred.output.compression.codec",
           // and "mapred.output.compression.type" have no impact on ORC 
because it uses table properties
           // to store compression information.
    -      conf.set("mapred.output.compress", "true")
    +      conf.value.set("mapred.output.compress", "true")
           fileSinkConf.setCompressed(true)
    -      
fileSinkConf.setCompressCodec(conf.get("mapred.output.compression.codec"))
    -      
fileSinkConf.setCompressType(conf.get("mapred.output.compression.type"))
    +      
fileSinkConf.setCompressCodec(conf.value.get("mapred.output.compression.codec"))
    +      
fileSinkConf.setCompressType(conf.value.get("mapred.output.compression.type"))
         }
    -    conf.setOutputCommitter(classOf[FileOutputCommitter])
    -    FileOutputFormat.setOutputPath(
    -      conf,
    -      SparkHiveHadoopWriter.createPathFromString(fileSinkConf.getDirName, 
conf))
    +    conf.value.setOutputCommitter(classOf[FileOutputCommitter])
     
    +    FileOutputFormat.setOutputPath(
    +      conf.value,
    +      SparkHiveHadoopWriter.createPathFromString(fileSinkConf.getDirName, 
conf.value))
         log.debug("Saving as hadoop file of type " + valueClass.getSimpleName)
    +    var writer: SparkHiveHadoopWriter = null
    +    // Map restore writesr for Dynamic Partition
    +    var writerMap: scala.collection.mutable.HashMap[String, 
SparkHiveHadoopWriter] = null
    +    if (dynamicPartNum == 0) {
    +      writer = new SparkHiveHadoopWriter(conf.value, fileSinkConf)
    +      writer.preSetup()
    +      sc.sparkContext.runJob(rdd, writeToFile _)
    +      writer.commitJob()
    +    } else {
    +      writerMap =  new scala.collection.mutable.HashMap[String, 
SparkHiveHadoopWriter]
    +      sc.sparkContext.runJob(rdd, writeToFile _)
    +      for ((k,v) <- writerMap) {
    +        v.commitJob()
    +      }
    +      writerMap.clear()
    +    }
     
    -    val writer = new SparkHiveHadoopWriter(conf, fileSinkConf)
    -    writer.preSetup()
    -
    -    def writeToFile(context: TaskContext, iter: Iterator[Writable]) {
    +    def writeToFile(context: TaskContext, iter: Iterator[(Writable, 
String)]) {
           // Hadoop wants a 32-bit task attempt ID, so if ours is bigger than 
Int.MaxValue, roll it
           // around by taking a mod. We expect that no task will be attempted 
2 billion times.
           val attemptNumber = (context.attemptId % Int.MaxValue).toInt
     
    -      writer.setup(context.stageId, context.partitionId, attemptNumber)
    -      writer.open()
    -
    -      var count = 0
    -      while(iter.hasNext) {
    -        val record = iter.next()
    -        count += 1
    -        writer.write(record)
    +      if (dynamicPartNum == 0) { // for All static partition
    +        writer.setup(context.stageId, context.partitionId, attemptNumber)
    +        writer.open()
    +        // writer for Dynamic Partition
    +        while(iter.hasNext) {
    +          val record = iter.next()
    +          writer.write(record._1)
    +        }
    +        writer.close()
    +        writer.commit()
    +      } else { // if there is dynamic Partition
    +        while(iter.hasNext) {
    +          val record = iter.next()
    +          val location = fileSinkConf.getDirName
    +          val partLocation = location + record._2 // different writer 
related with different file
    +          def createNewWriter(): SparkHiveHadoopWriter = {
    +            val tempWriter = new SparkHiveHadoopWriter(conf.value,
    +              new FileSinkDesc(partLocation, fileSinkConf.getTableInfo, 
false))
    --- End diff --
    
    We can call `.clone()` and then `.setDirName`, so that we don't simply 
override `compressed` to `false` here. 


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to