Created
April 29, 2014 08:54
-
-
Save nrchandan/11394440 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyspark | |
class Point(object): | |
'''this class being used as container''' | |
pass | |
def to_point_obj(point_as_dict): | |
'''convert a dict representation of a point to Point object''' | |
p = Point() | |
p.x = point_as_dict['x'] | |
p.y = point_as_dict['y'] | |
return p | |
def add_two_points(point_obj1, point_obj2): | |
print type(point_obj1), type(point_obj2) | |
point_obj1.x += point_obj2.x | |
point_obj1.y += point_obj2.y | |
return point_obj1 | |
def zero_point(): | |
p = Point() | |
p.x = p.y = 0 | |
return p | |
sc = pyspark.SparkContext('local', 'test_app') | |
a = sc.parallelize([{'x':1, 'y':1}, {'x':2, 'y':2}, {'x':3, 'y':3}]) | |
b = a.map(to_point_obj) # convert to an RDD of Point objects | |
c = b.fold(zero_point(), add_two_points) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above code fails with python PicklingError: