spark - 将RDD保存到RMDB(MYSQL)数据库中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spark - 将RDD保存到RMDB(MYSQL)数据库中相关的知识,希望对你有一定的参考价值。
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.
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)数据库中的主要内容,如果未能解决你的问题,请参考以下文章
Spark 使用 Python:将 RDD 输出保存到文本文件中
如何通过 pyspark 以 gzip 格式保存 spark RDD
Spark 中啥样的 RDD 可以使用 saveAsNewAPIHadoopDataset 保存到 BigQuery 表中