PySpark将两个RDD结果连接到一个空的RDD中

时间:2016-08-26 09:10:41

标签: apache-spark pyspark rdd apache-spark-mllib

我是一个Spark新手试图在我的数据集上编辑和应用这个电影推荐教程(https://databricks-training.s3.amazonaws.com/movie-recommendation-with-mllib.html)。但是它一直在抛出这个错误:

ValueError: Can not reduce() empty RDD

这是计算模型的均方根误差的函数:

def computeRmse(model, data, n):
    """
    Compute RMSE (Root Mean Squared Error).
    """
    predictions = model.predictAll(data.map(lambda x: (x[0], x[1])))

    print predictions.count()
    print predictions.first()
    print "predictions above"

    print data.count()
    print data.first()
    print "validation data above"


    predictionsAndRatings = predictions.map(lambda x: ((x[0], x[1]), x[2])) \
#LINE56
   .join(data.map(lambda line: line.split(‘,’) ).map(lambda x: ((x[0], x[1]), x[2]))) \
  .values()    

   print predictionsAndRatings.count()
   print "predictions And Ratings above" 
#LINE63
return sqrt(predictionsAndRatings.map(lambda x: (x[0] - x[1]) **    2).reduce(add) / float(n))

model = ALS.train(training,rank,numIter,lambda)。 data是验证数据集。 最初来自ratings.txt文件的培训和验证集,格式为:userID,productID,rating,ratingopID

这些是输出的一部分:

879
...
Rating(user=0, product=656, rating=4.122132631144641)
predictions above
...
1164
...
(u'640085', u'1590', u'5')
validation data above    
...
16/08/26 12:47:18 INFO DAGScheduler: Registering RDD 259 (join at     /path/myapp/MyappALS.py:56)
16/08/26 12:47:18 INFO DAGScheduler: Got job 20 (count at         /path/myapp/MyappALS.py:59) with 12 output partitions 
16/08/26 12:47:18 INFO DAGScheduler: Final stage: ResultStage 238 (count at /path/myapp/MyappALS.py:59)
16/08/26 12:47:18 INFO DAGScheduler: Parents of final stage:     List(ShuffleMapStage 237)
16/08/26 12:47:18 INFO DAGScheduler: Missing parents: List(ShuffleMapStage 237)
16/08/26 12:47:18 INFO DAGScheduler: Submitting ShuffleMapStage 237     (PairwiseRDD[259] at join at /path/myapp/MyappALS.py:56), which has no     missing parents    
....

0
predictions And Ratings above

...
Traceback (most recent call last):
File "/path/myapp/MyappALS.py", line 130, in <module>
validationRmse = computeRmse(model, validation, numValidation)
File "/path/myapp/MyappALS.py", line 63, in computeRmse
return sqrt(predictionsAndRatings.map(lambda x: (x[0] - x[1]) ** 2).reduce(add) / float(n))
File "/spark/python/lib/pyspark.zip/pyspark/rdd.py", line 805, in reduce
ValueError: Can not reduce() empty RDD

因此,从count()开始,我确定初始RDD不为空。 比INFO log Registering RDD 259 (join at /path/myapp/MyappALS.py:56)更意味着是否启动了联接作业?

我有什么不对劲吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

当我将int()添加到:

时,该错误消失了

predictionsAndRatings = predictions.map(lambda x: ((x[0], x[1]), x[2])) \ .join(data.map(lambda x: ((int(x[0]), int(x[1])), int(x[2])))) \ .values()

我们认为它是因为pedic从提供tupple的方法predictAll输出,但是算法手动解析的其他数据