当前位置: 首页 > 知识库问答 >
问题:

用pyspark从雪花加载数据的ClassNotFoundException

袁玮
2023-03-14
py4j.protocol.Py4JJavaError: An error occurred while calling o45.load.
: java.lang.NoClassDefFoundError: net/snowflake/client/jdbc/internal/org/bouncycastle/jce/provider/BouncyCastleProvider
from pyspark.sql import SparkSession
from pyspark import SparkConf

conf = SparkConf()
conf.set('spark.jars.packages',
         'net.snowflake:spark-snowflake_2.11:2.8.4-spark_2.4,net.snowflake:snowflake-jdbc:3.12.17')
spark = SparkSession.builder.config(conf=conf).getOrCreate()

sf_reader_options = {'sfURL': 'example.snowflakecomputing.com', 'sfAccount': 'example_account',
                     'sfWarehouse': 'example_warehouse', 'sfRole': 'DATASCIENCE', 'sfUser': 'user',
                     'sfPassword': 'pass', 'sfDatabase': 'db_name', 'sfSchema': 'schema_name', 'sfTimezone': 'UTC'}

reader = (spark
          .read
          .format('net.snowflake.spark.snowflake')
          .options(**sf_reader_options))

result = reader.option('query', 'select * from TABLE_NAME').load()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/charlie/lark/bigbird/venv/lib/python3.7/site-packages/pyspark/sql/readwriter.py", line 172, in load
    return self._df(self._jreader.load())
  File "/Users/charlie/lark/bigbird/venv/lib/python3.7/site-packages/py4j/java_gateway.py", line 1257, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "/Users/charlie/lark/bigbird/venv/lib/python3.7/site-packages/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/Users/charlie/lark/bigbird/venv/lib/python3.7/site-packages/py4j/protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o45.load.
: java.lang.NoClassDefFoundError: net/snowflake/client/jdbc/internal/org/bouncycastle/jce/provider/BouncyCastleProvider
    at net.snowflake.spark.snowflake.Parameters$.mergeParameters(Parameters.scala:202)
    at net.snowflake.spark.snowflake.DefaultSource.createRelation(DefaultSource.scala:59)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:332)
    at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:242)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:230)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:186)
    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: net.snowflake.client.jdbc.internal.org.bouncycastle.jce.provider.BouncyCastleProvider
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 17 more

我使用的是spark 2.4.7和spark-snowflake 2.8.4,以及snowflake jdbc 3.12.17。我在Mac OS X Big Sur上。这发生在我升级到大苏尔之后,尽管我不确定这是否有关系。

我试过:

  • 将bouncy castle提供程序作为包依赖项添加到我的配置中
  • 检查java_home是否指向Java 8(它确实指向)
  • 重新安装java 8(使用homebrew和adoptopenjdk)
  • 根据此处的说明,将bouncy castle添加为安全提供程序
  • 更新spark-snowflake和snowflake-jdbc(之前使用的是2.7.0和3.12.3,错误相同)

任何帮助都将不胜感激!

共有1个答案

郎和志
2023-03-14

最终,我能够通过以下方式解决这个问题:

  • 直接从Oracle下载Java(而不是用homebrew卸载并重新安装),
  • 删除spark,再次下载(从apache下载,而不是通过homebrew下载),并按此处所述设置环境变量(主要是...我使用虚拟环境,所以我没有将PYSPARK_PYTHON硬编码到系统python3)
  • 卸载pyspark并重新安装
  • 退出pycharm并重新打开(这刷新了在.zshrc中设置的所有环境变量,如JAVA_HOME)

几乎可以肯定有一个更简单的方法,但这个有效。

 类似资料: