一、基于用户的推荐公式
其中,S(u,K)表示与用户u最相似的K个用户,N(i)代表喜欢物品i的用户集合,rm表示用户v对物品i的评分。
二、代码
public class UserCFRecommendApp {public static void main(String[]args){SparkConf sparkConf = new SparkConf();sparkConf.setAppName("UserCFRecommendApp");sparkConf.setMaster("local[*]");SparkSession sparkSession = SparkSession.builder().config(sparkConf).getOrCreate();String url = "jdbc:mysql://localhost:3306/spark-mysql?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false";String driver = "com.mysql.jdbc.Driver";String user = "root";String password = "admin";Dataset<Row> score = sparkSession.read().format("jdbc").option("driver", driver).option("url",url).option("dbtable","user_item").option("user",user).option("password",password).load();Dataset<Row> similar = sparkSession.read().format("jdbc").option("driver", driver).option("url",url).option("dbtable","user_similar").option("user",user).option("password",password).load();//分组 top k
// similar = similar.selectExpr("a_user_id", "b_user_id", "count",
// "ROW_NUMBER() OVER (PARTITION BY a_user_id ORDER BY count DESC) rank")
// .where("rank <= 10");//similar.show();Dataset<Row> result = similar.as("us").join(score.as("s"), functions.column("us.b_user_id").$eq$eq$eq(functions.column("s.user_id"))).selectExpr("us.a_user_id user_id", "s.item_id", "us.count * s.score score").groupBy("user_id", "item_id").sum("score").selectExpr("user_id", "item_id", "`sum(score)` score");result.show();
// result.write()
// .mode(SaveMode.Overwrite)
// .format("jdbc")
// .option("driver", driver)
// .option("url",url)
// .option("dbtable","user_item_recom")
// .option("user",user)
// .option("password",password)
// .save();sparkSession.stop();}
}