Hadoop Reducer Values in Memory?

前端 未结 3 2058
故里飘歌
故里飘歌 2020-12-30 11:31

I\'m writing a MapReduce job that may end up with a huge number of values in the reducer. I am concerned about all of these values being loaded into memory at once.

3条回答
  •  伪装坚强ぢ
    2020-12-30 12:02

    You're reading the book correctly. The reducer does not store all values in memory. Instead, when looping through the Iterable value list, each Object instance is re-used, so it only keeps one instance around at a given time.

    For example in the follow code, the objs ArrayList will have the expected size after the loop but every element will be the same b/c the Text val instance is re-used every iteration.

    public static class ReducerExample extends Reducer {
    public void reduce(Text key, Iterable values, Context context) {
        ArrayList objs = new ArrayList();
                for (Text val : values){
                        objs.add(val);
                }
        }
    }
    

    (If for some reason you did want to take further action on each val, you should make a deep copy and then store it.)

    Of course even a single value could be larger than memory. In this case it's recommended to the developer to take steps to pare the data down in the preceding Mapper so that the value is not so large.

    UPDATE: See pages 199-200 of Hadoop The Definitive Guide 2nd Edition.

    This code snippet makes it clear that the same key and value objects are used on each 
    invocation of the map() method -- only their contents are changed (by the reader's 
    next() method). This can be a surprise to users, who might expect keys and vales to be 
    immutable. This causes prolems when a reference to a key or value object is retained 
    outside the map() method, as its value can change without warning. If you need to do 
    this, make a copy of the object you want to hold on to. For example, for a Text object, 
    you can use its copy constructor: new Text(value).
    
    The situation is similar with reducers. In this case, the value object in the reducer's 
    iterator are reused, so you need to copy any that you need to retain between calls to 
    the iterator.
    

提交回复
热议问题