-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryJoin.java
161 lines (149 loc) · 5.58 KB
/
BinaryJoin.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
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
160
161
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
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.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
/**
*
* @author Jade Koskela
*
* This class implements a triple equi-join as two binary joins.
* Input should be edges of the format (u,v), one per line.
*
*/
public class BinaryJoin extends Configured implements Tool{
static final String tempDir = "binaryJoin_temp";
public static class JoinMapper extends
Mapper<LongWritable, Text, Text, Text> {
Text outKey = new Text();
Text outValue = new Text();
byte relationPosition = 0;
String[] tuple;
@Override
public void setup(Context context) throws IOException,
InterruptedException {
super.setup(context);
String relation;
Configuration conf = context.getConfiguration();
FileSplit fs = (FileSplit)context.getInputSplit();
relation = fs.getPath().getName();
if(relation.contains("part-r"))
relation = fs.getPath().getParent().getName();
if(conf.get("left").equals(relation))
relationPosition |= 1;
if(conf.get("right").equals(relation))
relationPosition |= 2;
}
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
tuple = value.toString().split(",");
if((relationPosition & 1) > 0){
outKey.set(String.format("left,%s", tuple[1]));
outValue.set(tuple[0]);
context.write(outKey, outValue);
}
if((relationPosition & 2) > 0){
outKey.set(String.format("right,%s", tuple[0]));
outValue.set(tuple[1]);
context.write(outKey, outValue);
}
}
}
public static class JoinPart extends Partitioner<Text, Text>{
@Override
public int getPartition(Text key, Text value, int numPartitions) {
return (key.toString().split(",")[1].hashCode() & Integer.MAX_VALUE) % numPartitions;
}
}
public static class JoinReducer extends
Reducer<Text, Text, Text, NullWritable> {
Multimap<String, String> hashMap = HashMultimap.create();
Text outKey = new Text();
NullWritable outValue = NullWritable.get();
@Override
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
String[] k = key.toString().split(",");
Collection<String> list = new ArrayList<String>();
String valueString;
if(k[0].equals("left")){
for(Text t : values){
valueString = t.toString();
hashMap.put(k[1], valueString);
}
}
else if(k[0].equals("right")){
list = hashMap.get(k[1]);
for(Text t : values){
valueString = t.toString();
for(String s: list){
outKey.set(s + ',' + valueString);
context.write(outKey, outValue);
}
}
}
}
}
public int run(String[] args) throws Exception {
boolean result = false;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
String r1, r2, r3, output;
int numReducer;
if (args.length != 5) {
System.err.println("usage: BinaryJoin relation1 relation2 relation3 output numReducers");
ToolRunner.printGenericCommandUsage(System.err);
return -1;
}
r1 = args[0];
r2 = args[1];
r3 = args[2];
output = args[3];
numReducer = Integer.parseInt(args[4]);
fs.delete(new Path(tempDir), true);
result = joinStep(r1, r2, tempDir, numReducer);
if(!result) return -1;
result = joinStep(tempDir, r3, output, numReducer);
if(!result) return -1;
return 0;
}
public boolean joinStep(String left, String right, String output, int numReducer) throws Exception{
Job job = null;
Configuration conf = new Configuration();
conf.set("left", left);
conf.set("right", right);
job = new Job(conf);
job.setNumReduceTasks(numReducer);
job.setJarByClass(BinaryJoin.class);
job.setMapperClass(JoinMapper.class);
job.setMapOutputValueClass(Text.class);
job.setReducerClass(JoinReducer.class);
job.setOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setPartitionerClass(JoinPart.class);
FileInputFormat.setInputPaths(job, new Path(left), new Path(right));
FileOutputFormat.setOutputPath(job, new Path(output));
return job.waitForCompletion(true);
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new BinaryJoin(), args);
System.exit(exitCode);
}
}