flatMap (word, (song, another_song)) 到 rdd
我有一个具有以下结构的 rdd:
[('song1', 'artist1', ['word1', 'word2', 'word3', 'word1']),
('song2', 'artist2', ['word1', 'word3', 'word1'])]
我想要映射与不同歌曲一起出现的每个单词,例如:
('word1', ('song1', 'song2')), ('word2', ('song1')), ('word3', ('song1', 'song2'))
我尝试使用 flatMap 和 reduceByKey 执行以下操作:
rdd_songs = rdd.map(lambda x: [(word, x[0]) for word in x[2]]).flatMap(lambda x: x).reduceByKey(lambda a, b: a+b)
输出是我想要的,但不完全这样做:
[('word1', 'song1song1song2'), ('word2', 'song1'), ('word3', 'song1song2')
我是告诉 flatMapValues 函数会很有用,但似乎也无法理解该方法,很好,任何帮助都会非常有帮助
I have a rdd with this structure:
[('song1', 'artist1', ['word1', 'word2', 'word3', 'word1']),
('song2', 'artist2', ['word1', 'word3', 'word1'])]
Where I want map every word that appears with the different songs, for example:
('word1', ('song1', 'song2')), ('word2', ('song1')), ('word3', ('song1', 'song2'))
What I have tried is the following using flatMap and reduceByKey:
rdd_songs = rdd.map(lambda x: [(word, x[0]) for word in x[2]]).flatMap(lambda x: x).reduceByKey(lambda a, b: a+b)
The output is what I want but not exactly doing this:
[('word1', 'song1song1song2'), ('word2', 'song1'), ('word3', 'song1song2')
I was told to that the flatMapValues function would be useful but also can't seem to understand the method, very well, any help would be very helpful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论