1

I have two lines as below and they produced the attached output. The output is correct. But in the case of the second statement, I want to form a single tuple and not a tuple of tuples. How could i achieve the same?

print ratingsRDD.map(lambda x: (x[0],x[2])).take(5)
print ratingsRDD.map(lambda x: (x[0],x[2])).reduceByKey(lambda p,q: (p,q)).take(4)


[(1, 5.0), (1, 3.0), (1, 5.0), (1, 5.0), (1, 4.0)]
[(2, ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((4.0, 3.0), 4.0), 3.0), 3.0), 4.0), 3.0), 5.0), 4.0), 4.0), 3.0), 3.0), 3.0), 3.0), 4.0), 4.0), 5.0), 5.0), 4.0), 3.0), 3.0), 4.0), 4.0), 3.0), 5.0), 4.0), 5.0), 5.0), 3.0), 3.0), 2.0), 4.0), 3.0), 5.0), 3.0), 4.0), 5.0), 3.0), 4.0), 3.0), 3.0), 3.0), 3.0), 3.0), 5.0), 3.0), 5.0), 1.0), 3.0), 5.0), 4.0), 4.0), 3.0), 4.0), 4.0), 3.0), 3.0), 5.0), 4.0), 5.0), 2.0), 3.0), 2.0), 1.0), ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((5.0, 4.0), 4.0), 5.0), 3.0), 5.0), 3.0), 2.0), 3.0), 3.0), 2.0), 2.0), 4.0), 5.0), 5.0), 2.0), 5.0), 4.0), 4.0), 2.0), 5.0), 3.0), 5.0), 3.0), 3.0), 4.0), 4.0), 4.0), 3.0), 4.0), 3.0), 5.0), 5.0), 5.0), 3.0), 4.0), 2.0), 5.0), 4.0), 5.0), 2.0), 4.0), 4.0), 4.0), 3.0), 4.0), 5.0), 4.0), 3.0), 2.0), 5.0), 4.0), 5.0), 4.0), 3.0), 4.0), 3.0), 5.0), 5.0), 3.0), 4.0), 3.0), 5.0), 5.0), 5.0))), (4, ((((((((((3.0, 4.0), 5.0), 4.0), 5.0), 4.0), 4.0), 5.0), 5.0), 5.0), ((((((((((5.0, 4.0), 4.0), 5.0), 2.0), 5.0), 4.0), 4.0), 1.0), 5.0), 5.0))), (6, ((((((((((((((((((((((((((((((((((((5.0, 4.0), 5.0), 4.0), 5.0), 4.0), 5.0), 4.0), 4.0), 4.0), 3.0), 4.0), 4.0), 3.0), 4.0), 4.0), 3.0), 4.0), 5.0), 5.0), 3.0), 3.0), 4.0), 5.0), 4.0), 4.0), 5.0), 5.0), 5.0), 4.0), 4.0), 3.0), 5.0), 4.0), 4.0), 4.0), ((((((((((((((((((((((((((((((((((4.0, 4.0), 5.0), 4.0), 4.0), 2.0), 5.0), 3.0), 4.0), 1.0), 3.0), 5.0), 3.0), 4.0), 4.0), 3.0), 5.0), 4.0), 3.0), 3.0), 3.0), 3.0), 3.0), 5.0), 4.0), 4.0), 4.0), 5.0), 3.0), 3.0), 3.0), 3.0), 4.0), 4.0), 4.0))), (8, ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((3.0, 5.0), 3.0), 5.0), 5.0), 3.0), 3.0), 3.0), 5.0), 3.0), 3.0), 5.0), 3.0), 4.0), 3.0), 3.0), 3.0), 3.0), 3.0), 5.0), 2.0), 5.0), 3.0), 4.0), 5.0), 5.0), 4.0), 4.0), 5.0), 3.0), 5.0), 3.0), 3.0), 5.0), 3.0), 4.0), 4.0), 2.0), 4.0), 5.0), 4.0), 3.0), 5.0), 5.0), 3.0), 4.0), 5.0), 4.0), 3.0), 5.0), 4.0), 4.0), 3.0), 5.0), 4.0), 3.0), 3.0), 4.0), 4.0), 4.0), 3.0), 4.0), 3.0), 5.0), 2.0), 3.0), 3.0), 5.0), 5.0), 4.0), ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((3.0, 3.0), 4.0), 4.0), 3.0), 3.0), 5.0), 5.0), 3.0), 3.0), 2.0), 4.0), 4.0), 4.0), 3.0), 5.0), 5.0), 4.0), 5.0), 3.0), 4.0), 5.0), 4.0), 3.0), 5.0), 3.0), 3.0), 5.0), 5.0), 5.0), 4.0), 3.0), 5.0), 3.0), 2.0), 4.0), 4.0), 4.0), 4.0), 5.0), 5.0), 3.0), 3.0), 5.0), 5.0), 5.0), 4.0), 4.0), 4.0), 5.0), 4.0), 4.0), 2.0), 4.0), 3.0), 4.0), 5.0), 5.0), 5.0), 3.0), 2.0), 5.0), 4.0), 5.0), 3.0), 5.0), 5.0), 4.0), 3.0)))]
Community
  • 1
  • 1
user2543622
  • 5,760
  • 25
  • 91
  • 159

1 Answers1

4

Just use groupByKey. There is no good reason to use reduceByKey here:

grouped = sc.parallelize(
    [(1, 5.0), (1, 3.0), (1, 5.0), (1, 5.0), (1, 4.0)]
).groupByKey()

grouped.mapValues(list).first()
## (1, [5.0, 3.0, 5.0, 5.0, 4.0])

Just for the record, what you have is a tuple of tuples not a list of lists.

If you really want a solution which doesn't require groupByKey see How can I use reduceByKey instead of GroupByKey to construct a list?.

Community
  • 1
  • 1
zero323
  • 322,348
  • 103
  • 959
  • 935
  • can we stick to reducebykey? i read online that groupbykey is not an efficient option. Thanks for informing about tuple of tuples. I will update my question – user2543622 Feb 29 '16 at 00:34
  • 1
    It's not that bad if you aren't dealing with a ton of data. I believe it calls the same underlying functions, and, since you really do want to group things, it's probably fine. – Chris Chambers Feb 29 '16 at 00:37
  • 1
    @user2543622 Let me ask you a question (evil grin here). Do you remember why `groupByKey` should be avoided (I am pretty sure [this is what you mean](https://databricks.gitbooks.io/databricks-spark-knowledge-base/content/best_practices/prefer_reducebykey_over_groupbykey.html) by _read online_)? And a follow-up question is how `reduceByKey` with `[]` or `()` can help you? (Hint: it cannot...) – zero323 Feb 29 '16 at 00:47
  • @ChrisChambers I would even go further - if you need the result of `groupByKey` you cannot do better than `groupByKey`. – zero323 Feb 29 '16 at 00:51