-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apriori.java
353 lines (289 loc) · 11.4 KB
/
Apriori.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package org.apache.hadoop.examples;
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;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapred.JobConf;
import java.util.*;
public class Apriori {
public static class Mapper1 extends Mapper<Object, Text, Text, Text>{
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
context.write(new Text("#") , new Text(">," + value.toString()));
String[] line = value.toString().split(",");
for(int i = 1; i< line.length; i++)
{
String items = line[i];
context.write(new Text(items),new Text("1"));
}
}
}
public static class Reducer1 extends Reducer<Text,Text,Text,Text> {
private final int threshold = 60;
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
if(key.toString().equals("#"))
{
for(Text val : values)
{
context.write(key, val);
}
}
else
{
int support = 0;
for(Text val : values)
{
support += Integer.parseInt(val.toString());
}
if(support >= threshold)
context.write(new Text("i," + key.toString()), new Text(Integer.toString(support)));
}
}
}
public static class Mapper2 extends Mapper<Object, Text, Text, Text>{
private String flag;
protected void setup(Context context) throws IOException, InterruptedException {
FileSplit split = (FileSplit) context.getInputSplit();
flag = split.getPath().getName();
}
private ArrayList<String> freq_items = new ArrayList<String>();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
if(flag.substring(0,1).equals("f"))
{
String[] line = value.toString().split("\t");
if(line[0].equals("#"))
{
context.write(new Text(line[0]), new Text(line[1]));
}
else
{
String[] tokens = line[0].split(",");
freq_items.add(tokens[1]);
context.write(new Text("@"), new Text("iiii," + tokens[1]));
}
}
else
{
String[] line = value.toString().split("\t");
context.write(new Text("@"), new Text("pppp," + line[0]));
}
}
protected void cleanup(Context context) throws IOException, InterruptedException
{
if(context.getConfiguration().get("mode").equals("first"))
{
for(int i = 0 ; i < freq_items.size() - 1 ; i++)
{
for(int j = i + 1 ; j < freq_items.size() ; j++)
{
context.write(new Text("#"), new Text(freq_items.get(i) + "," + freq_items.get(j)));
}
}
}
}
}
public static class Reducer2 extends Reducer<Text,Text,Text,Text> {
private ArrayList<String> freq_items = new ArrayList<String>();
private ArrayList<String> freq_pairs = new ArrayList<String>();
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
if(key.toString().equals("#"))
{
for(Text val : values)
{
context.write(key, val);
}
}
else if(key.toString().equals("@"))
{
for(Text val : values)
{
String[] tokens = val.toString().split(",");
if(tokens[0].equals("iiii"))
{
freq_items.add(tokens[1]);
}
else
{
String str = "";
for(int i = 1 ; i < tokens.length ; i++)
str = str + tokens[i] + ",";
freq_pairs.add(str.substring(0, str.length() - 1));
}
}
for(String pr : freq_pairs)
{
for(String item : freq_items)
{
ArrayList<String> p = new ArrayList<String>();
String[] tokens = pr.split(",");
for(int i = 0 ; i < tokens.length ; i++)
{
p.add(tokens[i]);
}
int s = p.size();
if(!p.contains(item))
p.add(item);
if(p.size() == s + 1)
{
String output = "";
for(String val : p)
{
output = output+val+",";
}
context.write(new Text("#"), new Text(output.substring(0, output.length() - 1)));
}
}
}
}
}
}
public static class Mapper3 extends Mapper<Object, Text, Text, Text>{
public void map(Object key, Text value, Context context) throws IOException, InterruptedException
{
String[] line = value.toString().split("\t");
context.write(new Text(line[0]), new Text(line[1]));
}
}
public static class Reducer3 extends Reducer<Text,Text,Text,Text> {
private ArrayList< ArrayList<String> > transactions = new ArrayList< ArrayList<String> >();
private ArrayList< ArrayList<String> > pairs = new ArrayList< ArrayList<String> >();
private final int threshold = 60;
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
for(Text val : values)
{
String[] line = val.toString().split(",");
if(line[0].equals(">")) // transaction
{
ArrayList<String> tr = new ArrayList<String>();
for(String item : line)
{
tr.add(item);
}
transactions.add(tr);
}
else
{
ArrayList<String> pr = new ArrayList<String>();
for(String item : line)
{
pr.add(item);
}
pairs.add(pr);
}
}
// calculate support
ArrayList< Set<String> > all_pairs = new ArrayList< Set<String> >();
ArrayList<Integer> all_support = new ArrayList<Integer>();
for(int i = 0 ; i < pairs.size() ; i++)
{
int support = 0;
for(int j = 0 ; j < transactions.size() ; j++)
{
boolean isContains = true;
for(String item : pairs.get(i))
{
if(transactions.get(j).contains(item))
{
isContains = isContains && true;
}
else
{
isContains = isContains && false;
}
}
if(isContains)
support += 1;
}
if (support >= threshold)
{
Set<String> pair_set = new HashSet<String>();
for(String item : pairs.get(i))
{
pair_set.add(item);
}
all_pairs.add(pair_set);
all_support.add(support);
}
}
boolean[] flags = new boolean[all_pairs.size()]; // indicate if the pair is duplicate
Arrays.fill(flags, false);
//eliminate duplicate pairs
for(int i = 0 ; i < all_pairs.size() - 1 ; i++)
{
for(int j = i + 1 ; j < all_pairs.size() ; j++)
{
if(all_pairs.get(j).equals(all_pairs.get(i)))
{
flags[j] = true;
}
}
}
//output
int i = 0;
for(Set<String> s : all_pairs)
{
if(!flags[i])
{
String p = "";
for(String item : s)
{
p = p + item + ",";
}
p = p.substring(0, p.length() - 1);
context.write(new Text(p), new Text(Integer.toString(all_support.get(i))));
}
i++;
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf1 = new Configuration();
Job job1 = new Job(conf1, "Apriori");
job1.getConfiguration().set("mapreduce.output.basename", "fuck");
job1.setJarByClass(Apriori.class);
job1.setMapperClass(Mapper1.class);
job1.setReducerClass(Reducer1.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job1, new Path("data/groceries.txt"));
FileOutputFormat.setOutputPath(job1, new Path("output/Apriori_1"));
job1.waitForCompletion(true);
for(int i = 0 ; i < 3 ; i++)
{
Configuration conf2 = new Configuration();
if(i == 0)
conf2.set("mode", "first");
else
conf2.set("mode", "not first");
Job job2 = new Job(conf2, "Apriori");
job2.setJarByClass(Apriori.class);
job2.setMapperClass(Mapper2.class);
job2.setReducerClass(Reducer2.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);
if(i > 0)
FileInputFormat.addInputPath(job2, new Path("output/Apriori_1"));
FileInputFormat.addInputPath(job2, new Path("output/Apriori_" + Integer.toString(2*i + 1)));
FileOutputFormat.setOutputPath(job2, new Path("output/Apriori_" + Integer.toString(2*i + 2)));
job2.waitForCompletion(true);
Configuration conf3 = new Configuration();
Job job3 = new Job(conf3, "Apriori");
job3.setJarByClass(Apriori.class);
job3.setMapperClass(Mapper3.class);
job3.setReducerClass(Reducer3.class);
job3.setOutputKeyClass(Text.class);
job3.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job3, new Path("output/Apriori_" + Integer.toString(2*i + 2)));
FileOutputFormat.setOutputPath(job3, new Path("output/Apriori_" + Integer.toString(2*i + 3)));
job3.waitForCompletion(true);
}
}
}