在 Spark 中写入 AVRO 文件时应该使用什么 FileOutputCommitter?

时间:2021-04-05 13:30:08

标签: scala apache-spark hadoop avro spark-avro

在 AVRO 中将 RDD 保存到 S3 时,我在控制台中收到以下警告:

<块引用>

使用标准 FileOutputCommitter 提交工作。这很慢,而且可能不安全。

我一直无法找到一个简单的隐式,例如 saveAsAvroFile,因此我四处挖掘并得出了这个结论:

import org.apache.avro.Schema
import org.apache.avro.mapred.AvroKey
import org.apache.avro.mapreduce.{AvroJob, AvroKeyOutputFormat}
import org.apache.hadoop.io.NullWritable
import org.apache.hadoop.mapreduce.Job
import org.apache.spark.rdd.RDD

object AvroUtil {

  def write[T](
      path: String,
      schema: Schema,
      avroRdd: RDD[T],
      job: Job = Job.getInstance()): Unit = {
    val intermediateRdd = avroRdd.mapPartitions(
      f = (iter: Iterator[T]) => iter.map(new AvroKey(_) -> NullWritable.get()),
      preservesPartitioning = true
    )

    job.getConfiguration.set("avro.output.codec", "snappy")
    job.getConfiguration.set("mapreduce.output.fileoutputformat.compress", "true")

    AvroJob.setOutputKeySchema(job, schema)

    intermediateRdd.saveAsNewAPIHadoopFile(
      path,
      classOf[AvroKey[T]],
      classOf[NullWritable],
      classOf[AvroKeyOutputFormat[T]],
      job.getConfiguration
    )
  }
}

我很困惑,因为我看不出什么是不正确的,因为 AVRO 文件似乎正确输出。

1 个答案:

答案 0 :(得分:0)

您可以通过实现自己的 OutputFileCommitter 来覆盖现有 FileOutputCommitter 的行为,使其更加高效和安全。

按照此link,作者已通过示例进行了类似解释。