Sparkcore----三层架构模型

一、建立application、controller、service和dao文件。
二、各文件的代码
application代码:

import com.lzl.bigdata.spark.core.framework.controller.WordCountController
import org.apache.spark.{SparkConf, SparkContext}

object WordCountApplication extends App {

  val sparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
  val sc : SparkContext = new SparkContext(sparkConf)

  val controller =new WordCountController()
  controller.dispatch()
  //关闭连接
  sc.stop()
}

controller层代码:

import com.lzl.bigdata.spark.core.framework.service.WordCountService

class WordCountController {

  private val WordCountService = new WordCountService()

  //调度
  def dispatch(): Unit = {
   val word2Count = WordCountService.dataAnalysis()

    word2Count.foreach(println)
  }
}

Service层代码:

import com.lzl.bigdata.spark.core.framework.dao.WordCountDao
import org.apache.spark.rdd.RDD

//服务层
class WordCountService {

  private val WordCountDao = new WordCountDao()

  //数据分析
  def dataAnalysis():Array[(String,Int)] ={
    //读取数据
    val fileRDD =WordCountDao.readFile("datas/word.txt")
    // 将文件中的数据进行分词
    val wordRDD = fileRDD.flatMap( _.split(" ") )
    // 转换数据结构word => (word, 1)
    val word2OneRDD: RDD[(String, Int)] = wordRDD.map((_,1))
    // 将转换结构后的数据按照相同的单词进行分组聚合
    val word2CountRDD: RDD[(String, Int)] = word2OneRDD.reduceByKey(_+_)
    // 将数据聚合结果采集到内存中
    val word2Count: Array[(String, Int)] = word2CountRDD.collect()
    //返回 结果
    word2Count
  }
}

dao层代码:

import com.lzl.bigdata.spark.core.framework.application.WordCountApplication.sc
import org.apache.spark.rdd.RDD

// 持久层
class WordCountDao {
  // 读取文件数据
  def readFile(path:String):RDD[String] = {
    sc.textFile("datas/word.txt")
  }
}

三、在application运行结果如下:
Sparkcore----三层架构模型_第1张图片

你可能感兴趣的:(spark)