-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStep1.java
76 lines (65 loc) · 3.08 KB
/
Step1.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
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 java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Step1 {
public static class MapperClass extends Mapper<LongWritable,Text,Text,IntWritable>{
public void map(LongWritable lineId,Text line, Mapper.Context context) throws IOException, InterruptedException {
String[] value = line.toString().split("\t");
if (value.length >= 2)
context.write(new Text(value[0]), new IntWritable(Integer.parseInt(value[2])));
}
}
public static class ReducerClass extends Reducer<Text, IntWritable,Text,IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> Values, Context context) throws IOException, InterruptedException {
int sum =0;
while(Values.iterator().hasNext()){
sum = sum + Values.iterator().next().get();
}
context.write(key,new IntWritable(sum));
}
}
public static class PartitionerClass extends Partitioner<Text,IntWritable>{
@Override
public int getPartition(Text key, IntWritable value, int numPartitions) {
return key.hashCode();
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
Job job = new Job(conf, "Trigram count");
FileSystem.setDefaultUri(conf, new URI(args[1]));
//set format for input and output
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.getConfiguration().set("mapreduce.output.basename",args[0].substring(args[0].length()-1));
//path to input and output files
FileInputFormat.addInputPath(job,new Path(args[0]));
FileSystem.get(conf).delete(new Path(args[1]),true);
FileOutputFormat.setOutputPath(job,new Path(args[1]));
job.setJarByClass(Step1.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(MapperClass.class);
job.setReducerClass(ReducerClass.class);
job.setPartitionerClass(PartitionerClass.class);
job.setCombinerClass(ReducerClass.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}