保存并从本地二进制文件加载Spark RDD - 最小的工作示例 [英] Save and load Spark RDD from local binary file - minimal working example
问题描述
我正在开发一个Spark应用程序,其中首先计算RDD,然后需要将其存储到磁盘,然后再次加载到Spark中。为此,我正在寻找将RDD保存到本地文件然后加载它的最小工作示例。
I am working on a Spark app in which an RDD is first calculated, then need to be stored to disk, and then loaded again into Spark. To this end, I am looking for a minimal working example of saving an RDD to a local file and then loading it.
文件格式不适合文本转换,所以 saveAsTextFile
不会飞。
The file format is not suitable for text conversion, so saveAsTextFile
won't fly.
RDD可以是简单的RDD或Pair RDD,它不是关键。文件格式可以是HDFS或不是。
The RDD can either be a plain RDD or Pair RDD, it is not crucial. The file format can be either of HDFS or not.
这个例子可以用Java或Scala。
The example can be either in Java or Scala.
Thanks!
推荐答案
只要RDD中的值是可序列化的,您可以尝试使用 RDD。 saveAsObjectFile
/ SparkContext.objectFile
:
As long as values in the RDD are serializable you can try to use RDD.saveAsObjectFile
/ SparkContext.objectFile
:
case class Foobar(foo: Int, bar: Map[String, Int])
val rdd = sc.parallelize(Seq(
Foobar(1, Map("foo" -> 0)),
Foobar(-1, Map("bar" -> 3))
))
rdd.saveAsObjectFile("foobar")
sc.objectFile[Foobar]("foobar")
这篇关于保存并从本地二进制文件加载Spark RDD - 最小的工作示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!