Exercise 6, What should be the right return type?


#1

Exercise 06: Get top n traded stocks by volume each month from NYSE data with in a given year

What should be the right return type of given function.?. I got given below error.
Error:
:14: error: type mismatch;
found : Unit
required: (String, Iterable[(String, Long)])
}

def topNStocksByVolumeMonth(rec: (String, Iterable[(String, Long)]), topN: Int): (String, Iterable[(String, Long)]) =
{
val list = rec._2.toList
val topNVolume = list.map(r => r._2.toLong).sortBy(k => -k.toLong).distinct.take(topN)
val sortedList = list.sortBy(k => k._2.toLong).filter(r => topNVolume.contains(r._2.toLong)).
map(r => (rec._1, r))
//rec._1 = String , r = List((“Stock stiker”, volume))

}

If, I am returning wrong then how to return (String, Iterable[(String, Long)] this also.


#3

please find the sample below,

val date_group = sum_total.groupByKey()

def getTopNcustomer(a: (Int, Iterable[(Int, Float)]), topn: Int) = {
val x = a._2.toList.sortBy(o => -o._2)
val high_total = x.map(t => t._2).distinct.take(topn).min
val final_customer = x.takeWhile(q => q._2.toFloat >=high_total)
final_customer
}

val filter_cus = date_group.flatMap(e => getTopNcustomer(e, 5).map(a => (e._1,a)))


#4

Thnks… I got the idea.