val movies = sc.textFile("/user/common/ml-100k/u.item") val titles = movies.map(line => line.split("\\|").take(2)).map(array=> (array(0).toInt,array(1))).collectAsMap()
查看123对应的电影的名称
1 2 3
scala> titles(123) res30: String = Frighteners, The (1996)
val moviesForUser = ratings.keyBy(_.user).lookup(789) println(moviesForUser.size) #这个用户对33部电影做过评级 #获取评级最高的前10部电影 scala> moviesForUser.sortBy(-_.rating).take(10).map(rating => (titles(rating.product),rating.rating)).foreach(println) (Godfather, The (1972),5.0) (Trainspotting (1996),5.0) (Dead Man Walking (1995),5.0) (Star Wars (1977),5.0) (Swingers (1996),5.0) (Leaving Las Vegas (1995),5.0) (Bound (1996),5.0) (Fargo (1996),5.0) (Last Supper, The (1995),5.0) (Private Parts (1997),4.0)
scala> topKRecs.map(rating => (titles(rating.product), rating.rating)).foreach(println) (Clockwork Orange, A (1971),5.5976762921826575) (Fast, Cheap & Out of Control (1997),5.431324881530808) (GoodFellas (1990),5.4204134645044615) (What's Love Got to Do with It (1993),5.340233945688523) (Full Metal Jacket (1987),5.254370757962667) (Alien (1979),5.208694711418427) (Harold and Maude (1971),5.1758907376213825) (Nikita (La Femme Nikita) (1990),5.150276931639322) (Three Colors: Red (1994),5.123932678029936) (To Die For (1995),5.103514906503706)