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")
  }
}
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

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

猜你喜欢

转载自blog.csdn.net/fox64194167/article/details/80737212
今日推荐