Created
May 9, 2016 14:17
-
-
Save milindjagre/162754ca83f1f86d3d7dff379d316197 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 GlobalNumberAddition { | |
public static class GlobalNumberAdditionMapper | |
extends Mapper < Object, Text, Text, IntWritable > { | |
int sum = 0; | |
public void map(Object key, Text value, Context context) throws IOException, | |
InterruptedException { | |
StringTokenizer itr = new StringTokenizer(value.toString()); | |
while (itr.hasMoreTokens()) { | |
String str = itr.nextToken(); | |
sum = sum + Integer.parseInt(str); | |
} | |
} | |
public void cleanup(Context context) throws IOException, | |
InterruptedException { | |
context.write(new Text("Addition of numbers is"), new IntWritable(sum)); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
Configuration conf = new Configuration(); | |
Job job = Job.getInstance(conf, "Global Addition of Numbers"); | |
job.setJarByClass(GlobalNumberAddition.class); | |
job.setMapperClass(GlobalNumberAdditionMapper.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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This MapReduce code will calculate Addition of all numbers in a file.