How can I create key-value pairs?

后端 未结 1 797
广开言路
广开言路 2021-01-25 07:35

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

相关标签:
1条回答
  • 2021-01-25 07:51

    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)}}
    
    0 讨论(0)
提交回复
热议问题