用idea编写并运行第一个spark scala处理程序

1、用idea创建工程,类别为:Scala,选IDEA类型,下一步选择JDK 1.8和Scala SDK:scala-sdk-2.12.20(scala安装位置)。

2、点右键,添加框架支持(Add Framework Support),选择Maven支持。

3、pom.xml的内容:

bash 复制代码
<?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>groupId</groupId>
    <artifactId>scala01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.12.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-common-utils_2.13</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-network-common_2.13</artifactId>
            <version>3.5.2</version>
        </dependency>

    </dependencies>
</project>

刷新maven工程。

4、创建包:com.rainpet

包下新建Scala文件Hello.scala,内容如下:

bash 复制代码
package com.rainpet

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

object Hello {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("educoder").setMaster("local")
    val sc = new SparkContext(conf)
    //var file1=sc: SparkContext.textFile("hdfs://master:9000/user/input/1.txt")
    //ci'pin'tong'ji
    var file1 = sc.textFile("hdfs://master:8020/user/input/1.txt")
    var wordCounts = file1.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey(_ + _)
    wordCounts.collect().foreach(println)
  }

}

5、点运行或调试,即可以正常运行了。

相关推荐
武子康1 天前
大数据-277 Spark MLib-梯度提升树(GBDT)算法原理与工程实现指南
大数据·后端·spark
月明水寒1 天前
IDEA2026.1 vue文件报错
前端·javascript·vue.js·intellij-idea·idea·intellij idea
m0_752035631 天前
idea的debug configurations里面的shorten command line作用
java·ide·intellij-idea
再玩一会儿看代码1 天前
idea中快捷键详细总结整理
java·ide·经验分享·笔记·学习·intellij-idea
IT光1 天前
IDEA 2026.1 配置属性识别问题解决
java·ide·intellij-idea
jerryinwuhan1 天前
Spark SQL 详细讲义
大数据·sql·spark
QC班长1 天前
Maven公司私库配置踩坑点
java·服务器·maven·intellij-idea
人道领域2 天前
【黑马点评日记】高并发秒杀:库存超卖与锁机制解析
java·开发语言·redis·spring·intellij-idea
武子康2 天前
大数据-276 Spark MLib-深入理解Bagging与Boosting:集成学习核心算法对比与GBDT实战
大数据·后端·spark
1candobetter2 天前
JAVA后端开发——多模块项目重命名体系解析
java·开发语言·intellij-idea