Spark MySQL read/write

Source: Internet
Author: User

Val data2mysql2 = (iterator:iterator[(String, Int)]) = = {    varConn:connection =NULL; varPs:preparedstatement =NULLVal SQL="Insert into Location_info (location,counts,accesse_date) VALUES (?,?,?)"    Try{conn= Drivermanager.getconnection ("Jdbc://localhist:3306/bigdata","Root","Root")      //The data for the entire partition uses a connIterator.foreach(line ={PS=conn.preparestatement (SQL) ps.setstring (1, Line._1) ps.setint (2, Line._2) ps.setdate (3,NewDate (System.currenttimemillis ())) Ps.executeupdate ()})}Catch {       CaseE:exception = println ("Mysql Exception")    } finally {      if(PS! =NULL) Ps.close ()if(Conn! =NULL) Conn.close ()}
Rddres2.foreachpartition (Data2mysql)
def Mysql2spark () {Val conf=NewSparkconf (). Setappname ("Jdbcrdddemo"). Setmaster ("Local[2]") Val SC=Newsparkcontext (conf) Val Connection= () + ={class.forname ("Com.mysql.jdbc.Driver"). newinstance () drivermanager.getconnection ("Jdbc:mysql://localhost:3306/bigdata","Root","Root")} Val jdbcrdd=NewJdbcrdd (SC, connection,//Location_info (location,counts      "SELECT ID, location from location_info where ID >=? and ID <=?",      1,4,2, R={val id= R.getint (1) Val Code= R.getstring (2) (ID, Code)}) Val Jrdd=Jdbcrdd.collect () println (Jdbcrdd.collect (). Tobuffer) Sc.stop ()}

Spark MySQL read/write

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.