Spark Scala 实现二次排序和相加

使用自定义MR实现如下逻辑

product_no	lac_id	moment	start_time	user_id	county_id	staytime	city_id
13429100031	22554	8	2013-03-11 08:55:19.151754088	571	571	282	571
13429100082	22540	8	2013-03-11 08:58:20.152622488	571	571	270	571
13429100082	22691	8	2013-03-11 08:56:37.149593624	571	571	103	571
13429100087	22705	8	2013-03-11 08:56:51.139539816	571	571	220	571
13429100087	22540	8	2013-03-11 08:55:45.150276800	571	571	66	571
13429100082	22540	8	2013-03-11 08:55:38.140225200	571	571	133	571
13429100140	26642	9	2013-03-11 09:02:19.151754088	571	571	18	571
13429100082	22691	8	2013-03-11 08:57:32.151754088	571	571	287	571
13429100189	22558	8	2013-03-11 08:56:24.139539816	571	571	48	571
13429100349	22503	8	2013-03-11 08:54:30.152622440	571	571	211	571


字段解释:
product_no:用户手机号;
lac_id:用户所在基站;
start_time:用户在此基站的开始时间;
staytime:用户在此基站的逗留时间。


需求描述:
根据lac_id和start_time知道用户当时的位置,根据staytime知道用户各个基站的逗留时长。根据轨迹合并连续基站的staytime。
最终得到每一个用户按时间排序在每一个基站驻留时长
期望输出举例:

13429100031	22554	8	2013-03-11 08:55:19.151754088	571	571	282	571
13429100082	22540	8	2013-03-11 08:58:20.152622488	571	571	270	571
13429100082	22691	8	2013-03-11 08:56:37.149593624	571	571	370	571
13429100082	22540	8	2013-03-11 08:55:38.140225200	571	571	133	571
13429100087	22705	8	2013-03-11 08:56:51.139539816	571	571	220	571
13429100087	22540	8	2013-03-11 08:55:45.150276800	571	571	66	571
13429100140	26642	9	2013-03-11 09:02:19.151754088	571	571	18	571
13429100189	22558	8	2013-03-11 08:56:24.139539816	571	571	48	571
13429100349	22503	8	2013-03-11 08:54:30.152622440	571	571	211	571


分析上面的结果: 
第一列升序,第四列时间降序。因此,首先需要将这两列抽取出来,然后自定义排序。


import com.datascience.test.{Track, SecondarySort}
import org.apache.spark.{SparkConf, SparkContext}


/**
  * Created by on 2017/11/13.
  */
object FindTrack {

  def parse(line: String) = {
    val pieces = line.split("\t")
    val product_no = pieces(0).toString
    val lac_id = pieces(1).toString
    val moment = pieces(2).toString
    val start_time =  pieces(3).toString
    val user_id = pieces(4).toString
    val county_id = pieces(5).toString
    val staytime = pieces(6).toInt
    val city_id = pieces(7).toString
    val se = new SecondarySort(product_no, start_time)
    val track = new Track(product_no, lac_id,moment,start_time,user_id,county_id,staytime, city_id)
    (se,track)
  }

  def isHeader(line: String): Boolean = {
    line.contains("product_no")
  }

  def compTo(one:String,another:String):Int = {
    val len = one.length -1
    val v1 = one.toCharArray
    val v2 = another.toCharArray
    for(i <- 0 to len){
      val c1 = v1(i)
      val c2 = v2(i)
      if(c1 != c2) return c1 -c2
    }
    return 0
  }

  def add(x:Track, y:Track): Track = {
    if (compTo(x.startTime, y.startTime) < 0) {
      new Track(x.productNo,x.lacId,x.moment,x.startTime,x.userId,x.countyId,x.staytime + y.staytime,x.cityId)
    }
    else {
      new Track(y.productNo,y.lacId,y.moment,y.startTime,y.userId,y.countyId,x.staytime + y.staytime,y.cityId)
    }
  }

  def get(x:(SecondarySort,Iterable[Track])) :Track = {
      val xIter = x._2.head
     xIter
  }

