Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright [2013-2014] eBay Software Foundation
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *    http://www.apache.org/licenses/LICENSE-2.0
  *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ml.shifu.guagua.mapreduce.example.sum;
import  org.apache.hadoop.io.LongWritable;
import  org.apache.hadoop.io.Text;
SumSequenceFileWorker is used to accumulate the sum value for each line.

Each line of input should be number.

The master's sum value will be added to current iteration.

        extends
    private static final Logger LOG = LoggerFactory.getLogger(SumSequenceFileWorker.class);
    private List<Longlist;
    @Override
    public void init(
            WorkerContext<GuaguaWritableAdapter<LongWritable>, GuaguaWritableAdapter<LongWritable>> workerContext) {
        this. = new LinkedList<Long>();
    }
    @Override
    public GuaguaWritableAdapter<LongWritable> doCompute(
            WorkerContext<GuaguaWritableAdapter<LongWritable>, GuaguaWritableAdapter<LongWritable>> workerContext) {
        long sum = workerContext.getLastMasterResult() == null ? 0l : workerContext.getLastMasterResult().getWritable()
                .get();
        for(long longValuethis.) {
            sum += longValue;
        }
        .info("worker: {} ; sum: {}"workerContextsum);
        return new GuaguaWritableAdapter<LongWritable>(new LongWritable(sum));
    }
    @Override
    public void load(GuaguaWritableAdapter<Text> currentKeyGuaguaWritableAdapter<Text> currentValue,
            WorkerContext<GuaguaWritableAdapter<LongWritable>, GuaguaWritableAdapter<LongWritable>> workerContext) {
        this..add(Long.parseLong(currentValue.getWritable().toString()));
    }
    @Override
    public void initRecordReader(GuaguaFileSplit fileSplitthrows IOException {
        this.setRecordReader(new GuaguaSequenceRecordReader<Text, Text>(fileSplit, Text.class, Text.class));
        // this.setRecordReader(new GuaguaSequenceAsTextRecordReader(fileSplit));
    }
New to GrepCode? Check out our FAQ X