com.github.milind.NumberAdditionPerLine.java Source code

Java tutorial

Introduction

Here is the source code for com.github.milind.NumberAdditionPerLine.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.milind;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 *
 * @author milind
 */
public class NumberAdditionPerLine {

    public static class NumberAdditionPerLineMapper extends Mapper<Object, Text, Text, IntWritable> {

        public void map(Object key, Text value, Mapper.Context context) throws IOException, InterruptedException {
            int sum = 0;
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                String str = itr.nextToken();
                sum = sum + Integer.parseInt(str);
            }
            context.write(new Text("Addition of numbers :" + value.toString() + " =>"), new IntWritable(sum));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Addition of Numbers Per Line");
        job.setJarByClass(NumberAdditionPerLine.class);
        job.setMapperClass(NumberAdditionPerLineMapper.class);
        job.setNumReduceTasks(0);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}