繁体   English   中英

在 Spark 2.1.0 中读取大文件时出现内存不足错误

[英]Out of Memory Error when Reading large file in Spark 2.1.0

我想使用 spark 将大型(51GB)XML 文件(在外部硬盘驱动器上)读入数据帧(使用spark-xml 插件),进行简单的映射/过滤,重新排序,然后将其作为 CSV 写回磁盘文件。

但是无论我如何调整它,我总是得到一个java.lang.OutOfMemoryError: Java heap space

我想了解为什么不增加分区数会阻止 OOM 错误

难道它不应该将任务拆分成更多的部分,以便每个单独的部分都更小并且不会引起内存问题吗?

(Spark 不可能试图把所有东西都塞进内存中,如果不合适就会崩溃,对吧??)

我尝试过的事情:

  • 在读取和写入时重新分区/合并到(5,000 和 10,000 个分区)数据帧(初始值为 1,604)
  • 使用较少数量的执行程序(6、4,即使有2 个执行程序,我也会收到 OOM 错误!)
  • 减小分割文件的大小(默认看起来是 33MB)
  • 提供大量内存(我拥有的所有)
  • spark.memory.fraction增加到 0.8(默认为 0.6)
  • spark.memory.storageFraction减少到 0.2(默认为 0.5)
  • spark.default.parallelism设置为 30 和 40(我的默认值为 8)
  • spark.files.maxPartitionBytes设置为 64M(默认为 128M)

我所有的代码都在这里(注意我没有缓存任何东西):

val df: DataFrame = spark.sqlContext.read
  .option("mode", "DROPMALFORMED")
  .format("com.databricks.spark.xml")
  .schema(customSchema) // defined previously
  .option("rowTag", "row")
  .load(s"$pathToInputXML")

println(s"\n\nNUM PARTITIONS: ${df.rdd.getNumPartitions}\n\n")
// prints 1604

// i pass `numPartitions` as cli arguments
val df2 = df.coalesce(numPartitions)

// filter and select only the cols i'm interested in
val dsout = df2
  .where( df2.col("_TypeId") === "1" )
  .select(
    df("_Id").as("id"),
    df("_Title").as("title"),
    df("_Body").as("body"),
  ).as[Post]

// regexes to clean the text
val tagPat = "<[^>]+>".r
val angularBracketsPat = "><|>|<"
val whitespacePat = """\s+""".r


// more mapping
dsout
 .map{
  case Post(id,title,body,tags) =>

    val body1 = tagPat.replaceAllIn(body,"")
    val body2 = whitespacePat.replaceAllIn(body1," ")

    Post(id,title.toLowerCase,body2.toLowerCase, tags.split(angularBracketsPat).mkString(","))

}
.orderBy(rand(SEED)) // random sort
.write // write it back to disk
.option("quoteAll", true)
.mode(SaveMode.Overwrite)
.csv(output)

笔记

  • 输入拆分非常小(仅 33MB),那么为什么我不能有 8 个线程每个处理一个拆分? 它真的不应该打破我的记忆(我已经看到

更新我编写了一个较短版本的代码,它只读取文件然后 forEachPartition(println)。

我收到相同的 OOM 错误:

val df: DataFrame = spark.sqlContext.read
  .option("mode", "DROPMALFORMED")
  .format("com.databricks.spark.xml")
  .schema(customSchema)
  .option("rowTag", "row")
  .load(s"$pathToInputXML")
  .repartition(numPartitions)

println(s"\n\nNUM PARTITIONS: ${df.rdd.getNumPartitions}\n\n")

df
  .where(df.col("_PostTypeId") === "1")
  .select(
   df("_Id").as("id"),
   df("_Title").as("title"),
   df("_Body").as("body"),
   df("_Tags").as("tags")
  ).as[Post]
  .map {
    case Post(id, title, body, tags) =>
      Post(id, title.toLowerCase, body.toLowerCase, tags.toLowerCase))
  }
  .foreachPartition { rdd =>
    if (rdd.nonEmpty) {
      println(s"HI! I'm an RDD and I have ${rdd.size} elements!")
    }
  }

PS:我使用的是 spark v 2.1.0。 我的机器有 8 个内核和 16 GB 内存。

我在运行 spark-shell 时遇到了这个错误,因此我将驱动程序内存增加到一个很高的数字。 然后我就能够加载 XML。

spark-shell --driver-memory 6G

来源: https : //github.com/lintool/warcbase/issues/246#issuecomment-249272263

因为您要存储两次 RDD 并且您的逻辑必须像这样更改或使用 SparkSql 过滤

 val df: DataFrame = SparkFactory.spark.read
      .option("mode", "DROPMALFORMED")
      .format("com.databricks.spark.xml")
      .schema(customSchema) // defined previously
      .option("rowTag", "row")
      .load(s"$pathToInputXML")
      .coalesce(numPartitions)

    println(s"\n\nNUM PARTITIONS: ${df.rdd.getNumPartitions}\n\n")
    // prints 1604


    // regexes to clean the text
    val tagPat = "<[^>]+>".r
    val angularBracketsPat = "><|>|<"
    val whitespacePat = """\s+""".r

    // filter and select only the cols i'm interested in
     df
      .where( df.col("_TypeId") === "1" )
      .select(
        df("_Id").as("id"),
        df("_Title").as("title"),
        df("_Body").as("body"),
      ).as[Post]
      .map{
        case Post(id,title,body,tags) =>

          val body1 = tagPat.replaceAllIn(body,"")
          val body2 = whitespacePat.replaceAllIn(body1," ")

          Post(id,title.toLowerCase,body2.toLowerCase, tags.split(angularBracketsPat).mkString(","))

      }
      .orderBy(rand(SEED)) // random sort
      .write // write it back to disk
      .option("quoteAll", true)
      .mode(SaveMode.Overwrite)
      .csv(output)

您可以通过在环境变量中添加以下内容来更改堆大小:

  1. 环境变量名称:_JAVA_OPTIONS
  2. 环境变量值:-Xmx512M -Xms512M

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM