spark-WordCount 源码分析图解

spark-WordCount 源码分析图解

1. maven依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.3.4</version>
            <exclusions>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--解决io.netty.buffer.PooledByteBufAllocator.defaultUseCacheForAllThreads()Z异常-->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.18.Final</version>
        </dependency>
    </dependencies>

2. scala代码

package spark

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

object WordCount {

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

    val conf = new SparkConf()
    //    设置spark程序的运行名称
    conf.setAppName("WordCount")
    //    设置spark是本地运行还是集群运行
    conf.setMaster("local")

    val sc = new SparkContext(conf)
    //  单词统计
    val fileRDD = sc.textFile("data/word")
    //    数据是: hello word
    //    val words = fileRDD.flatMap(_.split(" "))  简写
    val words = fileRDD.flatMap((x: String) => {
      x.split(" ")
    })
    //数据 :
    // hello
    // word
    //    然后再转成tuple键值对  hello 1 ,word 1
    //    val pariWord = words.map(new Tuple2(_,1))
    val pariWord = words.map((x: String) => {
      new Tuple2(x, 1)
    })
    // 然后转成 hello 2, word 1,Angzush 2
    val res = pariWord.reduceByKey((x: Int, y: Int) => {
      x + y
    })

    //    如果我想转为类似hello 拥有两次字符的有几个
    //    那res的结果进行map
    val fanzhuan = res.map((x) => {
      (x._2, 1)
    })
    //    然后再进行reduceByKey
    val value = fanzhuan.reduceByKey(_ + _)
    value.foreach(println)
    res.foreach(println)

//    为了进入localhost:4040而休眠 
    Thread.sleep(Long.MaxValue)
  }
}

图解:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Angzush

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值