Homework Exercise Question -- Calculate Revenue and Min_order_item_SubTotal

spark-shell
scala

#1

Hello All,

Has anyone tried this problem, Its one of the problem that Durga sir mentioned in the video series but I was not able to find any thread for it. So thought of posting it here.

Problem Statement:–
Calculate Revenue per order and arrange the output data in below format.
(order_id, revenue, Min_order_item_SubTotal)
Here “Min_order_item_SubTotal” --> is the minimum subtotal value for any given order.

Note:- Durga sir has mentioned similar problem in below video series, video 75. Here is the link

One possible solution:–
val orderItems = sc.textFile("/public/retail_db/order_items")
val orderItemsMap = orderItems.map(oi => (oi.split(",")(1).toInt, oi.split(",")(4).toFloat))
val revenueAndMinPerOrderID = orderItemsMap.aggregateByKey((0.0f, 99999.12f))(
(inter, subtotal) => (inter._1+subtotal, if(subtotal != 0.0f) {if(subtotal > inter._2) inter._2 else subtotal} else 0.0f),
(total, inter) => (total._1 + inter._1, if(inter._2 != 0.0f) {if(total._2 > inter._2) inter._2 else total._2} else 0.0f)
)

Looking for better solution approach.