Spark-Save the Rdd to the Rmdb (MYSQL) database

Source: Internet
Author: User
Tags bulk insert

Scala Connection Database BULK INSERT:

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)

The RDD is saved to the database:

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

ImportJava.sql.DriverManagervar 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-Save the Rdd to the Rmdb (MYSQL) database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.