• SparkStreamingTest.scala


    /**
     * Created by root on 9/8/15.
     */
    import org.apache.spark._
    import org.apache.spark.rdd.RDD
    import org.apache.spark.streaming._
    import org.apache.spark.sql.SQLContext
    
    object SparkStreamingTest {
      def main(args: Array[String]) {
        //create a local StreamingContext with two working thread and batch interval of 1 second.
        val conf = new SparkConf().setAppName("Spark streaming test").setMaster("local[2]")
        val ssc = new StreamingContext(conf, Seconds(10))
        //val sc = ssc.sparkContext
        val lines = ssc.socketTextStream("localhost", 9999)
        val words = lines.flatMap(_.split(" "))
    
        //word count
        val pairs = words.map(word => (word, 1))
        val wordCounts = pairs.reduceByKey(_ + _)
        wordCounts.print()
    
        //convert spark streaming to sparksql
        words.foreachRDD((rdd: RDD[String], time: Time) => {
          val sqlContext = SQLContextSingleton.getInstance(rdd.sparkContext)
          import sqlContext.implicits._
          val wordsDataFrame = rdd.map(w => Record(w)).toDF()
          wordsDataFrame.registerTempTable("words")
          val wordsCountDataFrame = sqlContext.sql("select word, count(*) as total from words group by word")
          println(s"======================= $time =======================")
          wordsCountDataFrame.show()
        })
    
        ssc.start()
        ssc.awaitTermination()
      }
    }
    
    case class Record(word: String)
    
    object SQLContextSingleton {
      @transient private var instance: SQLContext = _
      def getInstance(sparkContext: SparkContext): SQLContext = {
        if (instance == null) {
          instance = new SQLContext(sparkContext)
        }
        instance
      }
    }
  • 相关阅读:
    codeforces
    codeforces
    HDU
    poj
    poj
    HDU Problem
    HDU 3555 Bomb 【数位dp】
    POJ 1942 Paths on a Grid【组合数学】
    杭电 Problem 2089 不要62 【数位dp】
    codeforces 486c-Palindrome Transformation【贪心】
  • 原文地址:https://www.cnblogs.com/sunflower627/p/4794675.html
Copyright © 2020-2023  润新知