为电影分配整数 ID
现在也对电影做同样的处理。然后将新的用户 ID 和电影 ID 连接成一个 dataframe。
本练习是课程的一部分
使用 PySpark 构建推荐引擎
练习说明
- 使用
.select()和.distinct()方法,从ratingsdataframe 中提取所有不重复的Movie。 - 使用
coalesce()将moviesdataframe 合并为 1 个分区。 - 补全给出的部分代码,为每部电影分配唯一的整数 ID。将新列命名为
movieId,并对结果 dataframe 调用.persist()方法。 - 先将
ratingsdataframe 与usersdataframe 连接,再与moviesdataframe 连接。将结果命名为movie_ratings。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Extract the distinct movie id's
movies = ratings.select("____").distinct()
# Repartition the data to have only one partition.
movies = movies.coalesce(____)
# Create a new column of movieId integers.
movies = movies.withColumn("____", monotonically_increasing_id()).____()
# Join the ratings, users and movies dataframes
movie_ratings = ratings.join(____, "User", "left").join(____, "Movie", "left")
movie_ratings.show()