#-*-coding:utf-8
from pyspark import SparkContext
from pyspark.mllib.recommendation import ALS,Rating
#数据格式 用户id,影片id,星级,时间戳
sc=SparkContext("local[2]","second spark app")
#读取数据
rawData=sc.textFile("/home/lxs/Downloads/ml-100k/u.data")
print rawData.first()
#分离数据
rawRating=rawData.map(lambda line:line.split("\t")[:3])
print rawRating.first()
ratings=rawRating.map(lambda x:Rating(int(x[0]),int(x[1]),float(x[2])))
print ratings.first()
#模型训练
model=ALS.train(ratings,50,10,0.01)
print model.userFeatures().count()
print model.productFeatures().count()
#商品预测
predictedRating=model.predict(789,123)
print predictedRating
#推荐10个商品
userId=789
K=10
topKRecs=model.recommendProducts(userId,K)
print topKRecs