When I run this code, I get empty collection error in some cases.
val result = df
.filter(col("channel_pk") === "abc")
.groupBy("member_PK")
.agg(sum(col("price") * col("quantityOrdered")) as "totalSum")
.select("totalSum")
.rdd.map(_ (0).asInstanceOf[Double]).reduce(_ + _)
The error happens at this line:
.rdd.map(_ (0).asInstanceOf[Double]).reduce(_ + _)
When collection is empty, I want result to be equal to 0. How can I do it?
if (assert(df.take(1).isEmpty)) result = 0 else result ...?