Skip to content

EvenOdd

  • Driver Class

MyDriver.java

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

// file system 
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

// box classes import 
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

// mapreduce imports
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MyDriver {
 public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

  Configuration conf = new Configuration();
  Job j = new Job(conf, "Evenodd");
  j.setJarByClass(MyDriver.class);
  j.setMapperClass(MyMapper.class);
  j.setReducerClass(MyReducer.class);
  j.setMapOutputKeyClass(Text.class);
  j.setMapOutputValueClass(IntWritable.class);

  FileInputFormat.addInputPath(j, new Path(args[0]));
  FileOutputFormat.setOutputPath(j, new Path(args[1]));

  System.exit(j.waitForCompletion(true) ? 0 : 1);
 }
}
  • Mapper Class

MyMapper.java

// exception handling 
import java.io.IOException;

// box classes import
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;

// import mapper class
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;

public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

 public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  String data[] = value.toString().split(","); // data = [85 131 993 392 689....]
  for (String num : data) {
   int number = Integer.parseInt(num);
   if ((number % 2) == 1) {
    context.write(new Text("ODD"), new IntWritable(number)); // ODD 85 131 993
   } else {
    context.write(new Text("EVEN"), new IntWritable(number)); // EVEN 392
   }
  }
 }
}
  • Reducer Class

MyReducer.java

// exceptions import
import java.io.IOException;

// import box classes
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

// import reducer class
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;

public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

 // reducer will recieve key value pair as ODD [ 85 131 993 491 539 985 413.....
 // EVEN [ 392 870 240 888 184 494 704 996 408....

 public void reduce(Text key, Iterable<IntWritable> values, Context context)
   throws IOException, InterruptedException {
  int sum = 0;
  if (key.equals("ODD")) {
   for (IntWritable value : values) {
    sum += value.get();
   }
  } else {
   for (IntWritable value : values) {
    sum += value.get();
   }
  }
  context.write(key, new IntWritable(sum));
 }
}
  • Input File

evenodd.txt

