Apache-spark – using parallelize to create a key/value pair RDD

apache-sparkpyspark

The spark API docs provide the following definition for creating an RDD using parallelize:

parallelize(c, numSlices=None)

Distribute a local Python collection to form an RDD. Using xrange is
recommended if the input represents a range for performance.

>>> sc.parallelize([0, 2, 3, 4, 6], 5).glom().collect()
[[0], [2], [3], [4], [6]]
>>> sc.parallelize(xrange(0, 6, 2), 5).glom().collect()
[[], [0], [], [2], [4]]

I would like to create a key/value pair RDD, how can I do this with parallelize? Example output RDD:

key    |  value
-------+-------
panda  |  0
pink   |  3
pirate |  3
panda  |  1
pink   |  4

Best Answer

sc.parallelize([("panda", 0), ("pink", 3)])
Related Topic