Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark RDD - Mapping with extra arguments

Is it possible to pass extra arguments to the mapping function in pySpark? Specifically, I have the following code recipe:

raw_data_rdd = sc.textFile("data.json", use_unicode=True) json_data_rdd = raw_data_rdd.map(lambda line: json.loads(line)) mapped_rdd = json_data_rdd.flatMap(processDataLine) 

The function processDataLine takes extra arguments in addition to the JSON object, as:

def processDataLine(dataline, arg1, arg2) 

How can I pass the extra arguments arg1 and arg2 to the flaMap function?

like image 533
Stan Avatar asked Oct 08 '15 14:10

Stan


People also ask

How many RDDs can Cogroup () can work at once?

cogroup() can be used for much more than just implementing joins. We can also use it to implement intersect by key. Additionally, cogroup() can work on three or more RDDs at once.

How flatMap is different from map in RDD?

map and flatMap are similar, in the sense they take a line from the input RDD and apply a function on it. The way they differ is that the function in map returns only one element, while function in flatMap can return a list of elements (0 or more) as an iterator. Also, the output of the flatMap is flattened.

Can we add data to RDD?

RDDs are immutable. They are not a read/write data structure. You would recreate an RDD from HBase to get new values.


1 Answers

  1. You can use an anonymous function either directly in a flatMap

    json_data_rdd.flatMap(lambda j: processDataLine(j, arg1, arg2)) 

    or to curry processDataLine

    f = lambda j: processDataLine(dataline, arg1, arg2) json_data_rdd.flatMap(f) 
  2. You can generate processDataLine like this:

    def processDataLine(arg1, arg2):     def _processDataLine(dataline):         return ... # Do something with dataline, arg1, arg2     return _processDataLine  json_data_rdd.flatMap(processDataLine(arg1, arg2)) 
  3. toolz library provides useful curry decorator:

    from toolz.functoolz import curry  @curry def processDataLine(arg1, arg2, dataline):      return ... # Do something with dataline, arg1, arg2  json_data_rdd.flatMap(processDataLine(arg1, arg2)) 

    Note that I've pushed dataline argument to the last position. It is not required but this way we don't have to use keyword args.

  4. Finally there is functools.partial already mentioned by Avihoo Mamka in the comments.

like image 103
zero323 Avatar answered Oct 03 '22 23:10

zero323