这是林子雨spark实验5的第三问:配置Spark通过JDBC连接数据库MySQL,编程实现利用DataFrame插入下列数据到MySQL, 最后打印出 age 的最大值和 age 的总和
下面贴出代码(测试过ok)
1 importjava.util.Properties2 importorg.apache.spark.sql.types._3 importorg.apache.spark.sql.Row4 importorg.apache.spark.SparkConf5 importorg.apache.spark.SparkContext6 importorg.apache.spark.sql.SQLContext7 object TestMySQL {8 def main(args: Array[String]) {9 val conf = newSparkConf()10 conf.setMaster("local")11 .setAppName("TestMySQL") //设置运行方式为本地
12 val sc = newSparkContext(conf)13 var sqlContext = newSQLContext(sc)14 val employeeRDD = sqlContext.sparkContext.parallelize(Array("3 Mary F 26","4 Tom M 23")).map(_.split(" "))15 val schema = StructType(List(StructField("id", IntegerType, true),StructField("name", StringType, true),StructField("gender", StringType, true),StructField("age", IntegerType, true))