Hadoop Map reduce in Java gives incorrect count for filtered columns

16 Views Asked by At

I have the following Map-Reducer in Java which reads data from a text file and filters the records based on 3 conditions. The requirement is to return the number of records in the text file which matches the given conditions.

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
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.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MovieFilter {

    public static class MovieFilterMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);

        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            // Skip the header line
            if (key.get() == 0) {
                return;
            }

            // Assuming each line in the text file represents a record
            String[] columns = value.toString().split(",");

            // Assuming column indices for popularity, vote_average, and vote_count
            try {
                double popularity = Double.parseDouble(columns[5].trim());
                double voteAverage = Double.parseDouble(columns[7].trim());
                double voteCount = Double.parseDouble(columns[8].trim());

                // Apply filtering conditions
                if (popularity > 500.0 && voteAverage > 8.0 && voteCount > 3000.0) {
                    context.write(new Text("count"), one);
                }
            } catch (NumberFormatException e) {
                // Handle parsing errors or missing values
                System.err.println("Error parsing values: " + value.toString());
            }
        }
    }

    public static class MovieFilterReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values, Context context)
                throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }

            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "movie filter");

        job.setJarByClass(MovieFilter.class);
        job.setMapperClass(MovieFilterMapper.class);
        job.setReducerClass(MovieFilterReducer.class);

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

        // Set input and output paths based on command-line arguments
        FileInputFormat.addInputPath(job, new Path(args[0])); // Input text file or directory
        FileOutputFormat.setOutputPath(job, new Path(args[1])); // Output directory

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

When I run it in Hadoop, it provides the output as "count 1". But there are 2 records in the text file which matches the conditions. Can I please know how to fix it.

0

There are 0 best solutions below