Deno George
Deno George

Reputation: 362

Python Spark How to Map Fields of one rdd to another rdd

I am very new to python spark as per above subject i want to map the fields of one Rdd to the field of another Rdd.Here is the example

rdd1:

c_id    name 
121210  abc
121211  pqr

rdd2:

c_id   cn_id cn_value
121211  0     0
121210  0     1

So the matched c_id will replace by name with cnid and the aggregated the cn_value. So the output will like this abc 0 0 pqr 0 1

from pyspark import SparkContext
import csv
sc = SparkContext("local", "spark-App")
file1 = sc.textFile('/home/hduser/sample.csv').map(lambda line:line.split(',')).filter(lambda line:len(line)>1)
file2 = sc.textFile('hdfs://localhost:9000/sample2/part-00000').map(lambda line:line.split(','))
file1_fields = file1.map(lambda x: (x[0],x[1]))
file2_fields = file2.map(lambda x: (x[0],x[1],float(x[2])))

How can i achieve my goal by putting some code here.

Any Help will highly appreciated thanks you

Upvotes: 1

Views: 2314

Answers (1)

zero323
zero323

Reputation: 330413

Operation you're looking for is called join. Given a structure of your it is probably best to use DataFrames and spark-csv (I assume that the second file is comma-separated as well, but has no header). Lets start with dummy data:

file1 = ... # path to the first file
file2 = ... # path to the second file

with open(file1, "w") as fw:
    fw.write("c_id,name\n121210,abc\n121211,pqr")

with open(file2, "w") as fw:
    fw.write("121211,0,0\n121210,0,1")

Read first file:

df1 = (sqlContext.read 
    .format('com.databricks.spark.csv')
    .options(header='true', inferSchema='true')
    .load(file1))

Load second file:

schema = StructType(
    [StructField(x, LongType(), False) for x in ("c_id", "cn_id", "cn_value")])

df2 = (sqlContext.read 
    .format('com.databricks.spark.csv')
    .schema(schema)
    .options(header='false')
    .load(file2))

Finally join:

combined = df1.join(df2, df1["c_id"] == df2["c_id"])
combined.show()

## +------+----+------+-----+--------+
## |  c_id|name|  c_id|cn_id|cn_value|
## +------+----+------+-----+--------+
## |121210| abc|121210|    0|       1|
## |121211| pqr|121211|    0|       0|
## +------+----+------+-----+--------+

Edit:

With RDDs you have you can do something like this:

file1_fields.join(file2_fields.map(lambda x: (x[0], x[1:])))

Upvotes: 2

Related Questions