Skip to content

Commit 34fcdc3

Browse files
add optional argument 'mode' for rdd.pipe
permissive - do not check returncode strict - only allow returncode 0 grep - allow returncode 0 or 1
1 parent a0c0161 commit 34fcdc3

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

python/pyspark/rdd.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,25 @@ def groupBy(self, f, numPartitions=None):
687687
return self.map(lambda x: (f(x), x)).groupByKey(numPartitions)
688688

689689
@ignore_unicode_prefix
690-
def pipe(self, command, env={}):
690+
def pipe(self, command, env={}, mode='permissive'):
691691
"""
692692
Return an RDD created by piping elements to a forked external process.
693693
694694
>>> sc.parallelize(['1', '2', '', '3']).pipe('cat').collect()
695695
[u'1', u'2', u'', u'3']
696696
"""
697+
if mode == 'permissive':
698+
def fail_condition(x):
699+
return False
700+
elif mode == 'strict':
701+
def fail_condition(x):
702+
return x == 0
703+
elif mode == 'grep':
704+
def fail_condition(x):
705+
return x == 0 or x == 1
706+
else:
707+
raise ValueError("mode must be one of 'permissive', 'strict' or 'grep'.")
708+
697709
def func(iterator):
698710
pipe = Popen(
699711
shlex.split(command), env=env, stdin=PIPE, stdout=PIPE)
@@ -707,7 +719,7 @@ def pipe_objs(out):
707719

708720
def check_return_code():
709721
pipe.wait()
710-
if pipe.returncode:
722+
if fail_condition(pipe.returncode):
711723
raise Exception("Pipe function `%s' exited "
712724
"with error code %d" % (command, pipe.returncode))
713725
else:

0 commit comments

Comments
 (0)