2016-02-18 24 views
0

我正在使用運行在雲中的Spark。我的存儲不是傳統的HDFS,但我通過URL連接到我的文件。例如,我可以執行以下操作,Spark將接收目錄中的所有文件。我也可以使用任何其他HDFS函數調用。Spark:按不同順序處理文件然後返回

sc.TextFile("hdfs://mystorage001/folder/month1/*")

我的數據分佈5個不同的驅動器之間,我想火花循環賽每個驅動器讀取之間,這樣我就可以從所有5並行讀取。我目前可以執行以下操作並處理所有數據,但不會並行讀取驅動器。相反,spark會讀取一個驅動器中的所有文件,然後移至下一個。

sc.TextFile("hdfs://mystorage001/folder/month1/*, hdfs://mystorage002/folder/month2/*, hdfs://mystorage003/folder/month3/*, hdfs://mystorage004/folder/month4/*,hdfs://mystorage005/folder/month5/*")

我有100個執行者。所以我也嘗試過這個,但是這會給我帶來最差的表現。

sc.TextFile("hdfs://mystorage001/folder/month1/*, 20) union sc.TextFile("hdfs://mystorage002/folder/month2/*, 20) union sc.TextFile("hdfs://mystorage003/folder/month3/*, 20) union sc.TextFile("hdfs://mystorage004/folder/month4/*, 20) union sc.TextFile("hdfs://mystorage005/folder/month5/*")

我知道,在每個目錄已經我命名的文件000000_0,000001_0,000002_0,等等......所以,如果我可以命令的文件列表火花由名字的那部分內容,我覺得這將完成我想要的,但唯一的辦法是我已經想出瞭如何返回列表的方法是通過wholeTextFile(),無論如何都需要首先加載所有數據。

+0

http://stackoverflow.com/a/32978688/1560062,http://stackoverflow.com/q/11342400/1560062 – zero323

回答

0
import org.apache.hadoop.fs._ 
val fs = FileSystem.get(sc.hadoopConfiguration) 
val path = Array(new Path("hdfs://mystorage001/folder/month1/"), 
new Path("hdfs://mystorage001/folder/month2/"), 
new Path("hdfs://mystorage001/folder/month3/"), 
new Path("hdfs://mystorage001/folder/month4/"), 
new Path("hdfs://mystorage001/folder/month5/")) 
path.map(fs.listStatus(_)).flatMap(_.zipWithIndex).sortBy(_._2).map(_._1.getPath).mkString(",")