I am collecting data using the Spark RDD API and have created a paired RDD, as shown below:
spark = SparkSession.builder.master('local').appName('app').getOrCreate()
sc = spark.sparkContext
raw_rdd = sc.textFile("data.csv")
paired_rdd = raw_rdd\
.map(lambda x: x.split(","))\
.map(lambda x: (x[2], [x[1], x[3],x[5]]))
Here is a sample excerpt of the paired RDD:
[('VXIO456XLBB630221', ['I', 'Nissan', '2003']),
('VXIO456XLBB630221', ['A', '', '']),
('VXIO456XLBB630221', ['R', '', '']),
('VXIO456XLBB630221', ['R', '', ''])]
As you notice, the keys in this paired RDD are the same for all elements, but only one element has all the fields completed.
What do we want to accomplish? We want to replace the empty fields with the values of the element with complete fields. So we would have an expected output like this:
[('VXIO456XLBB630221', ['I', 'Nissan', '2003']),
('VXIO456XLBB630221', ['A', 'Nissan', '2003']),
('VXIO456XLBB630221', ['R', 'Nissan', '2003']),
('VXIO456XLBB630221', ['R', 'Nissan', '2003'])]
I know the first step would be to do a groupByKey, i.e.,
paired_rdd.groupByKey().map(lambda kv: ____)
I am just not sure how to iterate through the values and how this would fit into one lambda function.