[英]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 不可能试图把所有东西都塞进内存中,如果不合适就会崩溃,对吧??)
我尝试过的事情:
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)
笔记
更新我编写了一个较短版本的代码,它只读取文件然后 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)
您可以通过在环境变量中添加以下内容来更改堆大小:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.