spark window下運(yùn)行 初試

前提 java scala hadoop intellij
下載spark http://spark.apache.org/downloads.html

Paste_Image.png

intellij新建scala項(xiàng)目
配置引入庫(kù) 最終效果如下,需引入hadoop的庫(kù)

Paste_Image.png
測(cè)試文件結(jié)構(gòu)

本地測(cè)試生成測(cè)試文件

/**
  * Created by Administrator on 2017/2/10.
  */

package com.soecode.SparkDemo

import java.io.PrintWriter

/**
  * 模擬一個(gè)城市人口
  */
object CreateTestFile {
  def main(args: Array[String]) {
    val start = System.currentTimeMillis();
    val out = new PrintWriter("d://renkou.txt")

    for (i <- 1 to 20000000) {
      out.println(i + "," + getName + "," + getBirth + "," + getSex)
    }
    out.close()
    val end = System.currentTimeMillis();
    print("任務(wù)結(jié)束,耗時(shí):" + (end - start) + "ms")
  }

  //隨機(jī)產(chǎn)生名
  def getName: String = {
    val chs = "abcdefghijklmnopqrstuvwxyz"
    val len = (1 + 5 * Math.random()).toInt
    var str = ""
    for (i <- 1 to len) {
      val l = (0 + 25 * Math.random()).toInt
      str += chs(l)
    }
    str
  }

  //隨機(jī)產(chǎn)生出生日期
  def getBirth: String = {
    val year = (1949 + 67 * Math.random()).toInt
    val month = (1 + 12 * Math.random()).toInt
    val day = (1 + 30 * math.random).toInt
    year + "-" + month + "-" + day
  }

  //隨機(jī)產(chǎn)生性別
  def getSex: Integer = if (Math.random() > 0.3) 1 else 0
}

統(tǒng)計(jì)性別

/**
  * Created by Administrator on 2017/2/10.
  */

package com.soecode.SparkDemo

import org.apache.spark.{SparkConf, SparkContext}

/**
  * 分析男女分布
  */
object StatBG {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Demo").setMaster("local");//spark conf
    val sc = new SparkContext(conf);//spark上下文

    println("任務(wù)開(kāi)始")
    val start = System.currentTimeMillis();
    val lines = sc.textFile("d://renkou.txt")//讀取本地文件建立RDD
    //使用map操作,形成新的集合。 如:Map(1,0,1,1,1)  0 代表女,1代表男
    val result = lines.map(s=>{
      val sp = s.split(",")
      sp(3)
    }).countByValue

    val end = System.currentTimeMillis();

    println("任務(wù)結(jié)束,耗時(shí):"+(end-start)+"ms"); /*32128ms*/
    println(result) /*Map(0 -> 6000325, 1 -> 13999675)*/
  }
}

統(tǒng)計(jì)星座

/**
  * Created by Administrator on 2017/2/10.
  */

package com.soecode.SparkDemo

import org.apache.spark.{SparkConf, SparkContext}

/**
  * 分析星座分布
  */
object StatBirth extends Serializable {
  val dayArr = Array[Integer](20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22)
  val constellationArr = Array[String]("摩羯座", "水瓶座", "雙魚(yú)座", "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "摩羯座")

  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Demo").setMaster("local");
    val sc = new SparkContext(conf);
    println("任務(wù)開(kāi)始")
    val start = System.currentTimeMillis();

    val lines = sc.textFile("d://renkou.txt")

    //RDD進(jìn)行map操作,獲取每行,然后split分割,換算星座返回新的map(金牛座,水瓶座,……)
    val result = lines.map(s => {
      val sp = s.split(",")
      val sp_birth = sp(2).split("-")
      val month = sp_birth(1).toInt
      val day = sp_birth(2).toInt
      getConstellation(month, day)
    }).countByValue

    val end = System.currentTimeMillis();
    println("任務(wù)結(jié)束,耗時(shí):" + (end - start) + "ms");

    for (m <- result) println(m._1 + ":" + m._2)
    /*任務(wù)結(jié)束,耗時(shí):50054ms
巨蟹座:1719635
射手座:1610575
雙魚(yú)座:1778776
白羊座:1613289
處女座:1665877
雙子座:1723186
金牛座:1722383
獅子座:1669192
天秤座:1721118
水瓶座:1610483
天蝎座:1611749
摩羯座:1553737*/
  }

  //獲取星座
  def getConstellation(month: Integer, day: Integer): String = {
    if (day < dayArr(month - 1)) constellationArr(month - 1) else constellationArr(month)
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容