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