복붙노트

[HADOOP] 퍼티 / SSH없이 파이썬을 통해 하둡 맵리 듀스 작업을 시작합니다

HADOOP

퍼티 / SSH없이 파이썬을 통해 하둡 맵리 듀스 작업을 시작합니다

나는, 이름 / IP 주소를 호스팅하는 SSH 명령 줄 창을 얻기 위해 퍼티로 이름과 암호를 로그인 입력해야 퍼티를 통해 SSH에 로그인하여 하둡 맵리 듀스 작업을 실행하고있다. 일단 SSH 콘솔 창에, 나는 다음과 같은 적절한 MR 명령을 제공합니다 :

하둡 항아리 /usr/lib/hadoop-0.20-mapreduce/contrib/streaming/hadoop-streaming-2.0.0-mr1-cdh4.0.1.jar - 파일 /nfs_home/appers/user1/mapper.py - 파일 / nfs_home / appers /user1/reducer.py -mapper '/usr/lib/python_2.7.3/bin/python mapper.py'-reducer '/usr/lib/python_2.7.3/bin/python reducer.py'-input / ccexp / 데이터 /test_xml/0901282-510179094535002-oozie-oozi-W/extractOut//.xml - 출력 / 사용자 / ccexptest / 출력 / USER1 / MRoutput

내가 뭘하고 싶은 내가 파이썬 스크립트 내에서 맵리 듀스 작업을 시작하고 퍼티를 통해 SSH에 로그인하는 것을 방지 할 수 있도록이 투박한 과정을 변경하는 파이썬을 사용합니다.

이 수행 할 수 있습니다 그렇다면, 사람이 어떻게 저를 보일 수 있는가?

해결법

  1. ==============================

    1.나는 다음과 같은 스크립트를 사용하여이 문제를 해결했다 :

    나는 다음과 같은 스크립트를 사용하여이 문제를 해결했다 :

    import paramiko
    
    # Define connection info
    host_ip = 'xx.xx.xx.xx'
    user = 'xxxxxxxx'
    pw = 'xxxxxxxx'
    
    # Paths
    input_loc = '/nfs_home/appers/extracts/*/*.xml'
    output_loc = '/user/lcmsprod/output/cnielsen/'
    python_path = "/usr/lib/python_2.7.3/bin/python"
    hdfs_home = '/nfs_home/appers/cnielsen/'
    output_log = r'C:\Users\cnielsen\Desktop\MR_Test\MRtest011316_0.txt'
    
    # File names
    xml_lookup_file = 'product_lookups.xml'
    mapper = 'Mapper.py'
    reducer = 'Reducer.py'
    helper_script = 'Process.py'
    product_name = 'test1'
    output_ref = 'test65'
    
    # ----------------------------------------------------
    
    def buildMRcommand(product_name):
        space = " "
        mr_command_list = [ 'hadoop', 'jar', '/share/hadoop/tools/lib/hadoop-streaming.jar',
                            '-files', hdfs_home+xml_lookup_file,
                            '-file', hdfs_home+mapper,
                            '-file', hdfs_home+reducer,
                            '-mapper', "'"+python_path, mapper, product_name+"'",
                            '-file', hdfs_home+helper_script,
                            '-reducer', "'"+python_path, reducer+"'",
                            '-input', input_loc,
                            '-output', output_loc+output_ref]
    
        MR_command = space.join(mr_command_list)
        print MR_command
        return MR_command
    
    # ----------------------------------------------------
    
    def unbuffered_lines(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 = ''
    
    # ----------------------------------------------------
    
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host_ip, username=user, password=pw)
    
    # Build Commands
    list_dir = "ls "+hdfs_home+" -l"
    getmerge = "hadoop fs -getmerge "+output_loc+output_ref+" "+hdfs_home+"test_011216_0.txt"
    
    # Run Command
    stdin, stdout, stderr = client.exec_command(list_dir)
    ##stdin, stdout, stderr = client.exec_command(buildMRcommand(product_name))
    ##stdin, stdout, stderr = client.exec_command(getmerge)
    
    print "Executing command..."
    writer = open(output_log, 'w')
    
    for l in unbuffered_lines(stderr):
        e = '[stderr] ' + l
        print '[stderr] ' + l.strip('\n')
        writer.write(e)
    
    for line in stdout:
        r = '[stdout]' + line
        print '[stdout]' + line.strip('\n')
        writer.write(r)
    
    client.close()
    writer.close()
    
  2. from https://stackoverflow.com/questions/34165621/launch-hadoop-mapreduce-job-via-python-without-putty-ssh by cc-by-sa and MIT license