spark 读写 mysql 数据demo(scala实现代码)

一、读操作

 1 package cn.guo.spark
 2 import java.sql.DriverManager
 3 import org.apache.spark.rdd.JdbcRDD
 4 import org.apache.spark.{SparkConf, SparkContext}
 5 object JdbcRDDDemo {
 6   def main(args: Array[String]) {
 7     val conf = new SparkConf().setAppName("JdbcRDDDemo").setMaster("local[2]")
 8     val sc = new SparkContext(conf)
 9     val connection = () => {
10       Class.forName("com.mysql.jdbc.Driver").newInstance()
11       DriverManager.getConnection("jdbc:mysql://localhost:3306/bigdata", "root", "123456")
12     }
13     val jdbcRDD = new JdbcRDD(
14       sc,
15       connection,
16       "SELECT * FROM ta where id >= ? AND id <= ?",
17       1, 4, 2,
18       r => {
19         val id = r.getInt(1)
20         val code = r.getString(2)
21         (id, code)
22       }
23     )
24     val jrdd = jdbcRDD.collect()
25     println(jrdd.toBuffer)
26     sc.stop()
27   }
28 }
29  

二、写操作

 1 /**
 2     * 存储到mysql
 3     */
 4   val dataToMysql = (partition: Iterator[(String)]) => {
 5     var conn: Connection = null
 6     var past: PreparedStatement = null
 7     try {
 8       conn = DriverManager.getConnection ("jdbc:mysql://master:3306/spark", "root", "root")
 9       //CREATE TABLE loc_base(loc VARCHAR(100),base_x DOUBLE,base_y DOUBLE,phone_num VARCHAR(20),time BIGINT)DEFAULT CHARSET=UTF8
10       val sql = "INSERT INTO  loc_base(loc,base_x,base_y,phone_num,time) VALUES(?,?,?,?,?)"
11       past = conn.prepareStatement (sql)
12       partition.foreach (info => {
13         val fields = info.split ("\t", -1)
14         past.setString (1, fields (0))
15         past.setDouble(2, fields (1).toDouble)
16         past.setDouble (3, fields (2).toDouble)
17         past.setString (4, fields (3))
18         past.setInt(5, fields (4).toInt)
19         past.execute ()
20       })
21     } catch {
22       case e: Exception => e.printStackTrace ()
23     } finally {
24       if (past != null) {
25         past.close ()
26       }
27       if (conn != null) {
28         conn.close ()
29       }
30     }
31   }

 

posted @ 2017-09-19 16:06  一醉方休  阅读(1367)  评论(0编辑  收藏  举报