85,131,993,392,689,858,935,554,301,824,466,774,473,753,136,80,497,96,33,386,827,175
571,542,226,166,818,188,559,189,958,563,435,35,717,452,272,105,778,844,44,411,362,100
620,908,181,53,477,321,711,800,310,163,437,329,420,500,611,309,789,828,841,561,720,13
598,529,657,986,67,352,55,378,69,546,581,159,230,46,339,579,976,457,345,638,363,560
944,201,256,22,4,391,127,517,896,953,862,38,466,217,440,205,606,266,835,385,852,696
97,468,821,298,591,789,49,395,216,750,403,406,472,835,498,525,195,504,261,847,254,110
801,545,279,386,240,462,193,356,988,632,183,43,425,339,573,878,119,335,281,273,290,542
14,486,136,670,629,353,395,719,119,241,561,484,43,474,32,553,79,475,861,702,64,575
826,305,667,261,269,650,1,728,41,416,929,758,368,684,220,723,271,877,71,623,26,385
458,612,978,731,928,866,979,188,992,701,913,299,325,929,876,244,608,647,177,274,476,538
444,865,797,203,761,507,858,322,797,806,454,103,922,947,535,959,174,96,719,968,908,211
352,320,853,568,225,860,31,396,33,679,498,452,763,72,761,677,378,496,863,351,291,481
211,378,681,62,507,998,169,1000,914,737,307,555,143,257,979,682,712,308,648,342,55,30
396,357,480,672,866,856,198,149,139,265,522,734,366,939,628,213,411,177,90,193,882,930
779,129,176,620,165,652,174,164,301,824,466,774,473,753,136,80,497,96,33,386,827,175
798,35,305,558,2,718,329,483,112,793,338,732,908,725,497,96,416,680,518,11,838,676
271,369,330,644,890,312,131,145,402,412,339,618,59,813,142,120,973,919,268,778,906,357
559,935,791,111,493,757,236,112,159,720,853,642,519,285,66,971,758,577,573,514,17,836
680,635,163,599,227,632,275,375,777,337,133,152,370,838,661,305,85,933,784,172,728,123
788,462,537,144,598,947,61,409,31,503,417,935,87,815,649,548,666,240,456,420,89,490
130,71,563,141,157,144,899,893,574,988,535,405,22,255,356,547,293,365,342,579,442,795
599,357,791,27,567,291,903,378,616,626,646,155,198,499,974,249,764,142,563,455,823,778
398,922,359,297,864,123,942,817,256,848,348,579,753,624,246,808,149,863,273,672,874,886
967,772,225,353,380,539,409,366,869,345,701,303,829,18,68,586,891,740,533,217,373,524
472,980,183,926,634,313,962,332,440,391,315,489,30,999,387,175,227,453,946,739,148,65
24,152,618,142,412,962,477,695,583,725,51,470,943,439,618,162,686,882,714,170,586,109
864,242,591,957,736,583,938,851,475,694,386,865,890,782,231,518,438,187,748,768,153,259
333,815,885,380,453,598,632,115,803,782,129,612,845,230,43,294,41,832,154,160,622,837
324,752,325,328,679,893,288,78,828,852,55,82,35,89,940,463,528,821,391,916,412,946
595,82,794,629,396,825,782,416,854,916,720,256,115,781,885,389,368,551,957,361,18,984
232,179,981,30,872,747,465,47,379,649,383,223,546,898,245,19,459,862,387,315,764,624
637,765,910,294,668,637,230,558,487,646,430,653,25,317,534,435,606,566,512,767,139,464
366,367,837,89,493,394,289,278,955,502,696,357,151,120,419,395,857,633,482,563,43,263
282,219,882,703,138,934,576,487,295,582,423,693,721,937,159,902,849,458,401,357,816,369
672,892,6,988,567,663,661,485,693,135,796,79,623,92,361,683,800,849,584,652,212,573
869,474,132,862,662,656,287,720,636,937,56,107,498,874,600,845,388,297,108,56,796,118
991,693,540,390,89,492,396,75,964,558,99,221,853,775,376,815,917,504,763,595,688,293
349,970,951,515,568,991,280,53,646,59,636,906,764,460,48,102,4,608,974,139,513,55
851,671,744,54,501,971,193,156,538,925,878,918,733,377,227,390,49,120,221,834,536,312
585,700,103,405,85,246,844,443,346,10,425,525,541,382,528,781,437,967,60,748,649,730
788,501,473,934,541,27,537,581,228,390,164,777,745,365,652,588,9,405,443,112,43,820
413,552,688,226,728,784,834,165,958,565,837,471,805,295,374,397,467,34,777,260,288,463
74,385,954,69,338,136,613,413,4,986,942,746,200,311,498,880,903,446,68,417,300,531
515,601,833,678,772,697,397,180,684,757,27,720,364,899,31,803,133,878,774,422,909,439
669,896,529,97,631,828,229,748,858,267,389,592,48,383,278,252,372,101,781,224,229,131
652,79,144,765,428,521,934,439,332,713,56,258,836,966,890,42,236,900,628,598,923,152
325,108,96,680,756,274,276,704,565,970,20,994,829,742,506,314,186,689,511,40,932,24
129,744,880,161,727,420,971,653,326,997,62,402,310,93,559,182,355,318,185,869,507,228
947,214,288,291,299,966,302,850,100,416,239,258,757,419,78,372,122,406,857,348,349,823
290,798,599,736,612,770,917,693,808,98,82,690,443,76,299,316,272,570,44,213,936,438
530,949,63,819,284,859,579,670,326,237,685,946,805,240,736,89,255,473,977,402,462,80
28,759,901,873,612,761,37,802,960,546,30,834,352,603,246,420,972,345,294,377,571,122
450,453,10,479,25,503,537,825,454,197,343,642,709,927,39,338,455,254,707,539,942,644
988,414,501,545,403,413,924,519,140,878,15,996,225,421,786,298,879,198,521,351,724,171
82,890,3,401,149,79,471,914,787,361,52,673,547,958,483,72,140,971,773,274,883,201
738,229,272,958,689,796,491,115,76,219,341,590,596,95,329,252,11,701,953,701,643,782
204,178,556,538,841,578,448,777,341,8,978,204,923,660,518,212,814,2,446,335,72,520
524,351,147,187,313,4,503,83,151,588,187,273,660,23,888,427,45,284,39,694,945,154
527,93,485,95,972,961,93,374,324,923,365,484,961,987,704,507,296,319,961,810,604,813
566,343,482,567,353,876,748,136,465,629,186,392,784,574,45,938,87,245,627,122,833,479
432,629,93,926,214,624,274,165,328,375,91,916,606,362,794,334,668,967,646,693,153,757
555,409,337,995,824,520,752,99,47,14,33,957,797,663,406,17,674,445,898,883,663,517
844,271,844,433,122,763,955,361,487,149,174,379,950,144,223,491,40,906,362,964,574,978
705,84,383,284,919,577,109,394,430,792,408,102,550,860,846,356,963,463,873,821,92,97
996,79,437,147,98,116,7,360,18,887,142,76,196,86,771,817,748,664,940,571,542,226
166,818,188,559,189,958,563,435,35,717,452,272,105,778,844,44,411,362,100,620,908,181
53,477,321,711,800,310,163,437,329,420,500,611,309,789,828,841,561,720,13,598,529,657
986,67,352,55,378,69,546,581,159,230,46,339,579,976,457,345,638,363,560,944,201,256
22,4,391,127,517,896,953,862,38,466,217,440,205,606,266,835,385,852,696,97,468,821
298,591,789,49,395,216,750,403,406,472,835,498,525,195,504,261,847,254,110,801,545,279
386,240,462,193,356,988,632,183,43,425,339,573,878,119,335,281,273,290,542,14,486,136
670,629,353,395,719,119,241,561,484,43,474,32,553,79,475,861,702,64,575,826,305,667
261,269,650,1,728,41,416,929,758,368,684,220,723,271,877,71,623,26,385,458,612,978
731,928,866,979,188,992,701,913,299,325,929,876,244,608,647,177,274,476,538,444,865,797
203,761,507,858,322,797,806,454,103,922,947,535,959,174,96,719,968,908,211,352,320,853
568,225,860,31,396,33,679,498,452,763,72,761,677,378,496,863,351,291,481,211,378,681
62,507,998,169,1000,914,737,307,555,143,257,979,682,712,308,648,342,55,30,396,357,480
672,866,856,198,149,139,265,522,734,366,939,628,213,411,177,90,193,882,930,779,129,176
620,165,652,174,164,301,824,466,774,473,753,136,80,497,96,33,386,827,175,798,35,305
558,2,718,329,483,112,793,338,732,908,725,497,96,416,680,518,11,838,676,271,369,330
644,890,312,131,145,402,412,339,618,59,813,142,120,973,919,268,778,906,357,559,935,791
111,493,757,236,112,159,720,853,642,519,285,66,971,758,577,573,514,17,836,680,635,163
599,227,632,275,375,777,337,133,152,370,838,661,305,85,933,784,172,728,123,788,462,537
144,598,947,61,409,31,503,417,935,87,815,649,548,666,240,456,420,89,490,130,71,563
141,157,144,899,893,574,988,535,405,22,255,356,547,293,365,342,579,442,795,599,357,791
27,567,291,903,378,616,626,646,155,198,499,974,249,764,142,563,455,823,778,398,922,359
297,864,123,942,817,256,848,348,579,753,624,246,808,149,863,273,672,874,886,967,772,225
353,380,539,409,366,869,345,701,303,829,18,68,586,891,740,533,217,373,524,472,980,183
926,634,313,962,332,440,391,315,489,30,999,387,175,227,453,946,739,148,65,24,152,618
142,412,962,477,695,583,725,51,470,943,439,618,162,686,882,714,170,586,109,864,242,591
957,736,583,938,851,475,694,386,865,890,782,231,518,438,187,748,768,153,259,333,815,885
380,453,598,632,115,803,782,129,612,845,230,43,294,41,832,154,160,622,837,324,752,325
328,679,893,288,78,828,852,55,82,35,89,940,463,528,821,391,916,412,946,595,82,794
629,396,825,782,416,854,916,720,256,115,781,885,389,368,551,957,361,18,984,232,179,981
30,872,747,465,47,379,649,383,223,546,898,245,19,459,862,387,315,764,624,637,765,910
294,668,637,230,558,487,646,430,653,25,317,534,435,606,566,512,767,139,464,366,367,837
89,493,394,289,278,955,502,696,357,151,120,419,395,857,633,482,563,43,263,282,219,882
703,138,934,576,487,295,582,423,693,721,937,159,902,849,458,401,357,816,369,672,892,6
988,567,663,661,485,693,135,796,79,623,92,361,683,800,849,584,652,212,573,869,474,132
862,662,656,287,720,636,937,56,107,498,874,600,845,388,297,108,56,796,118,991,693,540
390,89,492,396,75,964,558,99,221,853,775,376,815,917,504,763,595,688,293,349,970,951

How to execute

  1. H_CLASSPATH=$(hadoop classpath)
  2. creating a varible to store the path for the jar files needed for compiling
  3. javac *.java -cp $H_CLASSPATH
  4. compiling files to create .class files
  5. jar -cvf even_odd.jar *.class
  6. creating jar file
  7. hadoop fs -put evenodd.txt
  8. uploading the input file to HDFS
  9. hadoop jar even_odd.jar MyDriver evenodd.txt evenodd
  10. executing the map reduce program
  11. hadoop fs -ls evenodd
  12. listing the output files
  13. hadoop fs -cat evenodd\part-r-00000
  14. lising the contents of output file (only if step 5 was successful)