????????
????Spark????????????м????RDD?????????????????HDFS??Hive??HBase?У???????棬?????RDD?????????????????????Mysql??????????RDMS??.
????????д??????о??????????????????? stackoverflow???к??????:
????Error writing spark dataframe to mysql table JDBC batch insert performance
????????Щ????????
????- Using Apache Spark and MySQL for Data Analysis
????- spark 1.3.0 ??dataframe????д??Hive??????
????- Spark????????(Mysql)???????????
????- ????java??????JDBC????????????????
????- Spark????——?????Hbase+Mysql??
????RDD
????Spark SQL???JDBC????MySQL??д????
??????RDD
????import java.sql.{Date?? DriverManager?? PreparedStatement?? Connection}
????/*
????tableName = "tempTableName"
????columns = [key : String?? value : Int]
????DBIP = 10.10.10.10
????DBPort = 8888
????DB = tempDB
????*/
????def connection2Mysql() = {
????var conn : Connection = null
????var ps : PreparedStatement = nulll
????val userName = "admin"
????val passwd = "admin"
????val key = "Tom"
????val value = 1024
????val sql = "INSERT INTO tempTableName(key??value) values (????)"
????try {
????Class.forName("com.mysql.jdbc.Driver").newInstance
????conn = DriverManager.getConnection("jdbc:mysql://10.10.10.10:8888/tempDB"?? userName?? passwd)
????ps = conn.prepareStatement(sql)
????ps.setDate(1?? key)
????ps.setLong(2?? value)
????ps.executeUpdate()
????} catch {
????case e: Exception => println("----> Exception! : " + e + "<-----")
????} finally {
????if (ps != null) ps.close()
????if (conn != null) conn.close()
????}
????}
?????????????????????????????????client???й????飬????????е?????????submit????YARN??????????
????java.sql.SQLException: No suitable driver found for jdbc:mysql://10.10.10.10:8888/tempDB
????????
????Error:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
????????????????????????????????????У????????????????Class.forName("com.mysql.jdbc.Driver").newInstance ???д????????????. ???д??????????driver?????????Driver??????????:
????/**
????* The Java SQL framework allows for multiple database drivers. Each driver should supply a class that implements the Driver interface
????*
????* The DriverManager will try to load as many drivers as it can find and then for any given connection request?? it will ask each driver in turn to try to
????* connect to the target URL.
????*
????* It is strongly recommended that each Driver class should be small and standalone so that the Driver class can be loaded and queried without bringing in vast
????* quantities of supporting code.
????*
????* When a Driver class is loaded?? it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a
????* driver by doing Class.forName("foo.bah.Driver")
????*/
????public class Driver extends NonRegisteringDriver implements java.sql.Driver {
????// Register ourselves with the DriverManager
????static {
????try {
????java.sql.DriverManager.registerDriver(new Driver());
????} catch (SQLException E) {
????throw new RuntimeException("Can't register driver!");
????}
????}
????/**
????* Construct a new driver and register it with DriverManager
????* @throws SQLException if a database error occurs.
????*/
????public Driver() throws SQLException {
????// Required for Class.forName().newInstance()
????}
????}
????????????????ж???????
????1. ??–jars???????????Mysql jar??????????????. ??????????–driver-class-path??????????driver??classpath.
????$  bin/spark-submit --master local[2] --driver-class-path lib/mysql-connector-java-5.1.38.jar --class temp.jar
?????????????????node????? link
????–driver-class-path driver?????????????????????e??(:)???
????–jars driver??executor???????????????????????(??)???
????2. ????????????????????н????jar??????.
????????????????????????jar?????????jar???У??????????????????????????????????.
???????磬??build.sbt???????????
????// https://mvnrepository.com/artifact/mysql/mysql-connector-java
????libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.38"
?????????Artifacts?????????jar??.