• 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.

  • 相关阅读:
    ubuntu14.04安装chromium以及flash插件
    linux fuser的使用
    对max_allowed_packet这个参数的误解
    Linux hostname主机名配置文件与文件 /etc/hosts解析(copy来的,原作者看到了别打我)
    三个参数,对mysql存储限制的影响
    唉,没来这里好久了,也意味着我这一年多来没干什么正事儿,是回归的时候了!(简单谈谈爬虫的解析器)
    STM32 宏定义翻转端口
    ascii码表
    平均值算法
    stm8 16M晶振下精确软件延时
  • 原文地址:https://www.cnblogs.com/chenminklutz/p/7325988.html
Copyright © 2020-2023  润新知