[英]Using Spark on Dataproc, how to write to GCS separately from each partition?
在GCP Dataproc上使用Spark,我成功將整個RDD寫入GCS,如下所示:
rdd.saveAsTextFile(s"gs://$path")
產品是同一路徑中每個分區的文件。
如何為每個分區寫入文件(具有基於分區信息的唯一路徑)
以下是發明的無法正常運行的代碼示例
rdd.mapPartitionsWithIndex(
(i, partition) =>{
partition.write(path = s"gs://partition_$i", data = partition_specific_data)
}
)
當我從Mac上的分區中調用以下函數時,它將寫入本地磁盤,在Dataproc上,我收到一個錯誤,無法將gs識別為有效路徑。
def writeLocally(filePath: String, data: Array[Byte], errorMessage: String): Unit = {
println("Juicy Platform")
val path = new Path(filePath)
var ofos: Option[FSDataOutputStream] = null
try {
println(s"\nTrying to write to $filePath\n")
val conf = new Configuration()
conf.set("fs.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem")
conf.set("fs.AbstractFileSystem.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFS")
// conf.addResource(new Path("/home/hadoop/conf/core-site.xml"))
println(conf.toString)
val fs = FileSystem.get(conf)
val fos = fs.create(path)
ofos = Option(fos)
fos.write(data)
println(s"\nWrote to $filePath\n")
}
catch {
case e: Exception =>
logError(errorMessage, s"Exception occurred writing to GCS:\n${ExceptionUtils.getStackTrace(e)}")
}
finally {
ofos match {
case Some(i) => i.close()
case _ =>
}
}
}
這是錯誤:
java.lang.IllegalArgumentException: Wrong FS: gs://path/myFile.json, expected: hdfs://cluster-95cf-m
如果在Dataproc集群上運行,則無需在配置中顯式填充“ fs.gs.impl”; 一個new Configuration()
應該已經包含必要的映射。
這里的主要問題是val fs = FileSystem.get(conf)
使用val fs = FileSystem.get(conf)
的fs.defaultFS
屬性。 它無法知道您是否要獲取特定於HDFS或GCS的文件系統實例。 一般情況下,在Hadoop和火花,一個FileSystem
的實例是從根本上依賴於單個URL scheme
; 您需要為每個不同的方案獲取特定於方案的實例,例如hdfs://
或gs://
或s3://
。
解決問題的最簡單方法是始終使用Path.getFileSystem(Configuration)而不是FileSystem.get(Configuration)
。 並確保您的path
完全符合該方案:
...
val path = "gs://bucket/foo/data"
val fs = path.getFileSystem(conf)
val fos = fs.create(path)
ofos = Option(fos)
fos.write(data)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.