saveAsTextFile原理-源码(spark3.0)

本文深入探讨了Spark 3.0中`saveAsTextFile`的实现原理,通过`mapPartitions`将数据转换为Text格式,然后利用MapPartitionRDD调用`SaveAsHadoopFile`方法。在这个过程中,设置了key-value对、压缩格式等,并使用PairRDDFunctions进行操作。最终,借助SparkHadoopWriter执行分区数据的写入任务,每个分区写完后会进行提交。
摘要由CSDN通过智能技术生成

       用mapPartitions函数将数据封装成Text(hadoop的数据类型),返回的是MapPartitionRDD,在调用SaveAsHadoopFile

/**
   * TODO:存储数据到文件中 并指定压缩格式
   * Save this RDD as a compressed text file, using string representations of elements.
   */
  def saveAsTextFile(path: String, codec: Class[_ <: CompressionCodec]): Unit = withScope {
    // TODO:mapPartitions 返回的是MapPartitionRDD
    this.mapPartitions { iter =>
      // TODO:将数据封装成Text  是hadoop的格式
      val text = new Text()
      iter.map { x =>
        require(x != null, "text files do not allow null rows")
        text.set(x.toString)
        (NullWritable.get(), text)
      }
      // TODO:TextOutputFormat
    }.saveAsHadoopFile[TextOutputFormat[NullWritable, Text]](path, codec)
  }

      设置key value  压缩格式等PairRDDFunctions类

  /**
   * Output the RDD to any Hadoop-supported file system, using a Hadoop `OutputFormat` class
   * supporting the key and value types K and V in this RDD. Compress the result with the
   * supplied codec.
   */
  def saveAsHadoopFile[F <: OutputFormat[K, V]](
      path: String,
      codec: Class[_ <: CompressionCodec])(implicit fm: ClassTag[F]): Unit = self.withScope {
    val runtimeClass = fm.runtimeClass
    // TODO:设置路径 key类型 value类型 压缩格式等
    saveAsHadoopFile(path, keyClass, valueClass, runtimeClass.asInstanceOf[Class[F]], codec)
  }

  /**
   * Output the RDD to any Hadoop-supported file system, using a Hadoop `OutputFormat` class
   * supporting the key and value types K and V in this RDD. Compress with the supplied codec.
   */
  def saveAsHadoopFile(
      path: String,
      keyClass: Class[_],
      valueClass: Class[_],
      outputFormatClass: Class[_ <: OutputFormat[_, _]],
      codec: Class[_ <: CompressionCodec]): Unit = self.withScope {
    saveAsHadoopFile(path, keyClass, valueClass, outputFormatClass,
      new JobConf(self.context.hadoopConfiguration), Option(codec))
  }
/**
   * 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

best啊李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值