在笔记本启动后,我一直在尝试将外部jar包含到pyspark / Jupyter笔记本环境中,但徒劳无功。 (或者,可以通过以下方式成功添加罐子: 1.使用--jar ojdbc6.jar 2.将jar包含在spark-defaults.conf中)
我尝试了以下所有失败的方法:
想法1:
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession \
.builder \
.appName("testjars") \
.config("spark.jars", "C:\\Users\\U620311\\ojdbc6.jar") \
.config("spark.driver.extraClassPath", "C:\\Users\\U620311\\ojdbc6.jar") \
.config("spark.executor.extraClassPath", "C:\\Users\\U620311\\ojdbc6.jar") \
.getOrCreate()
spark.sparkContext._conf.getAll()
如下所示调用jar文件:
DB_USER = ""
DB_PASS = ""
DB_DRIVER = "oracle.jdbc.driver.OracleDriver"
JDBC_URL = "<connection string>"
query = '''(select * from <tablename>) t1'''
ojdbcDF = (sqlContext.read.format("jdbc").option("driver", DB_DRIVER).option("url", JDBC_URL).option("dbtable", query).option("user", DB_USER).option("password", DB_PASS).load())
想法2:
我玩过所有排列。包括extraclasspath但不包括spark.jars,反之亦然。没有成功。
想法3:
import os
os.environ['PYSPARK_SUBMIT_ARGS'] = '--jars ojdbc6.jar pyspark-shell'
import pyspark
sc.stop()
sc = pyspark.SparkContext()
from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
DB_USER = "<uid>"
DB_PASS = "<pwd>"
DB_DRIVER = "oracle.jdbc.driver.OracleDriver"
JDBC_URL = "<connection string>"
query = '''(select * from <tablename>) t1'''
ojdbcDF = (sqlContext.read.format("jdbc").option("driver", DB_DRIVER).option("url", JDBC_URL).option("dbtable", query).option("user", DB_USER).option("password", DB_PASS).load())
以上所有均导致相同的错误消息。您能否帮助您确定是否缺少配置或代码。请提出尝试的替代方法。
Py4JJavaError: An error occurred while calling o373.load.
: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.spark.sql.execution.datasources.jdbc.DriverRegistry$.register(DriverRegistry.scala:38)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$6.apply(JDBCOptions.scala:78)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$6.apply(JDBCOptions.scala:78)
at scala.Option.foreach(Option.scala:257)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:78)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:34)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:32)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:330)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:152)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:125)
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:280)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Thread.java:748)