最后,我在下面使用。
诀窍是使用“native python map”和“spark spreaming transform”。
可能不是一种优雅的方式,但它有效:)。
def predictScore(texts, modelRF):
predictions = texts.map( lambda txt : (txt , getFeatures(txt)) ).\
map(lambda (txt, features) : (txt ,(features.split(','))) ).\
map( lambda (txt, features) : (txt, ([float(i) for i in features])) ).\
transform( lambda rdd: sc.parallelize(\
map( lambda x,y:(x,y), modelRF.predict(rdd.map(lambda (x,y):y)).collect(),rdd.map(lambda (x,y):x).collect() )\
)\
)
# in the transform operation: x=text and y=features
# Return will be tuple of (score,'original text')
return predictions
希望,它会帮助面临同样问题的人。
如果有人有更好的想法,请在此处发布。
-奥贝德
注意:我还在 spark 用户列表上提交了问题,并在那里发布了我的答案。