spark pom.xml配置

该博客展示了如何使用Scala和Spark进行数据处理,包括读取文件、数据转换、求和、平均值计算以及排序。代码示例中,作者通过SparkContext读取文本文件,对数据进行分割、聚合和排序,演示了如何找出学生总成绩、平均成绩、数学最高分以及总分最高分。同时,还使用了Scala的Accumulator实现累加器功能。
摘要由CSDN通过智能技术生成

IDEA软件scala版本2.12.11

在这里插入图片描述
pml.xml

<dependencies>

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.12.4</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-compiler</artifactId>
        <version>2.12.4</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-reflect</artifactId>
        <version>2.12.4</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.12</artifactId>
        <version>3.0.0</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.15.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

代码:
import org.apache.spark.{SparkConf, SparkContext}

object Test01 {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName(“one”).setMaster(“local[*]”)
val context = new SparkContext(conf)
val iterator = context.textFile(“data/a.txt”)

val list = iterator.map(x => {
  val str = x.split(",")
  (str(0), str(1), str(2), str(3))
})

println("求每个学生的总成绩")
list.map(x=>{
  (x._1,(x._2.toInt+x._3.toInt+x._4.toInt))
}).foreach(println)

println("求每个学生的平均成绩")
list.map(x=>{
  (x._1,(x._2.toInt+x._3.toInt+x._4.toInt)/3)
}).foreach(println)

println("求数学第一名的学生的各门成绩")
list.map(x=>{
  (x._1,x._2.toInt,x._3,x._4)
}).sortBy(_._2,false)
  .collect().take(1).foreach(println)

println("求总分第一名的学生的各们成绩")
list.map(x=>{
  (x._1,x._2,x._3,x._4,(x._2.toInt+x._3.toInt+x._4.toInt))
}).sortBy(_._5,false)
  .collect().take(1).foreach(println)

println("使用累加器求每个学生的总成绩,不使用累加器不得分")
val leijia = context.collectionAccumulator("leijia")
list.groupBy(_._1).map(x=>{
  val tuples = x._2.map(x => {
    (x._2.toInt, x._3.toInt, x._4.toInt)
  }).map(x=>{
    x._1+x._2+x._3
  })
  (x._1,tuples)
}).foreach(println)

}
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值