2014-06-12 3 views
0

나는 Reducer Class의 기본 실행 방법과 동일한이 간단한 코드를 작성했지만 완전히 이상한 일이 발생합니다. 여기 이상한 HashMap 결과 - Java, Hadoop

은 기본 실행 방법 :

public void More ...run(Context context) throws IOException, InterruptedException { 
    setup(context); 
    while (context.nextKey()) { 
     reduce(context.getCurrentKey(), context.getValues(), context); 
    } 
    cleanup(context); 
} 

출력 :

New reducer: 0 
Reducer: 0:9,2:5 
end of this reducer 

Reducer: 0:9,5:7 
end of this reducer 

(키 많이)

Reducer: 7:7,6:7 
end of this reducer 

Reducer: 7:7,7:6 
end of this reducer 

여기 내 오버라이드 (override) 방법 :

@Override 
    public void run(Context context) throws IOException, InterruptedException { 
     setup(context); 

     HashMap<Text,HashSet<Text>> map = new HashMap<Text,HashSet<Text>>(); 

     while (context.nextKey()) { 
      //reduce(context.getCurrentKey(),context.getValues(),context); 
      Text key = context.getCurrentKey(); 
      map.put(key, new HashSet<Text>()); 
      for(Text v : context.getValues()){ 
       map.get(key).add(v); 
      } 
     } 

     for(Text k : map.keySet()){ 
      reduce(k,map.get(k),context); 
     } 
     cleanup(context); 
    } 
,451,515,

출력 :

New reducer: 0 

Reducer: 7:7,7:6 
end of this reducer 

(키 많이)

Reducer: 7:7,7:6 
end of this reducer 

내 문제는 내가 해시 맵에 키와 값을 복사 할 경우 첫째 아무것도 제대로 호출을 줄일 작동하지 않는다는 것이다 결국 그것은 동일한 키 (해시 맵에 저장된 첫 번째 키)를 여러 번 반복해서 전달합니다./ 누구든지 도와 줄 수 있습니까? 이 작업을 올바르게 수행하려면 어떻게해야합니까? 열쇠를 감속기에 보내기 전에 열쇠를 전처리하기 때문에이 작업을 수행해야합니다. 미리 감사드립니다.

+0

죄송합니다! 나는 그것이 더 좋아 보이기를 바란다) –

답변

1

하둡은 쓰기 가능 개체를 다시 사용합니다. 따라서 컬렉션에 추가하기 전에 새 컬렉션을 만들어야합니다.

과 같을 것이다 일을 복사하는 코드를 변경 : 긴 출력

while (context.nextKey()) { 
     Text key = new Text(context.getCurrentKey()); 
     map.put(key, new HashSet<Text>()); 
     for(Text v : context.getValues()){ 
      map.get(key).add(new Text(v)); 
     } 
} 
+0

그리고 물론. 내가 필요한 평판을 얻게 될 때 ... 당신은 내 표를 완전히 가지고있다! –

+0

@developer :) 그냥 내 대답을 받아 들여라. upvotes에 대해서는 걱정할 필요가 없다. –

+0

Stings을 쓸 때도 문제가 발생합니까? 아니면 Text 객체에서만 발생합니까? –