将数据从 BigQuery 表加载到 Dataproc 集群时出错

Posted

技术标签:

【中文标题】将数据从 BigQuery 表加载到 Dataproc 集群时出错【英文标题】:Error while loading data from BigQuery table to Dataproc cluster 【发布时间】:2020-05-30 21:00:08 【问题描述】:

我是 Dataproc 和 PySpark 的新手,在通过 Jupyter Lab API 将 BigQuery 表集成到 Dataproc 集群时遇到了一些问题。下面是我用于通过 Jupyter Notebook API 将 BigQuery 表加载到 Dataproc 集群的代码,但在加载表时出现错误

from pyspark.sql import SparkSession

 SparkSession.builder.appName('Jupyter BigQuery Storage').config(
  'spark.jars', 'gs://spark-lib/bigquery/spark-bigquery-latest.jar').getOrCreate()

df=spark.read.format("com.google.cloud.spark.bigquery").option(
  "table", "publicdata.samples.shakespeare").load()

df.printSchema()

下面是我遇到的错误

Py4JJavaErrorTraceback (most recent call last)
<ipython-input-17-789ad67053e5> in <module>()
      1 table = "publicdata.samples.shakespeare"
----> 2 df = spark.read.format("com.google.cloud.spark.bigquery").option("table",table).load()
      3 df.printSchema()

/usr/lib/spark/python/pyspark/sql/readwriter.pyc in load(self, path, format, schema, **options)
    170             return self._df(self._jreader.load(self._spark._sc._jvm.PythonUtils.toSeq(path)))
    171         else:
--> 172             return self._df(self._jreader.load())
    173 
    174     @since(1.4)

/opt/conda/anaconda/lib/python2.7/site-packages/py4j/java_gateway.pyc in __call__(self, *args)
   1255         answer = self.gateway_client.send_command(command)
   1256         return_value = get_return_value(
-> 1257             answer, self.gateway_client, self.target_id, self.name)
   1258 
   1259         for temp_arg in temp_args:

/usr/lib/spark/python/pyspark/sql/utils.pyc in deco(*a, **kw)
     61     def deco(*a, **kw):
     62         try:
---> 63             return f(*a, **kw)
     64         except py4j.protocol.Py4JJavaError as e:
     65             s = e.java_exception.toString()

/opt/conda/anaconda/lib/python2.7/site-packages/py4j/protocol.pyc in get_return_value(answer, gateway_client, target_id, name)
    326                 raise Py4JJavaError(
    327                     "An error occurred while calling 012.\n".
--> 328                     format(target_id, ".", name), value)
    329             else:
    330                 raise Py4JError(

Py4JJavaError: An error occurred while calling o254.load.
: java.lang.ClassNotFoundException: Failed to find data source: com.google.cloud.spark.bigquery. Please find packages at http://spark.apache.org/third-party-projects.html
    at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:639)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:190)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:164)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.google.cloud.spark.bigquery.DefaultSource
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$23$$anonfun$apply$15.apply(DataSource.scala:622)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$23$$anonfun$apply$15.apply(DataSource.scala:622)
    at scala.util.Try$.apply(Try.scala:192)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$23.apply(DataSource.scala:622)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$23.apply(DataSource.scala:622)
    at scala.util.Try.orElse(Try.scala:84)
    at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:622)
    ... 13 more```

【问题讨论】:

【参考方案1】:

请将SparkSession.builder 结果赋值给一个变量:

spark = SparkSession.builder\
  .appName('Jupyter BigQuery Storage')\
  .config('spark.jars', 'gs://spark-lib/bigquery/spark-bigquery-latest.jar')\
  .getOrCreate()

另外,公共数据集的引用是bigquery-public-data,所以请将阅读改为

df = spark.read.format("com.google.cloud.spark.bigquery")\
  .option("table", "bigquery-public-data.samples.shakespeare")\
  .load()

【讨论】:

我也把格式改成了format("bigquery")

以上是关于将数据从 BigQuery 表加载到 Dataproc 集群时出错的主要内容,如果未能解决你的问题,请参考以下文章

将数据从存储加载到 bigquery 时解析数据类型时出错

从 python 生成 Faker 数据并将其加载到 BigQuery 嵌套表中

从外部表读取与加载数据并在 Bigquery 中读取

GCP将数据作为字符串从GCS中的CSV文件加载到BigQuery表中

表不会加载到 BigQuery

将分区(火花)镶木地板加载到 bigquery 表