【问题标题】:How to use gcs-connector and google-cloud-storage alongside in Scala如何在 Scala 中同时使用 gcs-connector 和 google-cloud-storage
【发布时间】: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


    【解决方案1】:

    我找到了导致问题的原因。 Guava:27.1-android 在某些时候是某个库的依赖项,我不知道它是哪个以及如何到达那里,但它正在使用中。在这个版本的 Guava 中,Supplier 接口没有扩展 Java Supplier 接口。

    我通过将 Guava 27.1-jre 添加到我的依赖项来修复它。我不知道顺序是否重要,但我现在不敢碰任何东西。这是我放置它的地方:

    libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
    libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.4.1" % "provided"
    libraryDependencies += "com.google.guava" % "guava" % "27.1-jre"
    libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "1.70.0"
    //BQ samples as of 27feb2019 use hadoop2 but hadoop3 seems to work fine and are recommended elsewhere
    libraryDependencies += "com.google.cloud.bigdataoss" % "bigquery-connector" % "hadoop3-0.13.16" % "provided"
    libraryDependencies += "com.google.cloud.bigdataoss" % "gcs-connector" % "hadoop3-1.9.16" % "provided"
    

    希望这可以防止其他一些可怜的灵魂在这个 bs 上花费 2 天时间。

    【讨论】:

    • 这完全拯救了我的一天!
    • 对不起,我想我以前没见过那个。如果您无法弄清楚,请提出一个新问题,其他人可能已经:)
    【解决方案2】:

    @Andy 那是救命恩人!在花时间调查之后,我发现我正在使用的一些库使用不同版本的guava,因此您应该将这个27.1-jre 版本加载到您的环境中。 在我使用 Gradle 的情况下,我必须执行以下操作:

    // This is here *the first line* to support GCS files [Do not remove or move!]
    implementation group: 'com.google.guava', name: 'guava', version: '27.1-jre'
    
    api(project(':other.project')) {
        exclude group: 'com.google.guava', module: 'guava'
    }
    
    implementation(group: 'other.library', name: 'library.name', version: 'library.version') {
        exclude group: 'com.google.guava', module: 'guava'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2021-11-22
      • 2018-04-04
      • 2021-12-25
      • 2019-05-17
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多