zoukankan      html  css  js  c++  java
  • SparkContext can find no suitable driver for jdbc

    Well,there is no post after my blog has been renovationed,so it is.

    Here,

    when i use a jdbc format url to connect to a mysql server in spark program.

    a error occured , no suitable driver ……..,as follow:

    1
    2
    3
      def hive_to_mysql(data: DataFrame, tableName: String): Unit = {
        data.write.mode(SaveMode.Append).jdbc(url, tableName, mysql_prop)
      }

    I think that the sparkcontext will find a driver from registered driver list according to connection url,

    but the error occured.

    with i am a beginner so i don’t know how to solve it.

    i found a pull request about no suitable driver from spark github,

    pull 5782

    1
    2
    3
    4
    5
    6
    7
    8
    def getConnector(driver: String, url: String, properties: Properties): () => Connection = {
          () => {
            try {
     -        if (driver != null) Utils.getContextOrSparkClassLoader.loadClass(driver)
     +        if (driver != null) DriverRegistry.register(driver)
            } catch {
              case e: ClassNotFoundException => {
                logWarning(s"Couldn't find class $driver", e);
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
         val upperBound = parameters.getOrElse("upperBound", null)
          val numPartitions = parameters.getOrElse("numPartitions", null)
      
     -    if (driver != null) Utils.getContextOrSparkClassLoader.loadClass(driver)
     +    if (driver != null) DriverRegistry.register(driver)
      
          if (partitionColumn != null
              && (lowerBound == null || upperBound == null || numPartitions == null)) {
     @@ -136,7 +136,7 @@ private[sql] case class JDBCRelation(
        override val schema: StructType = JDBCRDD.resolveTable(url, table, properties)
      
        override def buildScan(requiredColumns: Array[String], filters: Array[Filter]): RDD[Row] = {
     -    val driver: String = DriverManager.getDriver(url).getClass.getCanonicalName
     +    val driver: String = DriverRegistry.getDriverClassName(url)
          JDBCRDD.scanTable(
            sqlContext.sparkContext,
            schema,

    according to the changes puller made , he create a DriverWrapper extends java.sql.Driver, and reload the jdbc driver with the same driver loaded by sparkcontext .

    the key point is that the pull request has been mergen,haha. so i add a line before connectting to mysql server,like this

    1
    2
    3
    4
      def hive_to_mysql(data: DataFrame, tableName: String): Unit = {
        DriverRegistry.register("com.mysql.jdbc.Driver")
        data.write.mode(SaveMode.Append).jdbc(url, tableName, mysql_prop)
      }

    i think it will succeed ,but but but ,three but is but, it failed, a new error was reported by spark program.

    sparkContext can not find the registered driver……………

    the the CTO in my company solve the final problem for me.

    1
    2
    3
    4
      Property mysql_prop = new Property()
      mysql_prop.add("user","root")
      mysql_prop.add("password","password")
      mysql_prop.add("driver","com.mysql.jdbc.Driver") //the most important line

    the most important line had shown we must tell sparkContext the classname of Driver I mean.

    why so , it is a bug that the spark job in yarn cluster mode ran by oozie workflow.

    In other words,you must put driver key into Property that will be used by mysql connection while the spark job is ran by oozie workflow in yarn cluster mode.

    so far, all spark job will finish successfully.

  • 相关阅读:
    Mac idea 打不开
    git学习之git reset命令
    更改 macOS 用户帐户和个人文件夹的名称
    SpringBoot系列: 如何优雅停止服务
    windows环境下启动mongodb服务
    rocketMq4.2.0启动broker报错找不到或无法加载主类 FilesJavajdk1.8.0_101libdt.jar;C:Program]
    初创公司与成熟的公司各有什么利弊?有5年工作经验的人适合进那一个?(行业职位是一样的情况下)
    mac 10.15 国内如何安装brew
    Mac下SSH Key配置
    买苹果MacBook Pro ,有必要买care吗?
  • 原文地址:https://www.cnblogs.com/chenminklutz/p/7325988.html
Copyright © 2011-2022 走看看