码迷,mamicode.com
首页 > 数据库 > 详细

spark - 将RDD保存到RMDB(MYSQL)数据库中

时间:2016-01-28 13:49:44      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

SCALA连接数据库批量插入:

scala> import java.sql.DriverManager

scala> var url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"

scala> var username = "cui"

scala> var password = "dbtest"

scala> val conn= DriverManager.getConnection(url,username,password)

scala> val pstat = conn.prepareStatement ("INSERT INTO `TEST` (`ID`, `AGE`) VALUES (?, ?)")

scala> pstat.clearBatch

scala> pstat.setInt(1,501)

scala> pstat.setInt(2,501)

scala> pstat.addBatch

scala> pstat.setInt(1,502)

scala> pstat.setInt(2,502)

scala> pstat.addBatch

scala> pstat.setInt(1,503)

scala> pstat.setInt(2,503)

scala> pstat.addBatch

scala> pstat.executeBatch
res24: Array[Int] = Array(1, 1, 1)

 

RDD保存到数据库:

Just use foreachPartition to create and execute a SQL statement via JDBC over a batch of records. The code is just normal JDBC code.

https://community.cloudera.com/t5/Advanced-Analytics-Apache-Spark/Spark-Streaming-save-output-to-mysql-DB/td-p/25607

 

import java.sql.DriverManager

var data = sc.parallelize(Array( (1,10) ,(1,100), (1,1000), (1,10000),(2,10) ,(2,100), (2,1000), (2,10000)  ),2 )

data.foreachPartition(
    it =>{
        var url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"
        val conn= DriverManager.getConnection(url,"username","password")
        val pstat = conn.prepareStatement ("INSERT INTO `TEST` (`ID`, `AGE`) VALUES (?, ?)")
        for (obj <-it){
            pstat.setInt(1,obj._1)
            pstat.setInt(2,obj._2)
            pstat.addBatch
        }
        try{
            pstat.executeBatch
        }finally{
            pstat.close
            conn.close
        }
    }
)

 

spark - 将RDD保存到RMDB(MYSQL)数据库中

标签:

原文地址:http://www.cnblogs.com/tugeler/p/5165868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!