2012-08-10 04:10:02 -04:00
|
|
|
"""
|
|
|
|
Worker that receives input from Piped RDD.
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
from base64 import standard_b64decode
|
|
|
|
# CloudPickler needs to be imported so that depicklers are registered using the
|
|
|
|
# copy_reg module.
|
2013-01-20 04:57:44 -05:00
|
|
|
from pyspark.accumulators import _accumulatorRegistry
|
2012-08-25 16:59:01 -04:00
|
|
|
from pyspark.broadcast import Broadcast, _broadcastRegistry
|
2012-08-19 20:12:51 -04:00
|
|
|
from pyspark.cloudpickle import CloudPickler
|
2013-01-20 04:57:44 -05:00
|
|
|
from pyspark.serializers import write_with_length, read_with_length, write_int, \
|
2012-12-24 20:20:10 -05:00
|
|
|
read_long, read_int, dump_pickle, load_pickle, read_from_pickle_file
|
2012-08-25 19:46:07 -04:00
|
|
|
|
2012-08-10 04:10:02 -04:00
|
|
|
|
|
|
|
# Redirect stdout to stderr so that users must return values from functions.
|
|
|
|
old_stdout = sys.stdout
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
|
|
|
|
|
2012-08-25 19:46:07 -04:00
|
|
|
def load_obj():
|
|
|
|
return load_pickle(standard_b64decode(sys.stdin.readline().strip()))
|
2012-08-10 04:10:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-01-08 19:04:41 -05:00
|
|
|
split_index = read_int(sys.stdin)
|
2012-10-19 13:24:49 -04:00
|
|
|
num_broadcast_variables = read_int(sys.stdin)
|
2012-08-25 16:59:01 -04:00
|
|
|
for _ in range(num_broadcast_variables):
|
2012-10-19 13:24:49 -04:00
|
|
|
bid = read_long(sys.stdin)
|
2012-08-25 19:46:07 -04:00
|
|
|
value = read_with_length(sys.stdin)
|
2012-10-19 13:24:49 -04:00
|
|
|
_broadcastRegistry[bid] = Broadcast(bid, load_pickle(value))
|
2012-08-25 19:46:07 -04:00
|
|
|
func = load_obj()
|
|
|
|
bypassSerializer = load_obj()
|
|
|
|
if bypassSerializer:
|
|
|
|
dumps = lambda x: x
|
2012-08-10 04:10:02 -04:00
|
|
|
else:
|
2012-08-25 19:46:07 -04:00
|
|
|
dumps = dump_pickle
|
2013-01-08 19:04:41 -05:00
|
|
|
iterator = read_from_pickle_file(sys.stdin)
|
|
|
|
for obj in func(split_index, iterator):
|
2012-08-25 19:46:07 -04:00
|
|
|
write_with_length(dumps(obj), old_stdout)
|
2013-01-20 04:57:44 -05:00
|
|
|
# Mark the beginning of the accumulators section of the output
|
|
|
|
write_int(-1, old_stdout)
|
|
|
|
for aid, accum in _accumulatorRegistry.items():
|
|
|
|
write_with_length(dump_pickle((aid, accum._value)), old_stdout)
|
2012-08-10 04:10:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|