【发布时间】:2020-11-04 19:10:19
【问题描述】:
我正在尝试列出存储桶中的所有对象,然后将其中的部分或全部读取为 CSV。我已经花了两天时间,试图同时做这两个,但如果我使用 Google 的库,我一次只能工作一个。
我认为问题在于 Google 自己的库之间不兼容,但我并不完全确定。首先,我认为我应该展示我是如何做每一件事的。
这就是我读取单个文件的方式。在我的 Scala 版本中,您可以使用 gs:// 网址和 spark.read.csv:
val jsonKeyFile = "my-local-keyfile.json"
ss.sparkContext.hadoopConfiguration.set("google.cloud.auth.service.account.json.keyfile", jsonKeyFile)
spark.read
.option("header", "true")
.option("sep", ",")
.option("inferSchema", "false")
.option("mode", "FAILFAST")
.csv(gcsFile)
这实际上是单独工作的,我从中得到了一个有效的 DF。然后当我尝试添加谷歌的存储库时出现问题:
libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "1.70.0"
如果我再次尝试运行相同的代码,我会从 .csv 调用中得到这个坏男孩:
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
19/05/14 16:38:00 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
An exception or error caused a run to abort: Class com.google.common.base.Suppliers$SupplierOfInstance does not implement the requested interface java.util.function.Supplier
java.lang.IncompatibleClassChangeError: Class com.google.common.base.Suppliers$SupplierOfInstance does not implement the requested interface java.util.function.Supplier
at com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystemBase.getGcsFs(GoogleHadoopFileSystemBase.java:1488)
at com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystemBase.configure(GoogleHadoopFileSystemBase.java:1659)
at com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystemBase.initialize(GoogleHadoopFileSystemBase.java:683)
at com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystemBase.initialize(GoogleHadoopFileSystemBase.java:646)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:3303)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:124)
...(lots more trace, probably irrelevant)
那么,你可能会问,你为什么不直接使用这个库呢?嗯...这是列出存储桶中对象的代码:
StorageOptions
.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(
File(jsonKeyFile).inputStream()))
.build()
.getService
.list(bucket)
.getValues
.asScala
.map(irrelevant)
.toSeq
.toDF("irrelevant")
而且我还没有找到一种在没有指定库的情况下轻松做到这一点的方法。
【问题讨论】:
标签: scala apache-spark google-cloud-storage