Spark Best方式groupByKey,orderBy和filter

时间:2018-02-22 17:38:49

标签: scala apache-spark filter group-by sql-order-by

我有50GB的数据与这个架构[ID,时间戳,countryId],我想得到每个"更改"使用spark 2.2.1按时间戳排序的所有事件中的每个人我的意思是如果我有这个事件:

1,20180101,2
1,20180102,3
1,20180105,3
2,20180105,3
1,20180108,4
1,20180109,3
2,20180108,3
2,20180109,6

我想获得这个:

1,20180101,2
1,20180102,3
1,20180108,4
1,20180109,3
2,20180105,3
2,20180109,6

为此我开发了这段代码:

val eventsOrdened = eventsDataFrame.orderBy("ID", "timestamp")

val grouped = eventsOrdened
  .rdd.map(x => (x.getString(0), x))
  .groupByKey(300)
  .mapValues(y => cleanEvents(y))
  .flatMap(_._2)

其中" cleanEvents"是:

def cleanEvents(ordenedEvents: Iterable[Row]): Iterable[Row] = {

val ordered = ordenedEvents.toList

val cleanedList: ListBuffer[Row] = ListBuffer.empty[Row]

ordered.map {
  x => {

    val next = if (ordered.indexOf(x) != ordered.length - 1) ordered(ordered.indexOf(x) + 1) else x
    val country = x.get(2)
    val nextountry = next.get(2)
    val isFirst = if (cleanedList.isEmpty) true else false
    val isLast = if (ordered.indexOf(x) == ordered.length - 1) true else false

    if (isFirst) {
      cleanedList.append(x)
    } else {
      if (cleanedList.size >= 1 && cleanedList.last.get(2) != country && country != nextCountry) {
        cleanedList.append(x)
      } else {
        if (isLast && cleanedList.last.get(2) != zipCode) cleanedList.append(x)
      }
    }

  }
}
cleanedList
}

它有效,但速度太慢,欢迎任何优化!!

谢谢!

2 个答案:

答案 0 :(得分:1)

您可能需要尝试以下操作:

  1. 二级排序。它是低级分区和排序,您将创建自定义分区。更多信息:http://codingjunkie.net/spark-secondary-sort/

  2. 使用combineByKey

    case class Details(id: Int, date: Int, cc: Int)
    val sc = new SparkContext("local[*]", "App")
    val list = List[Details](
        Details(1,20180101,2),
        Details(1,20180102,3),
        Details(1,20180105,3),
        Details(2,20180105,3),
        Details(1,20180108,4),
        Details(1,20180109,3),
        Details(2,20180108,3),
        Details(2,20180109,6))
    
    val rdd = sc.parallelize(list)
    val createCombiner = (v: (Int, Int)) => List[(Int, Int)](v)
    val combiner = (c: List[(Int, Int)], v: (Int, Int)) => (c :+ v).sortBy(_._1)
    val mergeCombiner = (c1: List[(Int, Int)], c2: List[(Int, Int)]) => (c1 ++ c2).sortBy(_._1)
    
    rdd
       .map(det => (det.id, (det.date, det.cc)))
       .combineByKey(createCombiner, combiner, mergeCombiner)
       .collect()
       .foreach(println)
    
  3. 输出将是这样的:

    (1,List((20180101,2), (20180102,3), (20180105,3), (20180108,4), (20180109,3)))
    (2,List((20180105,3), (20180108,3), (20180109,6)))
    

答案 1 :(得分:1)

可以使用窗口函数“lag”:

  case class Details(id: Int, date: Int, cc: Int)
  val list = List[Details](
  Details(1, 20180101, 2),
  Details(1, 20180102, 3),
  Details(1, 20180105, 3),
  Details(2, 20180105, 3),
  Details(1, 20180108, 4),
  Details(1, 20180109, 3),
  Details(2, 20180108, 3),
  Details(2, 20180109, 6))
val ds = list.toDS()
// action 
val window = Window.partitionBy("id").orderBy("date")
val result = ds.withColumn("lag", lag($"cc", 1).over(window)).where(isnull($"lag") || $"lag" =!= $"cc").orderBy("id", "date")
result.show(false)

结果是(滞后列可以删除):

|id |date    |cc |lag |
+---+--------+---+----+
|1  |20180101|2  |null|
|1  |20180102|3  |2   |
|1  |20180108|4  |3   |
|1  |20180109|3  |4   |
|2  |20180105|3  |null|
|2  |20180109|6  |3   |
+---+--------+---+----+