如何创建键值对? [英] How can I create key-value pairs?
本文介绍了如何创建键值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用 groupByKey
在我的键 - 值对。现在我有这个(键,可迭代)。
我想从它使这些键 - 值对:((键,(列表中的一个元素)),1)对所有表V
我怎样才能做到这一点?迭代名单上怎么可以循环?
VAL线= sc.textFile(followers.txt)图(S = GT; {
VAL子= s.split()
(子(0),子(1))
})
VAL aggrigateNeighbors = lines.groupByKey()。收集()的foreach(的println)
VAL朋友= aggrigateNeighbors.flatMap {X =>((K,v.hasNext()),1)}
解决方案
您快到了。
您需要映射了迭代器,并从每个所需条目
未经检验的,对不起,我不是在电脑前的,我可以测试它。
VAL线= sc.textFile(followers.txt)图(S = GT; {
VAL子= s.split()
(子(0),子(1))
})
VAL aggregateNeighbors = lines.groupByKey()
VAL朋友= {aggregateNeighbors.flatMap案(K,V)=> v.map {S =>((K,S),1)}}
I have used groupByKey
on my key-value pairs. Now I have this (key, Iterable).
I want to make these key-value pairs from it: ((key,(one element of list)),1) for all list v.
How can I do this? How can loop on iterable list?
val lines = sc.textFile("followers.txt").map(s => {
val substrings = s.split(" ")
(substrings(0), substrings(1))
})
val aggrigateNeighbors = lines.groupByKey().collect().foreach(println)
val friends = aggrigateNeighbors.flatMap{x=>((k,v.hasNext()),1)}
解决方案
You're nearly there.
You need to map over the iterator and make your desired entry from each
Untested, sorry, I'm not in front of the computer on which I could test it.
val lines = sc.textFile("followers.txt").map(s => {
val substrings = s.split(" ")
(substrings(0), substrings(1))
})
val aggregateNeighbors = lines.groupByKey()
val friends = aggregateNeighbors.flatMap{case (k,v) => v.map{s=>((k,s), 1)}}
这篇关于如何创建键值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文