spark 读写mysql,本地调试运行OK,打包集群运行:
20/08/04 19:28:01 ERROR yarn.ApplicationMaster: User class threw exception: java.sql.SQLException: No suitable driver
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$7.apply(JDBCOptions.scala:84)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$7.apply(JDBCOptions.scala:84)
at scala.Option.getOrElse(Option.scala:121)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:83)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:37)
at org.apache.spark.sql.DataFrameReader.jdbc(DataFrameReader.scala:262)
at sparkapp.ReadMysql$.fieldPartiionRead(ReadMysql.scala:72)
at sparkapp.ReadMysql$.main(ReadMysql.scala:33)
at sparkapp.ReadMysql.main(ReadMysql.scala)
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 org.apache.spark.deploy.yarn.ApplicationMaster$$anon$2.run(ApplicationMaster.scala:645)
20/08/04 19:28:01 INFO yarn.ApplicationMaster: Final app status: FAILED, exitCode: 15, (reason: User class threw exception: java.sql.SQLException: No suitable driver)
at sparkapp.ReadMysql$.fieldPartiionRead(ReadMysql.scala:72) 这行为连接mysql,但是本地没有任何问题。
经网友发现,说是jdk缺少mysql依赖。
将mysql-connector-java-5.1.38.jar 下载上传到 /usr/local/java/jdk/jre/lib/ext 下。然后重新执行。OK。但是观察发现,任务中间有重试一次,上服务器看日志,发现第一次运行将任务分配到别的节点上运行了,然后还是这个错误。第二次分配到当前节点运行,就成功了。如果集群模式的话,需要在每台节点上都搞下环境才稳些。