spark streaming foreachRDD 使用

package com.immooc.spark

import java.sql.DriverManager

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

object ForeachRDDApp {
  def main(args: Array[String]): Unit = {


      val sparkConf = new SparkConf().setAppName("ForeachRDDApp").setMaster("local[2]")

       val ssc = new StreamingContext(sparkConf, Seconds(5))

       val lines = ssc.socketTextStream("localhost", 9998)

       val result = lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_)


        result.print()
    result.foreachRDD { rdd =>
      rdd.foreachPartition { partitionOfRecords =>
        // ConnectionPool is a static, lazily initialized pool of connections
        val connection = createConnection()
        partitionOfRecords.foreach(record => {
           var sql = "insert into wordcount(word, wordcount) values('" + record._1 + "'," + record._2 + ")"
           connection.createStatement().execute(sql)
        })

      }
    }

        ssc.start()
        ssc.awaitTermination()
  }


  def createConnection() = {
      Class.forName("com.mysql.jdbc.Driver")
      DriverManager.getConnection("jdbc:mysql://localhost:3306/test_spark","root","root")
  }
}

      mysql
      mysql-connector-java
      5.1.38
    

http://www.codeblogbt.com/archives/127717

你可能感兴趣的:(spark,streaming)