一般如我们在 pycharm 本地运行 pyspark 连接 mysql 时,可能会报 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
错误,显而易见没有找到 java 的 mysql driver 包。
报错内容
笔者执行的全代码如下:
from pyspark.sql import SparkSession
from pyspark.sql import SQLContext
spark = SparkSession.builder.appName('demo').getOrCreate()
sqlContext = SQLContext(spark.sparkContext)
df = sqlContext.read.format("jdbc").options(
url="jdbc:mysql://localhost:3306/my_db",
driver="com.mysql.jdbc.Driver",
dbtable="my_table",
user="root",
password="").load()
df.show()
运行后,报如下错误信息:
Traceback (most recent call last):
File "/xxx/xxx/xxx.py", line 16, in <module>
password="").load()
File "/xxx/xxx/lib/python3.7/site-packages/pyspark/sql/readwriter.py", line 172, in load
return self._df(self._jreader.load())
File "/xxx/xxx/lib/python3.7/site-packages/py4j/java_gateway.py", line 1257, in __call__
answer, self.gateway_client, self.target_id, self.name)
File "/xxx/xxx/lib/python3.7/site-packages/pyspark/sql/utils.py", line 63, in deco
return f(*a, **kw)
File "/xxx/xxx/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 o36.load.
: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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.jdbc.DriverRegistry$.register(DriverRegistry.scala:45)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$5.apply(JDBCOptions.scala:99)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$5.apply(JDBCOptions.scala:99)
at scala.Option.foreach(Option.scala:257)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:99)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:35)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:32)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:318)
at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:223)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:211)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:167)
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)
解决方法
下载 mysql jdbc 相关 jar 包,并在 Spark 全局加如下 driver class 配置即可:
SparkSession.builder.config('spark.driver.extraClassPath', '<path>/mysql-connector-java-5.1.49.jar')
完整代码如下:
from pyspark.sql import SparkSession
from pyspark.sql import SQLContext
SparkSession.builder.config('spark.driver.extraClassPath',
'/xxx/xxx/.m2/repository/mysql/mysql-connector-java/5.1.49/mysql-connector-java-5.1.49.jar')
spark = SparkSession.builder.appName('demo').getOrCreate()
sqlContext = SQLContext(spark.sparkContext)
df = sqlContext.read.format("jdbc").options(
url="jdbc:mysql://localhost:3306/my_db",
driver="com.mysql.jdbc.Driver",
dbtable="my_table",
user="root",
password="").load()
df.show()