Spark 实验:Scala手机号流量求和排序

一、实验描述

根据数据文件phone_data.txt按照如下需求:
1)统计每一个手机号耗费的总上行流量、下行流量、总流量
2)将统计结果按照手机归属地不同号段(手机号前3位)输出到不同文件中
3)根据需求1)产生的结果再次对总流量进行排序。
4)按照要求2)每个手机号段输出的文件中按照总流量内部排序。
数据:
参看 phone_data.txt 文件
输入数据格式:
在这里插入图片描述

输出数据格式:
在这里插入图片描述

二、实验分析

(1) 由于需要统计每一个手机号耗费的总上行流量、下行流量、总流量,故可以将手机号当作键、其上行流量与上行流量以及总流量构成的Array作为值保存在Map中。通过读取文件并遍历每一行,为Map添加键值或更新键的值。然后遍历Map将统计结果写入文件。
(2) 在(1)的基础上需要按照手机归属地不同号段(手机号前3位)输出到不同文件中,这一需求可以通过读取(1)的统计结果文件并遍历行,将每行的前三个字符作为键、行字符串构成的Array作为值保存在Map中,在遍历过程中添加键值或更新值(Array新增一个行字符串)。然后遍历Map,以键为文件名,并遍历值作为文件的内容。
(3) 在(1)的基础上需要按总流量排序,这一需求可以通过读取(1)的统计结果文件并遍历行,将行字符串作为Tuple的第一个元素、总流量作为Tuple的第二个元素保存在Array中,然后通过Array的sortBy方法排序并遍历排序结果同时写入文件中。
(4) 在(1)的基础上需要按照要求(2)每个手机号段输出的文件中按照总流量内部排序,这个需求其实是(2)、(3)的结合。这一需求可以通过读取(1)的统计结果文件并遍历行,将每行的前三个字符作为键、行字符串构成的Array作为值保存在Map中,在遍历过程中添加键值或更新值(Array新增一个行字符串)。然后遍历Map,以键为文件名构建PrintWriter对象,同时遍历值,把值的各个元素作为Tuple的第一个元素、并从各个元素分割总流量作为Tuple的第二个元素保存在Array中,通过Array的sortBy方法排序并遍历排序结果,同时通过构建的PrintWriter对象写入文件中。

三、实验代码

(1) PhoneFlowSum.scala

package cn.edu.neu.Question_1

import java.io.{File, PrintWriter}

import scala.io.Source

object PhoneFlowSum {

    def main(args: Array[String]): Unit ={
        val file = Source.fromFile("src/main/resources/phone_data.txt")
        val lines = file.getLines()
        val phoneFlowMap = scala.collection.mutable.Map[String, Array[Int]]()
        lines.foreach(line => {
            val strs = line.split(" {4}")
            if(phoneFlowMap.contains(strs(1))){
                val newFlow = Array(strs(8).toInt, strs(9).toInt, strs(8).toInt+strs(9).toInt)
                var i = 0
                for (elem <- newFlow) {
                    phoneFlowMap(strs(1))(i) += elem
                    i += 1
                }
            }else{
                phoneFlowMap+=(strs(1) -> Array(strs(8).toInt, strs(9).toInt, strs(8).toInt+strs(9).toInt))
            }
        })
        val out = new PrintWriter("src/main/resources/question_1.txt")
        for ((k, v) <- phoneFlowMap) {
            out.print(k+"\t")
            v.foreach(flow => out.print(flow+"\t"))
            out.println()
            out.flush()
        }
        out.close()
    }
}

在这里插入图片描述

(2) PhoneFlowSumPartition.scala

package cn.edu.neu.Question_2

import java.io.PrintWriter

import scala.io.Source

object PhoneFlowSumPartition {
    def main(args: Array[String]): Unit ={
        val file = Source.fromFile("src/main/resources/question_1.txt")
        val lines = file.getLines()
        val partitions = scala.collection.mutable.Map[String, Array[String]]()
        lines.foreach(line => {
            if(partitions.contains(line.substring(0,3))){
                partitions.update(line.substring(0,3),partitions(line.substring(0,3)):+line)
            }else{
                partitions+=(line.substring(0,3) -> Array(line))
            }
        })
        for ((k, v) <- partitions) {
            val out = new PrintWriter("src/main/resources/question_2/"+k+".txt")
            v.foreach(phoneFlow => out.println(phoneFlow))
            out.flush()
            out.close()
        }
    }
}

在这里插入图片描述

(3) PhoneFlowSort.scala

package cn.edu.neu.Question_3

import java.io.PrintWriter

import scala.io.Source

object PhoneFlowSumSort {
    def main(args: Array[String]): Unit ={
        val file = Source.fromFile("src/main/resources/question_1.txt")
        val lines = file.getLines()
        val flowArrs = new Array[(String, Int)](20)
        var i = 0
        for (elem <- lines) {
            println(elem)
            val strs = elem.split("\t")
            flowArrs(i)=(elem, strs(3).toInt)
            i += 1
        }
        val sortFlow = flowArrs.sortBy(flow => flow._2)
        // println(sortFlow.getClass)
        val out = new PrintWriter("src/main/resources/question_3.txt");
        sortFlow.foreach(elem => {out.println(elem._1);out.flush()})
        out.close()
    }
}

在这里插入图片描述

(4) PhoneFlowSumPartitionSort.scala

package cn.edu.neu.Question_4

import java.io.PrintWriter

import scala.io.Source

object PhoneFlowSumPartitionSort {
    def main(args: Array[String]): Unit ={
        val file = Source.fromFile("src/main/resources/question_1.txt")
        val lines = file.getLines()
        val partitions = scala.collection.mutable.Map[String, Array[String]]()
        lines.foreach(line => {
            if(partitions.contains(line.substring(0,3))){
                partitions.update(line.substring(0,3),partitions(line.substring(0,3)):+line)
            }else{
                partitions+=(line.substring(0,3) -> Array(line))
            }
        })
        for ((k, v) <- partitions) {
            val flowArrs = new Array[(String, Int)](v.length)
            val out = new PrintWriter("src/main/resources/question_4/"+k+".txt")
            var i = 0
            for (elem <- v) {
                val strs = elem.split("\t")
                flowArrs(i) = (elem, strs(3).toInt)
                i += 1
            }
            val sortFlow = flowArrs.sortBy(flow => flow._2)(Ordering.Int.reverse)
            for (elem <- sortFlow) {out.println(elem._1);out.flush()}
            out.close()
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值