Mapreduce question

Hello @itversity can you please help me in solving below map reduce question using java-

Assume you hv 2 files with N lines. Each line contains just number separated by , You need to write a mapreduce program to find out line wise total even number n total odd number.
Your sample output should be-
line1 totalEven:5 totalOdd:6
.
.
.
lineN totalEven:N totalOdd:N

Hi, I tried below code, it is working.
Keep all the files (there can be any number of input files) in a directory. Use this directory path as input.
There might be minor issues… u can resolve.
Here is the code.

package wordcount;

import datatype.StringIntValue;
import java.io.IOException;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import partitions.WordCountPartitioner;

public class EvenOddCount extends Configured implements Tool {

    private static class EOMap extends Mapper<LongWritable, Text, IntWritable, StringIntValue> {

            int i=0, j=-1, line=0;

            public void map(LongWritable offset, Text record, Context output) throws IOException, InterruptedException {

                    int e_count=0, o_count=0, junk;

                    //get line number
                    i=(int) offset.get();
                    if(i>j) {
                            j=i;
                            line+=1;
                    }
                    //get even & odd counts per line
                    for(String temp: record.toString().split(",")) {
                            if(((Integer.parseInt(temp))%2)==0)
                                    e_count+=1;
                            else
                                    o_count+=1;  
                    }
                    //write line number & counts of even & odd
                    output.write(new IntWritable(line), new StringIntValue("e",e_count));
                    output.write(new IntWritable(line), new StringIntValue("o",o_count));
            }
    }

    private static class EOReduce extends Reducer<IntWritable, StringIntValue, IntWritable, Text> {

            String formatted_output;
            public void reduce(IntWritable line, Iterable<StringIntValue> value, Context output) throws IOException, InterruptedException {
                    int e_count=0, o_count=0;
                    for(StringIntValue i: value) {
                            if(i.getInd().contains("e"))
                                    e_count+=i.getCount();
                            else
                                    o_count+=i.getCount();
                    }
                    formatted_output="Even count: "+e_count+"\t"+"Odd count: "+o_count;
                    output.write(line, new Text(formatted_output));

            }
    }


    public int run(String[] arg0) throws Exception {
            // TODO Auto-generated method stub

            Job job=Job.getInstance(getConf(), "even-odd count");
            job.setJarByClass(getClass());
            job.setInputFormatClass(TextInputFormat.class);
            job.setOutputFormatClass(TextOutputFormat.class);

            job.setMapperClass(EOMap.class);
            job.setReducerClass(EOReduce.class);

// job.setPartitionerClass(WordCountPartitioner.class);

// job.setNumReduceTasks(0);

            job.setMapOutputKeyClass(IntWritable.class);
            job.setMapOutputValueClass(StringIntValue.class);

            job.setOutputKeyClass(IntWritable.class);
            job.setOutputValueClass(Text.class);

            FileInputFormat.setInputPaths(job, new Path(arg0[0]));
            FileOutputFormat.setOutputPath(job, new Path(arg0[1]));

            return (job.waitForCompletion(true)? 0:1);
    }

    public static void main(String[] args) throws Exception {
            System.exit(ToolRunner.run(new EvenOddCount(), args));
    }

}

This is another class required…

package datatype;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class StringIntValue implements Writable {

    Text ind;
    IntWritable count;

    public StringIntValue() {
            super();
            this.ind = new Text("");
            this.count = new IntWritable(0);
    }

    public StringIntValue(String ind, int count) {
            super();
            this.ind = new Text(ind);
            this.count = new IntWritable(count);
    }

    public String getInd() {
            return ind.toString();
    }

    public void setInd(String ind) {
            this.ind = new Text(ind);
    }

    public int getCount() {
            return count.get();
    }

    public void setCount(int count) {
            this.count = new IntWritable(count);
    }

    public void readFields(DataInput arg0) throws IOException {
            // TODO Auto-generated method stub

            this.ind.readFields(arg0);
            this.count.readFields(arg0);
    }

    public void write(DataOutput arg0) throws IOException {
            // TODO Auto-generated method stub

            this.ind.write(arg0);
            this.count.write(arg0);

    }

    @Override
    public String toString() {
            return ind+"\t"+count;
    }

}

inputs…
file1
1,4,3,7,6,89
5,2,8,9,1,7,6,9
4,2,3,6,5,9,7,8,2
5,3,2
4,6,5,7
file2
1,4,3,7,6,8549,234,642
2,8,9,1,7,6,9
4,3,5,9,7,8,2
5,3
4,5,7,2,4,3
file3
54,7634,65,1,4,3,7,6,89
5,2,8,9,1,7,6,9
4,2,3,6,5,9,433,782,45,5,7,8,2
5,3,2,5,2,7,1,9,6,4,8,0
4,6,5,7,65,78,243,567,87

final output
1 Even count: 10 Odd count: 13
2 Even count: 9 Odd count: 14
3 Even count: 14 Odd count: 15
4 Even count: 7 Odd count: 10
5 Even count: 8 Odd count: 11