找不到Scala程序的MySql驱动程序 [英] MySql driver not found for Scala program

查看:268
本文介绍了找不到Scala程序的MySql驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Scala的新手,正在尝试使用Spark从mysql数据库读取.无论我做什么,我都面临着一个类未找到的异常.我尝试使用Squeryl,Scalike等在没有Spark的情况下进行连接.始终存在相同的问题. 这是我尝试过的一个示例:

I'm a newbie on Scala, am trying to use Spark to read from a mysql database. I'm facing a class-not-found exception whatever I do. I tried to connect without Spark, using Squeryl, Scalike, etc. Always the same problem. Here's one example I tried :

logger.info("Write part")

val dataframe_mysql = spark.sqlContext
  .read.format("jdbc")
  .option("url", s"jdbc:mysql://${datamart_server}:3306/vol")
  .option("driver", "com.mysql.jdbc.Driver")
  .option("dbtable", "company")
  .option("user", datamart_user).option("password", datamart_pwd)
  .load()

dataframe_mysql.show()

我试图将驱动程序类名放在src/main/resources/application.conf中:

I tried to put the driver classname in a src/main/resources/application.conf:

db.default.driver="com.mysql.jdbc.Driver"

但这没有帮助.我有:

java.sql.SQLException:没有合适的驱动程序

java.sql.SQLException: No suitable driver

我还共享了sbt文件,以显示如何添加依赖项:

I also share the sbt file to show how I add the dependencies :

name := "commercial-api-datamart-feed"
version := "0.1"
scalaVersion := "2.11.6"
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.0"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.3" % Runtime
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.0"
libraryDependencies += "org.apache.spark" %% "spark-core" % "2.3.0"
libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.3.0"
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.24" % Runtime

火花不是强制性的,但我认为它对性能更好.

Spark is not mandatory but I think it's better for performance.

推荐答案

您如何运行此代码?您需要将MySQL JAR传递为--jars;如果启动spark-shellspark-submit,则类似--jars /path/to/mysql.jar.

How are you running this code? You'll need to pass the MySQL JAR as --jars; something like --jars /path/to/mysql.jar if starting up spark-shell or spark-submit.

如果您希望运行单个JAR,则需要确保将MySQL JAR嵌入为超级JAR的一部分.我从未使用过SBT,但是您需要检查创建的最终JAR是否在其中包含MySQL类-如果没有,请使用相关的SBT标志来实现.

If you prefer running a single JAR, you'll need to ensure that the MySQL JAR is embedded as part of your uber JAR. I've never used SBT but you'll need to check whether the final JAR created has the MySQL classes inside it -- if not, use the relevant SBT flags to make that happen.

这篇关于找不到Scala程序的MySql驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