找到可能不存在的RDD的并集

时间:2016-11-23 11:42:07

标签: scala apache-spark rdd

我正试图获得一些RDD的联合。 RDD正在通过SparkContext.textFile读入,但有些可能不存在于文件系统中。

val rdd1 = Try(Repository.fetch(data1Path))
val rdd2 = Try(Repository.fetch(data2Path))
val rdd3 = Try(Repository.fetch(data3Path))
val rdd4 = Try(Repository.fetch(data4Path))

val all = Seq(rdd1, rdd2, rdd3, rdd4)
val union = sc.union(all.map {case Success(r) => r})
val results = union.filter(some-filter-logic).collect

然而,由于延迟评估,所有这些Try语句都会评估为Success,无论这些文件是否存在,并且在{{{}}评估时FileNotFoundException结束collect 1}}被称为。

有解决方法吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试运行循环来检查文件是否存在,并且在同一循环中您可以创建RDD并获得联合。 要么 您可以使用wholeTextFiles API将一个目录中的所有文件读取为键值对。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div>
  <br>
</div>

<div>
  <br>Text goes here text goes here..Text goes here text goes here.. Text goes here text goes here..
</div>

如果任何文件也为空,则不会产生任何问题。