2012-10-04 2 views
0

나는 hadoop 프레임 워크에 초보이며지도는 추상화를 줄인다.가장 작은 숫자를 찾는다. hadoop streaming python

기본적으로,이 거대한 텍스트 파일에서 가장 작은 수 (로 구분 ",") 그래서

을 찾는 생각, 여기 mapper.py

#!/usr/bin/env python 

import sys 

# input comes from STDIN (standard input) 
for line in sys.stdin: 
# remove leading and trailing whitespace 
line = line.strip() 
# split the line into words 
numbers = line.split(",") 
# increase counters 
for number in numbers: 
    # write the results to STDOUT (standard output); 
    # what we output here will be the input for the 
    # Reduce step, i.e. the input for reducer.py 
    # 
    # tab-delimited; the trivial word count is 1 
    print '%s\t%s' % (number, 1) 

감속기

내 코드입니다
#!/usr/bin/env python 

from operator import itemgetter 
import sys 
smallest_number = sys.float_info.max 
for line in sys.stdin: 
# remove leading and trailing whitespace 
    line = line.strip() 

# parse the input we got from mapper.py 
    number, count = line.split('\t', 1) 
    try: 
      number = float(number) 
    except ValueError: 
      continue 

    if number < smallest_number: 
     smallest_number = number 
     print smallest_number <---- i think the error is here... there is no key value thingy 

    print smallest_number 

는 오류가 나는 얻을 :

 12/10/04 12:07:22 ERROR streaming.StreamJob: Job not successful. Error: NA 
     12/10/04 12:07:22 INFO streaming.StreamJob: killJob... 
      Streaming Command Failed! 
+0

어떤 결과가 나타 납니까? 뭐가 문제 야? "핵심 가치"는 무슨 소리 야? – Junuxx

+0

@Junuxx : 안녕하세요 .. 방금 오류를 게시했습니다 .. 기본적으로 ..지도에서 텍스트 파일의 가장 작은 숫자를 찾는 추상화를 어떻게 줄일 수 있습니까?/ 오류에 대한 얘기는 .. 매퍼가 나옵니다. number, 1) 기본적으로 워드 카운트 예에서 매퍼와 동일한 형식입니다. 감속기에서 내가 신경 쓰는 것은 숫자입니다. 숫자를 가져 와서 가장 작은 현재 숫자와 비교하고 스왑을합니까? – Fraz

+2

Hadoop없이 디버깅하는 것이 도움이 될 수 있습니다.'cat input | ./mapper.py | 정렬 | ./reducer.py' 성공적으로 실행됩니까? –

답변

0

우선, 하나의 감속기 만 사용하지 않으면 솔루션이 작동하지 않는다는 사실을 알았 으면합니다. 실제로 여러 개의 감속기를 사용하는 경우 각 감속기는 수신하는 가장 작은 번호를 뱉어 내고 둘 이상의 번호로 끝납니다. 그러나 다음 질문은이 문제 (즉, 하나의 작업)에 대해 하나의 감속기 만 사용해야하는 경우 MapReduce를 사용하여 얻을 수있는 것입니다. 트릭은 매퍼가 병렬로 실행된다는 것입니다. 반면 매퍼가 매번 읽은 값을 출력하지 않게하려면, 하나의 감속기가 순차적 솔루션보다 개선되지 않은 전체 데이터를 조사해야합니다. 이 문제를 해결하는 방법은 각 매퍼가 읽는 가장 작은 숫자 만 출력하는 것입니다. 또한 모든 매퍼 출력을 동일한 감속기로 보내려면 매퍼 출력 키가 모든 매퍼에서 동일해야합니다.

매퍼는 다음과 같이 표시됩니다

#!/usr/bin/env python        

import sys 

smallest = None 
for line in sys.stdin: 
    # remove leading and trailing whitespace   
    line = line.strip() 
    # split the line into words      
    numbers = line.split(",") 
    s = min([float(x) for x in numbers]) 
    if smallest == None or s < smallest: 
    smallest = s 

print '%d\t%f' % (0, smallest) 

감속기 :

#!/usr/bin/env python           

import sys 

smallest = None 
for line in sys.stdin: 
    # remove leading and trailing whitespace      
    line = line.strip() 
    s = float(line.split('\t')[1]) 
    if smallest == None or s < smallest: 
    smallest = s 

print smallest 

그래서 숫자를 정렬 할 맵리 듀스 프레임 워크 자체를 사용하여 예를 들어,이 문제를 해결하기 위해 다른 가능한 방법이있다 감속기가받는 첫 번째 숫자가 가장 작습니다. 더 많은 MapReduce 프로그래밍 패러다임을 이해하려면 this tutorial with examples, from my blog을 읽어보십시오.

관련 문제