java - 使用 Spark 连接 MariaDB 时出现 ClassNotFoundException

标签 java scala apache-spark sbt apache-spark-sql

我正在尝试将 Spark 连接到 MariaDB,但出现了

Exception in thread "main" java.lang.ClassNotFoundException: org.mariadb.jdbc.Driver

现在,在将其标记为重复之前,我知道该错误意味着什么,并且我已经阅读了不同的线程,但没有成功。

这是我的build.sbt

scalaVersion := "2.11.8"

name := "SparkJdbc"

libraryDependencies ++= Seq(
    "org.apache.spark" %% "spark-core" % "2.0.2",
    "org.apache.spark" %% "spark-sql" % "2.0.2",
    "org.mariadb.jdbc" % "mariadb-java-client" % "1.5.2"
)

我也有单独的 jar ,并且我已经尝试过

spark-submit --class SparkJdbc target/scala-2.11/sparkjdbc_2.11-0.1-SNAPSHOT.jar 
--jars /path/to/mariadb-java-client-1.5.2.jar

但这也因相同的错误而失败。

以下是我用来连接的代码

val (driver, url, username, password) = ("org.mariadb.jdbc.Driver", "jdbc:mysql://localhost/db1", "user", "password");

Class.forName(driver).newInstance;

val data = spark.sqlContext.read
    .format("jdbc")
    .option("url", url)
    .option("user", username)
    .option("password", password)
    .option("dbtable", "SELECT * FROM db1.football LIMIT 10")
    .load();

最佳答案

我可以看到两个可能的错误:

  1. --jars 应在 jar 名称之前使用。来自 documentation ,关于 Jar 名称后面的参数:

Arguments passed to the main method of your main class, if any

  • 文件应位于所有节点上的同一路径上或位于 HDFS 等分布式文件系统中 - 如果您仅在主节点或驱动程序中获取文件,则应用程序将引发错误
  • 编辑:

    should还设置 --driver-class-path=/path/to/jar/with/driver.jar

    取自文档的其他选项是:

    The JDBC driver class must be visible to the primordial class loader on the client session and on all executors. This is because Java’s DriverManager class does a security check that results in it ignoring all drivers not visible to the primordial class loader when one goes to open a connection. One convenient way to do this is to modify compute_classpath.sh on all worker nodes to include your driver JARs.

    关于java - 使用 Spark 连接 MariaDB 时出现 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41242196/

    相关文章:

    scala - 在 spark 中读取 csv 文件时出现 ArrayIndexOutOfBoundsException

    python - 使用 toPandas() 方法将 spark 数据帧转换为 Pandas 数据帧时会发生什么

    Java 构建错误且没有明显原因

    java - 将 map 拆分为两个列表

    java - changeit 在 java net ssl keystore 中到底做了什么?

    java - 如何使用java api更新elasticsearch中的嵌套字段值

    Java,计算两个日期之间的天数

    scala - 获取包的所有类

    apache-spark - Spark 机器学习 AST 比较

    python - 如何使用 StandardScaler 标准化 Spark 中的一列?