欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spark-jdbc No suitable driver found错误处理

程序员文章站 2024-03-14 17:05:46
...

错误如图:

spark-jdbc No suitable driver found错误处理

代码:

object Mysql extends App{

    val sparkConf = new SparkConf()//.setAppName("RDDRelation").setMaster("local[1]")
    val sc=new SparkContext(sparkConf)
    val sqlContext = new SQLContext(sc)
  sqlContext.setConf("spark.sql.warehouse.dir","file:///")

  val url="jdbc:mysql://114.55.96.216:3306/test"
//  val url = "jdbc:mysql://114.55.96.216:3306/test?user=test&password=1234"
  val prop = new java.util.Properties
  prop.setProperty("user","test")
  prop.setProperty("password","1234")
  //jdbc1*******************
  val number=10
  val str1=s"userid>$number"
  val s=sqlContext.read.format("jdbc").jdbc(url,"userinfo",Array[String](str1),prop).select("username").rdd.distinct().collect()
  val s1=s.map(_.toString().replaceAll("\\[|\\]",""))
  println("*"*50)
  s1.foreach(println(_))
  println("*"*50)
 //jdbc2*******************
  val jdbcDF = sqlContext.read.format("jdbc")
    .options(Map("url" -> "jdbc:mysql://114.55.96.216:3306/test",
      "driver" -> "com.mysql.jdbc.Driver",
      "dbtable" -> "userinfo" ,
      "user" -> "test",
      "password" -> "1234")).load()
  jdbcDF.show()
//  sqlContext.read.format("jdbc")

}

依赖:

spark-jdbc No suitable driver found错误处理

mysql的jar包在打包时时就已经打进去了,
提交到集群上运行时jdbc1错误。如图1,jdbc2正常
但是jdbc2是拉取整个表的数据,不能条件从数据库查询出部分数据,
解决方法,上传一个 mysql-connector-java-5.1.35.jar至服务器

submit时指定jar包

如:
  bin/spark-submit \
 --master  yarn \
 --class test.Mysql \
 --driver-class-path /usr/hdp/2.4.0.0-169/spark/lib/mysql-connector-java-5.1.35.jar \
 /usr/hdp/2.4.0.0-169/spark/jar/test-scala.jar


,问题解决