  def main(args: Array[String]) {
    val sc = new SparkContext(new SparkConf().setAppName("FindTrack"))
    val base = "/user/ds/"
    val rawData = sc.textFile(base + "track.txt")

    val mds = rawData.filter(x => !isHeader(x)).map{x => parse(x)}.groupByKey().sortByKey(true).collect().map{x => get(x)}.reduceLeft{ (x, y) =>
      if((x.productNo == y.productNo && x.lacId == y.lacId))
        add(x, y)
      else
      {
        println(x)
        y
      }
    }
  }
}


二次识别比较类

/**
  * Created by on 2017/11/13.
  */
class SecondarySort(val first:String, val second:String) extends Ordered[SecondarySort] with Serializable{

  def compTo(one:String,another:String):Int = {
    val len = one.length -1
    val v1 = one.toCharArray
    val v2 = another.toCharArray
    for(i <- 0 to len){
      val c1 = v1(i)
      val c2 = v2(i)
      if(c1 != c2) return c1 -c2
    }
    return 0
  }

  override def compare(that: SecondarySort): Int = {
  val minus = compTo(this.first,that.first)
  if(minus !=0) return  minus
  return  -compTo(this.second,that.second)
  }

  override def equals(obj:Any) :Boolean  = {
    if(!obj.isInstanceOf[SecondarySort]) return false
    val obj2 = obj.asInstanceOf[SecondarySort]
    return (this.first==obj2.first) && (this.second==obj2.second)
  }

  override def toString :String = {
    first +" "+ second
  }

  override  def hashCode :Int = {
    return this.first.hashCode()+this.second.hashCode();
  }
}



/**
  * Created by on 2017/11/13.
  */
class Track extends  java.io.Serializable  {

  var productNo : String = ""
  var lacId : String  = ""
  var moment : String = ""
  var startTime : String = ""
  var userId  : String = ""
  var countyId : String = ""
  var staytime : Int  = 0
  var cityId  : String = ""

  def this(_productNo: String, _lacId: String,_moment: String, _startTime: String,_userId: String,_countyId: String,_staytime: Int, _cityId: String)
  {
    this()
    this.productNo = _productNo
    this.lacId = _lacId
    this.moment = _moment
    this.startTime = _startTime
    this.userId = _userId
    this.countyId = _countyId
    this.staytime = _staytime
    this.cityId = _cityId
  }

  override def toString :String = {
      productNo +" "+ lacId + " "+ moment + " " + startTime + "  " + userId  + "  "+ countyId + "  "+ staytime + "  " + cityId
  }
}

结果图:二次排序实现了



最后的加法操作还是有点问题,如果大家有好大建议,请给我留言,不胜感激






好的,我会给你解答关于Spark RDD实现二次排序的问题。 首先,对于二次排序(Secondary Sort),我们需要根据两个维度来对数据进行排序,比如要按照学生的学号(第一维)和成绩(第二维)进行排序。在Spark中,可以使用sortByKey函数对数据进行排序,但默认只能按照一个维度排序。 为了实现二次排序,我们需要借助Scala的Tuple(元组)类型,将学生的学号和成绩组成一个二元组,作为RDD中的元素。然后,我们可以通过对元组进行比较,实现二次排序的效果。 下面是一个Scala实现二次排序的示例代码: ```scala // 生成包含二元组的RDD val data = sc.parallelize(Seq( (1, 85), (2, 92), (3, 76), (1, 90), (2, 88), (3, 80) )) // 按照第一维升序、第二维降序排序 val sortedData = data .sortByKey() // 按照第一维升序排序 .map(_.swap) // 交换元组的顺序,变成(成绩, 学号) .sortByKey(false) // 按照第二维降序排序 .map(_.swap) // 再次交换元组的顺序,变成(学号, 成绩) // 输出排序结果 sortedData.foreach(println) ``` 在以上示例代码中,我们首先生成一个包含二元组的RDD,其中第一维为学号,第二维为成绩。然后,我们先按照第一维升序排序,再交换元组的顺序,变成(成绩, 学号),再按照第二维降序排序,最后再次交换元组的顺序,得到最终的排序结果。 这就是Spark RDD实现二次排序的基本方法,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值