如何附加到 HDFS 中的同一个文件(spark 2.11) [英] How can I append to same file in HDFS(spark 2.11)

查看:30
本文介绍了如何附加到 HDFS 中的同一个文件(spark 2.11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SparkStreaming 将流数据存储到 HDFS 中,但它不断在新文件中创建,而不是附加到一个文件或几个文件中

I am trying to store Stream Data into HDFS using SparkStreaming,but it Keep creating in new file insted of appending into one single file or few multiple files

如果一直创建n个文件,我觉得效率不会很高

If it keep creating n numbers of files,i feel it won't be much efficient

HDFS 文件系统

代码

lines.foreachRDD(f => {
  if (!f.isEmpty()) {
    val df = f.toDF().coalesce(1)
    df.write.mode(SaveMode.Append).json("hdfs://localhost:9000/MT9")
  }
 })

在我的 pom 中,我使用了各自的依赖项:

In my pom I am using respective dependencies:

  • spark-core_2.11
  • spark-sql_2.11
  • spark-streaming_2.11
  • spark-streaming-kafka-0-10_2.11

推荐答案

正如你已经意识到的 Append 在 Spark 中意味着写入现有目录而不是附加到文件.

As you already realized Append in Spark means write-to-existing-directory not append-to-file.

这是有意的和期望的行为(想想如果进程在附加"过程中失败会发生什么,即使格式和文件系统允许).

This is intentional and desired behavior (think what would happen if process failed in the middle of "appending" even if format and file system allow that).

合并文件等操作应由单独的进程应用(如有必要),以确保正确性和容错性.不幸的是,这需要一个完整的副本,出于明显的原因,这在批量到批量的基础上是不受欢迎的.

Operations like merging files should be applied by a separate process, if necessary at all, which ensures correctness and fault tolerance. Unfortunately this requires a full copy which, for obvious reasons is not desired on batch-to-batch basis.

这篇关于如何附加到 HDFS 中的同一个文件(spark 2.11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