Spark MySql连接器Jar

时间:2018-09-25 04:49:20

标签: mysql apache-spark jdbc

我正在从Spark应用程序(spark-2.0.0-bin-hadoop2.7)连接到MySql数据库。连接时,我遇到了错误。

这是我的代码:

def main(args: Array[String]) {

    val conf = new SparkConf()
    .setAppName("LoadMySql")
    .setMaster("local[*]")
    .set("spark.sql.warehouse.dir", "file:///C:/temp")

    val spark = SparkSession
    .builder
    .config(conf)
    .getOrCreate()

    val option = Map("url" -> "jdbc://mysql://IPAddress:3306/TestDB", "dbtable" -> "Users", "user" -> "root", "password" -> "*****")

    val dataRDD = spark.sqlContext.read.format("org.apache.spark.sql.jdbc").options(option).load()

错误:

Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Exception in thread "main" java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(Unknown Source)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$2.apply(JdbcUtils.scala:54)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$2.apply(JdbcUtils.scala:54)
    at scala.Option.getOrElse(Option.scala:121)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.createConnectionFactory(JdbcUtils.scala:53)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:123)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation.<init>(JDBCRelation.scala:117)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:53)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:315)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:149)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:122)
    at com.Lab.SparkAPI.LoadMySql$.main(LoadMySql.scala:26)
    at com.Lab.SparkAPI.LoadMySql.main(LoadMySql.scala)

我已经通过“添加外部Jar”将mysql-connector-java-5.1.27-bin.jar包含到我的eclipse项目中。

我尝试了几种不同版本的jar,但到目前为止还没有运气。

任何想法我需要连接到MySQL的哪个版本?


[编辑]

最后,我能够从MySQL连接并加载数据...

我做了以下2个设置...

  • 将所有名称授予DBName。*到“ root” @'%',由“ password”标识;
  • 修改/etc/mysql/mysql.conf.d/mysqld.cnf以便在[mysqld]部分下注释#bind-address = 127.0.0.1

此问题的回答here

1 个答案:

答案 0 :(得分:1)

我想您正在使用Scala作为编程语言。您没有提供足够的信息,例如我们用来连接MySQL的代码。无论如何,您似乎尚未为MySQL指定任何驱动程序。试试吧:

  1. 将MySQL导入您的proyect

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
    
  2. 创建Properties对象,并指定jdbc驱动程序。然后,使用该对象向MySQL写入数据/从MySQL读取数据。

       
    val prop = new Properties()
    
    prop.setProperty("user", dbUser)
    prop.setProperty("password", dbPassword)
    prop.put("driver", "com.mysql.jdbc.Driver");
    
    df.write.mode("append").jdbc("jdbc:mysql://" + dbHost + "/" + dbName + "?rewriteBatchedStatements=true", "TABLE_NAME", prop)
    

希望有帮助。