【发布时间】:2016-02-10 18:14:30
【问题描述】:
错误:
org.apache.spark.SparkException: RDD 转换和动作只能由驱动程序调用,不能在其他转换内部调用;例如,rdd1.map(x => rdd2.values.count() * x) 是无效的,因为值转换和计数操作不能在 rdd1.map 转换中执行。有关详细信息,请参阅 SPARK-5063。
def computeRatio(model: MatrixFactorizationModel, test_data: org.apache.spark.rdd.RDD[Rating]): Double = {
val numDistinctUsers = test_data.map(x => x.user).distinct().count()
val userRecs: RDD[(Int, Set[Int], Set[Int])] = test_data.groupBy(testUser => testUser.user).map(u => {
(u._1, u._2.map(p => p.product).toSet, model.recommendProducts(u._1, 20).map(prec => prec.product).toSet)
})
val hitsAndMiss: RDD[(Int, Double)] = userRecs.map(x => (x._1, x._2.intersect(x._3).size.toDouble))
val hits = hitsAndMiss.map(x => x._2).sum() / numDistinctUsers
return hits
}
我正在使用MatrixFactorizationModel.scala中的方法,我必须映射用户,然后调用该方法来获取每个用户的结果。通过这样做,我引入了嵌套映射,我认为这会导致问题:
我知道这个问题实际上发生在:
val userRecs: RDD[(Int, Set[Int], Set[Int])] = test_data.groupBy(testUser => testUser.user).map(u => {
(u._1, u._2.map(p => p.product).toSet, model.recommendProducts(u._1, 20).map(prec => prec.product).toSet)
})
因为在映射时我打电话给model.recommendProducts
【问题讨论】:
-
你的问题是什么?这个问题非常重要(而且它非常概念化,否则火花可能会混乱),如果我是正确的,它在 UC 提供的spark's course 中进行了讨论伯克利
标签: scala mapreduce apache-spark apache-spark-mllib