Untitled
unknown
plain_text
a year ago
2.6 kB
5
Indexable
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; 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; // Define the SortStudNames class that contains the MapReduce job configuration public class SortStudNames { // Mapper class that defines the map operation public static class SortMapper extends Mapper<LongWritable, Text, Text, Text> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // Split the input line by comma String[] token = value.toString().split(","); // Write the student name as key, and a composite value (ID-name) as value context.write(new Text(token[1]), new Text(token[0] + "-" + token[1])); } } // Reducer class that defines the reduce operation public static class SortReducer extends Reducer<Text, Text, NullWritable, Text> { @Override public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { // For each value in the list of values for the same key, write it to the context for (Text details : values) { context.write(NullWritable.get(), details); } } } // Main method to configure and submit the MapReduce job public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Configuration conf = new Configuration(); Job job = new Job(conf, "SortStudNames"); job.setJarByClass(SortStudNames.class); job.setMapperClass(SortMapper.class); job.setReducerClass(SortReducer.class); // Set the types of the output key and value job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); // Set the input and output paths (taken from command line arguments for flexibility) FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); // Exit with a success or failure notification System.exit(job.waitForCompletion(true) ? 0 : 1); } }
Editor is loading...
Leave a Comment