def collect_output(mr_out_dir, out_file=None):
if out_file is None:<EOL><INDENT>output = []<EOL>for fn in iter_mr_out_files(mr_out_dir):<EOL><INDENT>with hdfs.open(fn, "<STR_LIT>") as f:<EOL><INDENT>output.append(f.read())<EOL><DEDENT><DEDENT>return "<STR_LIT>".join(output)<EOL><DEDENT>else:<EOL><INDENT>block_size = <NUM_LIT><EOL>with open(out_file, '<STR_LIT:a>') as o:<EOL><INDENT>for fn in iter_mr_out_files(mr_out_dir):<EOL><INDENT>with hdfs.open(fn) as f:<EOL><INDENT>data = f.read(block_size)<EOL>while len(data) > <NUM_LIT:0>:<EOL><INDENT>o.write(data)<EOL>data = f.read(block_size)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>
Return all mapreduce output in ``mr_out_dir``.

Append the output to ``out_file`` if provided.  Otherwise, return
the result as a single string (it is the caller's responsibility to
ensure that the amount of data retrieved fits into memory).