-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTableJoin.java
More file actions
159 lines (117 loc) · 4.35 KB
/
TableJoin.java
File metadata and controls
159 lines (117 loc) · 4.35 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Map;
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.MapWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
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 TableJoin {
public static class StdMarkWritable implements Writable {
private Text name;
private IntWritable assign_1;
private IntWritable assign_2;
public StdMarkWritable() {
this.name = new Text("");
this.assign_1 = new IntWritable(-1);
this.assign_2 = new IntWritable(-1);
}
public StdMarkWritable(Text name, IntWritable assign_1, IntWritable assign_2) {
super();
this.name = name;
this.assign_1 = assign_1;
this.assign_2 = assign_2;
}
public Text getName() {
return name;
}
public void setName(Text name) {
this.name = name;
}
public IntWritable getAssign_1() {
return assign_1;
}
public void setAssign_1(IntWritable assign_1) {
this.assign_1 = assign_1;
}
public IntWritable getAssign_2() {
return assign_2;
}
public void setAssign_2(IntWritable assign_2) {
this.assign_2 = assign_2;
}
@Override
public void readFields(DataInput data) throws IOException {
this.name.readFields(data);
this.assign_1.readFields(data);
this.assign_2.readFields(data);
}
@Override
public void write(DataOutput data) throws IOException {
this.name.write(data);
this.assign_1.write(data);
this.assign_2.write(data);
}
public String toString() {
return this.name.toString() + " " + this.assign_1.toString() + " " + this.assign_2.toString();
}
}
public static class StudentMapper extends Mapper<LongWritable, Text, Text, StdMarkWritable> {
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, StdMarkWritable>.Context context)
throws IOException, InterruptedException {
String [] parts = value.toString().split(",");
StdMarkWritable val = new StdMarkWritable();
val.setName(new Text(parts[1]));
context.write(new Text(parts[0]),val);
}
}
public static class MarkMapper extends Mapper<LongWritable, Text, Text, StdMarkWritable> {
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, StdMarkWritable>.Context context)
throws IOException, InterruptedException {
String [] parts = value.toString().split(",");
StdMarkWritable val = new StdMarkWritable();
val.setAssign_1(new IntWritable(Integer.parseInt(parts[1])));
val.setAssign_2(new IntWritable(Integer.parseInt(parts[2])));
context.write(new Text(parts[0]), val);
}
}
public static class MyReducer extends Reducer<Text, StdMarkWritable, Text, StdMarkWritable> {
@Override
protected void reduce(Text key, Iterable<StdMarkWritable> values,
Reducer<Text, StdMarkWritable, Text, StdMarkWritable>.Context context) throws IOException, InterruptedException {
StdMarkWritable value = new StdMarkWritable();
for(StdMarkWritable a: values)
if(a.getName().toString().equals("")) {
value.setAssign_1(new IntWritable(a.getAssign_1().get()));
value.setAssign_2(new IntWritable(a.getAssign_2().get()));
} else {
value.setName(new Text(a.getName().toString()));
}
context.write(key, value);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Table Join");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(StdMarkWritable.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(StdMarkWritable.class);
MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class, StudentMapper.class);
MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, MarkMapper.class);
job.setReducerClass(MyReducer.class);
FileOutputFormat.setOutputPath(job, new Path(args[2]));
job.waitForCompletion(true);
}
}