本文整理汇总了Scala中org.apache.spark.util.StatCounter类的典型用法代码示例。如果您正苦于以下问题:Scala StatCounter类的具体用法?Scala StatCounter怎么用?Scala StatCounter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StatCounter类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: NAStatCounter
//设置package包名称以及导入依赖的类
package cn.hjmao.aaspark.ch02
import org.apache.spark.util.StatCounter
class NAStatCounter extends Serializable {
val stats: StatCounter = new StatCounter()
var missing: Long = 0
def add(x: Double): NAStatCounter = {
if (java.lang.Double.isNaN(x)) {
missing += 1
} else {
stats.merge(x)
}
this
}
def merge(other: NAStatCounter): NAStatCounter = {
missing += other.missing
stats.merge(other.stats)
this
}
override def toString: String = {
"stats: " + stats.toString + " NaN: " + missing
}
}
object NAStatCounter extends Serializable {
def apply(x: Double): NAStatCounter = new NAStatCounter().add(x)
}
示例2: NAStatCounter
//设置package包名称以及导入依赖的类
class NAStatCounter extends Serializable {
import org.apache.spark.util.StatCounter
val stats: StatCounter = new StatCounter()
var missing: Long =0
def add(x: Double): NAStatCounter = {
if ( java.lang.Double.isNaN(x) ) {
missing += 1
} else {
stats.merge(x)
}
this
}
def merge(other: NAStatCounter): NAStatCounter = {
stats.merge(other.stats)
missing += other.missing
this
}
override def toString = {
"stats:" + stats.toString() + " NaN:" + missing
}
}
object NAStatCounter extends Serializable {
def apply(x: Double) = new NAStatCounter().add(x)
}
val arr = Array[Double](1.0, Double.NaN, 17.29)
val nas = arr.map( d => NAStatCounter(d) )
import org.apache.spark.rdd.RDD
def statsWithMissing(rdd: RDD[Array[Double]]): Array[NAStatCounter] = {
val stats = rdd.mapPartitions((iter: Iterator[Array[Double]]) => {
val nas: Array[NAStatCounter] = iter.next().map(d => NAStatCounter(d))
iter.foreach( arr => {
nas.zip(arr).foreach( { case (n,d) => n.add(d) } )
})
Iterator(nas)
})
stats.reduce( (n1,n2) => {
n1.zip(n2).map ({case (a,b) => a.merge(b)})
})
}
val statsm = statsWithMissing(parsed.filter(_.matched).map(_.scores))
val statsn = statsWithMissing(parsed.filter(!_.matched).map(_.scores))