Skip to content

Instantly share code, notes, and snippets.

@granthenke
Created April 2, 2015 21:51
Show Gist options
  • Save granthenke/d5414936a285a294dd76 to your computer and use it in GitHub Desktop.
Save granthenke/d5414936a285a294dd76 to your computer and use it in GitHub Desktop.
Scala Scratch Pad
case class MeanM(sum: Double, count: Long) {
def +(that: MeanM): MeanM = {
new MeanM(this.sum + that.sum, this.count + that.count)
}
def mean = sum / count.toDouble
}
val data = List(("cust1", 1.0),("cust1", 5.0),("cust2", 6.0))
val dataRDD = sc.makeRDD(data)
dataRDD.aggregateByKey(MeanM(0,0))(_ + MeanM(_,1), _ + _).mapValues(_.mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment