Combine two rows in spark based on a condition in pyspark

Viewed 10579

I have input record in following format: Input data format

enter image description here

I want the data to be transofmmed in the following format: Output data format

enter image description here

I want to combine my 2 rows based on the condition type.

As per my knowledge I need to take the composite key of the 3 data fields and compare the type fields once they are equal.

Can someone please help me with the implementation in Spark using Python?

EDIT: Following is my try using RDD in pyspark

record = spark.read.csv("wasb:///records.csv",header=True).rdd
print("Total records: %d")%record.count()
private_ip = record.map(lambda fields: fields[2]).distinct().count()
private_port = record.map(lambda fields: fields[3]).distinct().count()
destination_ip = record.map(lambda fields: fields[6]).distinct().count()
destination_port = record.map(lambda fields: fields[7]).distinct().count()
print("private_ip:%d, private_port:%d, destination ip:%d, destination_port:%d")%(private_ip,private_port,destination_ip,destination_port)
types = record.map(lambda fields: ((fields[2],fields[3],fields[6],fields[7]),fields[0])).reduceByKey(lambda a,b:a+','+b)
print types.first()

And following is my output till now.

((u'100.79.195.101', u'54835', u'58.96.162.33', u'80'), u'22-02-2016 13:11:03,22-02-2016 13:13:53')
2 Answers
Related