【问题标题】:Specifying the output file name in Apache Spark在 Apache Spark 中指定输出文件名
【发布时间】:2019-08-15 23:22:22
【问题描述】:

我正在尝试迁移到 PySpark 的 MapReduce 作业。有什么方法可以定义输出文件的名称,而不是获取part-xxxxx

在 MR 中,我使用 org.apache.hadoop.mapred.lib.MultipleTextOutputFormat 类来实现这一点,

PS:我确实尝试了saveAsTextFile() 方法。例如:

lines = sc.textFile(filesToProcessStr)
counts = lines.flatMap(lambda x: re.split('[\s&]', x.strip()))\
.saveAsTextFile("/user/itsjeevs/mymr-output")

这将创建相同的part-0000 文件。

[13:46:25] [spark] $ hadoop fs -ls /user/itsjeevs/mymr-output/
Found 3 items
-rw-r-----   2 itsjeevs itsjeevs          0 2014-08-13 13:46 /user/itsjeevs/mymr-output/_SUCCESS
-rw-r--r--   2 itsjeevs itsjeevs  101819636 2014-08-13 13:46 /user/itsjeevs/mymr-output/part-00000
-rw-r--r--   2 itsjeevs itsjeevs   17682682 2014-08-13 13:46 /user/itsjeevs/mymr-output/part-00001

编辑

最近阅读了the article,这将使 Spark 用户的生活更加轻松。

【问题讨论】:

  • 你是如何在python中导入MultipleTextOutputFormat的?

标签: python apache-spark


【解决方案1】:

Spark 也在后台使用 Hadoop,因此您可能会得到您想要的。这就是saveAsTextFile 的实现方式:

def saveAsTextFile(path: String) {
  this.map(x => (NullWritable.get(), new Text(x.toString)))
    .saveAsHadoopFile[TextOutputFormat[NullWritable, Text]](path)
}

您可以将自定义的OutputFormat 传递给saveAsHadoopFile。我不知道如何从 Python 中做到这一点。抱歉回答不完整。

【讨论】:

  • 谢谢丹尼尔。这绝对是一个开始的地方。暂时将其标记为已接受。
  • @Jeevs 你知道如何为输出文件提供自定义名称吗?
  • 从那时起,我对 Hadoop 有了更全面的了解。您可以使用自定义OutputFormat 来做到这一点,但这需要相当多的努力。文件名来自文件写入系统的相当深处。我鼓励您直接接受文件名。也许在写完之后重命名它们。
【解决方案2】:

您的输出文件将如下所示;

我的文件名-r-00000 我的文件名-r-00001

        SparkConf sparkConf = new SparkConf().setAppName("WCSYNC-FileCompressor-ClusterSaver");
        SparkContext sc = new SparkContext(sparkConf);
            JavaSparkContext context = new JavaSparkContext(sc)
context.hadoopConfiguration().set("mapreduce.output.basename", "myfilename");




saveAsNewAPIHadoopFile(outputpath,
                                Text.class,
                                Text.class,
                                TextOutputFormat.class,
                                context.hadoopConfiguration());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多