我有一些中間數據需要存儲在HDFS和本地數據中。我正在使用Spark 1.6。在HDFS中作爲中間形式,我在/output/testDummy/part-00000
和/output/testDummy/part-00001
中獲取數據。我想使用Java/Scala將這些分區保存在本地,以便我可以將它們分別保存爲/users/home/indexes/index.nt
(通過合併在本地)或/users/home/indexes/index-0000.nt
和/home/indexes/index-0001.nt
。使用mapPartition和迭代器保存火花RDD
這裏是我的代碼: 注:testDummy是相同的測試,輸出與兩個分區。我想將它們單獨存儲或合併,但使用index.nt
文件進行本地存儲。我更喜歡分開存儲在兩個數據節點中。我正在使用集羣並在YARN上提交Spark任務。我還添加了一些評論,多少次以及我得到的數據。我怎麼辦?任何幫助表示讚賞。
val testDummy = outputFlatMapTuples.coalesce(Constants.INITIAL_PARTITIONS).saveAsTextFile(outputFilePathForHDFS+"/testDummy")
println("testDummy done") //1 time print
def savesData(iterator: Iterator[(String)]): Iterator[(String)] = {
println("Inside savesData") // now 4 times when coalesce(Constants.INITIAL_PARTITIONS)=2
println("iter size"+iterator.size) // 2 735 2 735 values
val filenamesWithExtension = outputPath + "/index.nt"
println("filenamesWithExtension "+filenamesWithExtension.length) //4 times
var list = List[(String)]()
val fileWritter = new FileWriter(filenamesWithExtension,true)
val bufferWritter = new BufferedWriter(fileWritter)
while (iterator.hasNext){ //iterator.hasNext is false
println("inside iterator") //0 times
val dat = iterator.next()
println("datadata "+iterator.next())
bufferWritter.write(dat + "\n")
bufferWritter.flush()
println("index files written")
val dataElements = dat.split(" ")
println("dataElements") //0
list = list.::(dataElements(0))
list = list.::(dataElements(1))
list = list.::(dataElements(2))
}
bufferWritter.close() //closing
println("savesData method end") //4 times when coal=2
list.iterator
}
println("before saving data into local") //1
val test = outputFlatMapTuples.coalesce(Constants.INITIAL_PARTITIONS).mapPartitions(savesData)
println("testRDD partitions "+test.getNumPartitions) //2
println("testRDD size "+test.collect().length) //0
println("after saving data into local") //1
PS:我也跟着,this和this但不完全相同什麼我尋找,我也莫名其妙,但沒有得到任何東西index.nt
斯卡拉已經使'名單使世界變得更好一點::(dataElements(2))'等同於'dataElements(2):: list'所以不要讓斯卡拉難過,並開始使用這種語法在至少當方法類似操作符時。順便說一句,'ListBuffer'可能在這裏更合適 – Dici