I have 50GB of data with this schema [ID, timestamp, countryId] and I would like to get each "change" of each person in all of their events ordered by timestamp using spark 2.2.1. I mean if I have this events:
1,20180101,2
1,20180102,3
1,20180105,3
2,20180105,3
1,20180108,4
1,20180109,3
2,20180108,3
2,20180109,6
I would like to obtain this:
1,20180101,2
1,20180102,3
1,20180108,4
1,20180109,3
2,20180105,3
2,20180109,6
For this I have developed this code:
val eventsOrdened = eventsDataFrame.orderBy("ID", "timestamp")
val grouped = eventsOrdened
.rdd.map(x => (x.getString(0), x))
.groupByKey(300)
.mapValues(y => cleanEvents(y))
.flatMap(_._2)
where "cleanEvents" is:
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
}
It works but it's too slow, any optimization are welcome!!
Thanks!
Window function "lag" can be used:
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)
Result is (lag column can be removed):
|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 |
+---+--------+---+----+
You might want to try the following:
Secondary sorting. It's does low-level partitioning and sorting and you will create a customize partition. More info here: http://codingjunkie.net/spark-secondary-sort/
Use 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)
the output would be something like this:
(1,List((20180101,2), (20180102,3), (20180105,3), (20180108,4), (20180109,3)))
(2,List((20180105,3), (20180108,3), (20180109,6)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.