![](/img/trans.png)
[英]Does the Spark driver wait for all partitions to finish work from rdd.foreachPartition before continuing?
[英]Spark Scala Get Data Back from rdd.foreachPartition
我有一些這樣的代碼:
println("\nBEGIN Last Revs Class: "+ distinctFileGidsRDD.getClass)
val lastRevs = distinctFileGidsRDD.
foreachPartition(iter => {
SetupJDBC(jdbcDriver, jdbcUrl, jdbcUser, jdbcPassword)
while(iter.hasNext) {
val item = iter.next()
//println(item(0))
println("String: "+item(0).toString())
val jsonStr = DB.readOnly { implicit session =>
sql"SELECT jsonStr FROM lasttail WHERE fileGId = ${item(0)}::varchar".
map { resultSet => resultSet.string(1) }.single.apply()
}
println("\nJSON: "+jsonStr)
}
})
println("\nEND Last Revs Class: "+ lastRevs.getClass)
代碼輸出(經過大量編輯)類似於:
BEGIN Last Revs Class: class org.apache.spark.rdd.MapPartitionsRDD
String: 1fqhSXPE3GwrJ6SZzC65gJnBaB5_b7j3pWNSfqzU5FoM
JSON: Some({"Struct":{"fileGid":"1fqhSXPE3GwrJ6SZzC65gJnBaB5_b7j3pWNSfqzU5FoM",... )
String: 1eY2wxoVq17KGMUBzCZZ34J9gSNzF038grf5RP38DUxw
JSON: Some({"Struct":{"fileGid":"1fqhSXPE3GwrJ6SZzC65gJnBaB5_b7j3pWNSfqzU5FoM",... )
...
JSON: None()
END Last Revs Class: void
問題 1:如何使 lastRevs 值采用有用的格式,例如 JSON 字符串/null 或類似 Some / None 的選項?
問題 2:我的偏好:是否有另一種方法可以獲取類似 RDD 格式(而不是迭代器格式)的分區數據?
dstream.foreachRDD { (rdd, time) =>
rdd.foreachPartition { partitionIterator =>
val partitionId = TaskContext.get.partitionId()
val uniqueId = generateUniqueId(time.milliseconds, partitionId)
// use this uniqueId to transactionally commit the data in partitionIterator
}
}
來自http://spark.apache.org/docs/latest/streaming-programming-guide.html#performance-tuning
問題 3:我使用的獲取數據的方法是否合理(鑒於我正在關注上面的鏈接)? (撇開現在這是一個 scalikejdbc 系統 JDBC 的事實。這將是除此原型之外的某種類型的鍵值存儲。)
要創建使用執行器本地資源(例如數據庫或網絡連接)的轉換,您應該使用rdd.mapPartitions
。 它允許在執行器本地初始化一些代碼,並使用這些本地資源來處理分區中的數據。
代碼應如下所示:
val lastRevs = distinctFileGidsRDD.
mapPartitions{iter =>
SetupJDBC(jdbcDriver, jdbcUrl, jdbcUser, jdbcPassword)
iter.map{ element =>
DB.readOnly { implicit session =>
sql"SELECT jsonStr FROM lasttail WHERE fileGId = ${element(0)}::varchar"
.map { resultSet => resultSet.string(1) }.single.apply()
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.