1. 程式人生 > >43.top10熱門品類之使用Scala實現二次排序

43.top10熱門品類之使用Scala實現二次排序

本文為《Spark大型電商專案實戰》 系列文章之一,主要介紹使用Scala實現二次排序。

程式碼實現

在Scala IDE中的包com.erik.sparkproject中建立SortKey.scala,實現二次排序,程式碼如下:

package com.erik.sparkproject

/**
 * @author Erik
 */
class SortKey(val clickCount: Int, 
    val orderCount: Int, 
    val payCount: Int)
    extends Ordered[SortKey] with Serializable {

  def
compare(that: SortKey): Int = { if(clickCount - that.clickCount != 0) { clickCount - that.clickCount } else if(orderCount - that.orderCount != 0) { orderCount - that.orderCount } else if(payCount - that.payCount != 0) { payCount - that.payCount } else { 0 } } }

測試

對實現的二次排序方法進行測試,新建SortKeyTest.scala,程式碼如下:

package com.erik.sparkproject

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext


/**
 * @author Erik
 */
object SortKeyTest {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
        .setAppName("SortKeyTest"
) .setMaster("local") val sc = new SparkContext(conf) val arr = Array(Tuple2(new SortKey(30, 35, 40), "1"), Tuple2(new SortKey(35, 30, 40), "2"), Tuple2(new SortKey(30, 38, 30), "3")) val rdd = sc.parallelize(arr, 1) val sortedRdd = rdd.sortByKey(false) for(tuple <- sortedRdd.collect()) { println(tuple._2) } } }

如果執行後結果為2 3 1,則說明測試通過。