-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.java
More file actions
50 lines (45 loc) · 1.94 KB
/
Sort.java
File metadata and controls
50 lines (45 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class Sort {
public static class FrequencySortMap extends Mapper<Object, Text, IntWritable, Text>
{
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException
{
String word = value.toString().replaceAll("[^a-z-]", "");
int frequency = Integer.parseInt(value.toString().replaceAll("[^0-9]", ""));
context.write(new IntWritable(frequency), new Text(word));
}
}
public static class FrequencyReducer
extends Reducer<IntWritable, Text, IntWritable, Text> {
public void reduce(IntWritable freq, Iterable<Text> words,
Context context) throws IOException, InterruptedException {
for (Text key : words) {
context.write(freq, key);
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(Sort.class);
job.setMapperClass(FrequencySortMap.class);
job.setCombinerClass(FrequencyReducer.class);
job.setReducerClass(FrequencyReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}