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()

  }

}

运行结果:

相关推荐
qyresearch_1 小时前
全球碳化硅晶片市场深度解析:技术迭代、产业重构与未来赛道争夺战(2025-2031)
大数据·人工智能
猫猫头有亿点炸2 小时前
大数据可能出现的bug之flume
大数据·bug·flume
小奕同学A2 小时前
数字化技术的五个环节:大数据、云计算、人工智能、区块链、移动互联网
大数据·人工智能·云计算
计算机毕设定制辅导-无忧学长7 小时前
TDengine 集群高可用方案设计(一)
大数据·时序数据库·tdengine
言之。8 小时前
别学了,打会王者吧
java·python·mysql·容器·spark·php·html5
技术项目引流10 小时前
elasticsearch查询中的特殊字符影响分析
大数据·elasticsearch·搜索引擎
EasyDSS11 小时前
视频监控从安装到优化的技术指南,视频汇聚系统EasyCVR智能安防系统构建之道
大数据·网络·网络协议·音视频
lilye6611 小时前
精益数据分析(20/126):解析经典数据分析框架,助力创业增长
大数据·人工智能·数据分析
苏小夕夕11 小时前
spark-streaming(二)
大数据·spark·kafka