python - MapReduce with paramiko how to print stdout as it streams -


i have created small python script using paramiko allows me run mapreduce jobs without using putty or cmd windows initiate jobs. works great, except don't see stdout until job completes. how can set can see each line of stdout generated, able via cmd window?

here script:

import paramiko  # define connection info host_ip = 'xx.xx.xx.xx' user = 'xxxxxxxxx' pw = 'xxxxxxxxx'  # commands list_dir = "ls /nfs_home/appers/cnielsen -l" mr = "hadoop jar /opt/cloudera/parcels/cdh/lib/hadoop-0.20-mapreduce/contrib/streaming/hadoop-streaming.jar -files /nfs_home/appers/cnielsen/product_lookups.xml -file /nfs_home/appers/cnielsen/mapper.py -file /nfs_home/appers/cnielsen/reducer.py -mapper '/usr/lib/python_2.7.3/bin/python mapper.py test1' -file /nfs_home/appers/cnielsen/process.py -reducer '/usr/lib/python_2.7.3/bin/python reducer.py' -input /nfs_home/appers/extracts/*/*.xml -output /user/loc/output/cnielsen/test51" getmerge = "hadoop fs -getmerge /user/loc/output/cnielsen/test51 /nfs_home/appers/cnielsen/test_010716_0.txt"  client = paramiko.sshclient() client.set_missing_host_key_policy(paramiko.autoaddpolicy()) client.connect(host_ip, username=user, password=pw) ##stdin, stdout, stderr = client.exec_command(list_dir) ##stdin, stdout, stderr = client.exec_command(getmerge) stdin, stdout, stderr = client.exec_command(mr)  print "executing command..."  line in stdout:     print '... ' + line.strip('\n') l in stderr:     print '... ' + l.strip('\n') client.close() 

this code implicitly calling stdout.read() blocks until eof. you'll therefore have read stdout/stderr in chunks instantly output. this answer , modified version of this answer should resolving issue. i'd recommend adapt answer 2 use-case prevent common stalling scenarios.

here's adapted example taken answer 1

sin,sout,serr = ssh.exec_command("while true; uptime; done")  def line_buffered(f):     line_buf = ""     while not f.channel.exit_status_ready():         line_buf += f.read(1)         if line_buf.endswith('\n'):             yield line_buf             line_buf = ''  l in line_buffered(sout):   # or serr     print l 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -