-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecondJob.java
103 lines (92 loc) · 3.22 KB
/
SecondJob.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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.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.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class SecondJob {
public static class UserRatingMapper
extends Mapper<LongWritable, Text, Text, Text> {
private Text movieID = new Text(), rating = new Text();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String v = value.toString();
String[] tokens = v.split(", ");
movieID.set(tokens[0].substring(2).trim());
rating.set("Rating, " + tokens[1]);
context.write(movieID, rating);
}
}
public static class MovieMapper
extends Mapper<LongWritable, Text, Text, Text> {
private Text movieID = new Text(), result = new Text();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String v = value.toString();
String[] tokens = v.split(", ");
if (tokens[2].equals("Comedy") || tokens[2].equals("Children")) {
movieID.set(tokens[0]);
result.set("Movie, " + tokens[1]);
context.write(movieID, result);
}
}
}
public static class MovieRatingReducer
extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
boolean genresEqualCC = false;
double avg = 0;
int count = 0;
String title = "";
for (Text val : values) {
String v = val.toString();
String[] tokens = v.split(", ");
if (v.charAt(0) == 'M') {
genresEqualCC = true;
title = tokens[1];
} else {
count++;
avg += Double.parseDouble(tokens[1]);
}
}
if (genresEqualCC) {
if (count != 0) {
avg /= count;
}
result.set(title + ", " + avg);
context.write(key, result);
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "second job");
job.setJarByClass(SecondJob.class);
//job.setNumReduceTasks(0);
//job.setCombinerClass(MovieRatingReducer.class);
job.setReducerClass(MovieRatingReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
MultipleInputs.addInputPath(
job,
new Path(args[0]),
TextInputFormat.class,
UserRatingMapper.class
);
MultipleInputs.addInputPath(
job,
new Path(args[1]),
TextInputFormat.class,
MovieMapper.class
);
FileOutputFormat.setOutputPath(job, new Path(args[2]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}