将RDD保存到文件中

时间:2016-03-19 16:43:35

标签: scala apache-spark

我正在尝试将org.apache.spark.rdd.RDD[((String, String), Int)] = MapPartitionsRDD[21]类型的RDD写入文件:

我通过以下代码创建了我的RDD:

val res = motList2.cartesian(motList2).map { case (x, y) =>
((x._1, y._1), x._2 + y._2)
}

val dir = "H:\\procjet-results"
res.saveAsTextFile(dir)

motList2可以是:

motList2=List( (("Chicken","Pasta"),2), ("Chicken","robot"),3) ... )

但我总是有例外

org.apache.hadoop.mapred.FileAlreadyExistsException

我检查过,文件夹已创建,但所有文件都是空的。

2 个答案:

答案 0 :(得分:0)

由于异常显示“org.apache.hadoop.mapred.FileAlreadyExistsException”,因此作业失败的原因是您尝试写入的目录/文件已存在。如果你以前有过hadoop的经验。这是同样的问题。默认情况下,Spark不会覆盖您尝试保存结果的位置,因此如果已经存在,则必须先手动或在代码中删除它。

答案 1 :(得分:0)

你应该使用文件://前缀来确保网址更安全;很多时候hdfs是尝试保存文件的默认值。

相关问题