Spark(1)-wordCount入门

1. 创建Maven项目

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wakedata</groupId>
    <artifactId>code</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <spark.version>3.4.1</spark.version>
        <scala.version>2.12.14</scala.version>
    </properties>

    <dependencies>
       <!-- scala依赖       -->
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <!-- spark core 即为spark内核 ,其他⾼级组件都要依赖spark core       -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>${spark.version}</version>
        </dependency>
    </dependencies>

    <build>
        <!--scala待编译的文件目录-->
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <!--scala插件-->
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <!--<arg>-make:transitive</arg>--><!--scala2.11 netbean不支持这个参数-->
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--manven打包插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>reference.conf</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>cn.itcast.rpc.Master</mainClass> <!--main方法-->
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2.目录结构

3. 代码实现

Scala 复制代码
package sparkCore

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


/***
 * 1. 创建SparkContext
 * 2. 创建RDD
 * 3. 调用RDD的Transformation算子
 * 4. 调用Action
 * 5. 释放资源
 */

object wordcount_01 {

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

    val conf:SparkConf = new SparkConf().setAppName("WordCount").setMaster("local")
    //创建SparkContext,使⽤SparkContext来创建RDD
    val sc: SparkContext = new SparkContext(conf)

    //spark写Spark程序,就是对抽象的神奇的⼤集合【RDD】编程,调⽤它⾼度封装的API //使⽤SparkContext创建RDD
    val lines: RDD[String] = sc.textFile("./data/words.txt")

    //切分压平
    val words: RDD[String] = lines.flatMap(_.split(" "))

    将单词和⼀组合放在元组中
    val wordsAndOne: RDD[(String, Int)] = words.map((_, 1))

    //分组聚合,reduceByKey可以先局部聚合再全局聚合
    val reduced: RDD[(String, Int)] = wordsAndOne.reduceByKey(_ + _)

    //排序
    val sorted: RDD[(String, Int)] = reduced.sortBy(_._2, false)

    //打印结果
    sorted.foreach(line => print(line))

    //释放资源
    sc.stop()

  }

}

运行结果:

相关推荐
boonya3 小时前
Elasticsearch核心原理与面试总结
大数据·elasticsearch·面试
TDengine (老段)4 小时前
TDengine 时间函数 WEEKDAY() 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
LQ深蹲不写BUG7 小时前
ElasticSearch 基础内容深度解析
大数据·elasticsearch·搜索引擎
Debug_Snail9 小时前
【营销策略算法】关联规则学习-购物篮分析
大数据·人工智能
BYSJMG9 小时前
计算机毕设大数据方向:基于Spark+Hadoop的餐饮外卖平台数据分析系统【源码+文档+调试】
大数据·hadoop·分布式·python·spark·django·课程设计
java水泥工10 小时前
基于Echarts+HTML5可视化数据大屏展示-茶叶种植大数据溯源平台
大数据·echarts·html5
华略创新11 小时前
标准化与定制化的平衡艺术:制造企业如何通过灵活配置释放系统价值
大数据·人工智能·制造·crm·管理系统·erp·企业管理
半夏陌离13 小时前
SQL 实战指南:电商订单数据分析(订单 / 用户 / 商品表关联 + 统计需求)
java·大数据·前端
成长之路51414 小时前
【面板数据】各省制造业出口技术复杂度数据集(2010-2023年)
大数据