Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package org.bianqi.spark
  2.  
  3. import java.util.Properties
  4.  
  5. import org.apache.spark.sql.{Row, SQLContext}
  6. import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
  7. import org.apache.spark.{SparkConf, SparkContext}
  8.  
  9. /**
  10. * Created by BQ
  11. * School www.qqhru.edu.cn
  12. * Date 2017/8/11
  13. */
  14. object JdbcRDD {
  15. def main(args: Array[String]): Unit = {
  16. val conf = new SparkConf().setAppName("MYSQL-DEMO")
  17. val sc = new SparkContext(conf)
  18. val sqlContext = new SQLContext(sc)
  19. val personRDD = sc.parallelize(Array("1 tom 4",
  20. "2 jerry 3","3 kitty 6")).map(_.split(" "))
  21. val schema = StructType(
  22. List(
  23. StructField("id",IntegerType,true),
  24. StructField("name",StringType,true),
  25. StructField("age",IntegerType,true)
  26. )
  27. )
  28. val rowRDD = personRDD.map(p=>Row(p(0).toInt,
  29. p(1).trim,p(2).toInt))
  30. val personDataFrame = sqlContext.createDataFrame(rowRDD,schema)
  31. val prop = new Properties()
  32. prop.put("user","root")
  33. prop.put("password","123")
  34. personDataFrame.write.mode("append")
  35. .jdbc("jdbc:mysql://localhost:3306/user","bigdata.person", prop)
  36. sc.stop()
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement