栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

spark pom.xml配置

spark pom.xml配置

IDEA软件scala版本2.12.11


pml.xml



    
        org.scala-lang
        scala-library
        2.12.4
    
    
        org.scala-lang
        scala-compiler
        2.12.4
    
    
        org.scala-lang
        scala-reflect
        2.12.4
    
    
        log4j
        log4j
        1.2.12
    
    
        org.apache.spark
        spark-core_2.12
        3.0.0
    




    
        
            org.scala-tools
            maven-scala-plugin
            2.15.2
            
                
                    
                        compile
                        testCompile
                    
                
            
        
    

代码:
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)

}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/734802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号