-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPAM.java
416 lines (348 loc) · 10.7 KB
/
SPAM.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import java.io.*;
import java.util.*;
import java.net.URI ;
import org.apache.hadoop.fs.* ;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class SPAM extends Configured implements Tool {
// Mapper
public static class Map extends MapReduceBase implements Mapper {
private List<String> seed = new ArrayList<String>();
private Hashtable hash = new Hashtable();
private String taskId;
private int maxTransSize ;
private int T ;
// get taskID
public void configure(JobConf job) {
taskId = job.get("mapred.task.id");
super.configure(job);
T = job.getInt("T",0);
maxTransSize = job.getInt("maxTS",3);
}
public void map(Object key, Object value, OutputCollector output, Reporter reporter) throws IOException {
// clear
hash.clear();
seed.clear();
// build seed hash table
String seed = value.toString() ;
seedToHash(seed) ;
// buildTree
//buildTree_file(seed, output) ;
buildTree_memory(seed , output) ;
}
// build tree memory
public void buildTree_memory(String seed , OutputCollector output ){
try{
List<String> cur_node = new ArrayList<String>() ;
String[] lists = seed.split("\t") ;
for(String s : lists){
String[] tmp = s.split(":") ;
cur_node.add(tmp[0]) ;
this.seed.add(tmp[0]) ;
}
//create level
int level=0 ;
while( !cur_node.isEmpty() ){
output.collect(new Text("level "+level) , new IntWritable(0)) ;
level++ ;
cur_node = createLevel(output, cur_node) ;
}
}catch(IOException e){
e.printStackTrace() ;
}
}
public List<String> createLevel(OutputCollector output , List<String> cur_node){
List<String> new_node = new ArrayList<String>() ;
try{
for(String node : cur_node) {
int is_seed=0 ;
for(String sd : seed){
//S_STEP
boolean[] result = s_step(getHash(node) , getHash(sd)) ;
String newnode = node+"->"+sd ;
//OUTPUT result
int value = sumArray(result) ;
// each map shouldn't decide T
output.collect(new Text(newnode) , new IntWritable(value)) ;
//NEXT NODE
String[] leaf = (newnode).split("->") ;
//if(leaf.length < maxTransSize && value >= T ){
if(leaf.length < maxTransSize ){
addHash( newnode , result) ;
new_node.add(newnode) ;
}
//I_STEP
String[] i_string = node.split("->") ;
String i_string_part = i_string[i_string.length-1] ;
String[] i_element = i_string_part.split(":") ;
String i_string_ele = i_element[i_element.length-1] ;
if(i_string_part.indexOf(sd) == -1 && seed.indexOf(i_string_ele) < seed.indexOf(sd) ){
result = i_step( getHash(node) , getHash(sd)) ;
newnode = node+":"+sd ;
value = sumArray(result) ;
// each map shouldn't decide T
output.collect(new Text(newnode) , new IntWritable(value)) ;
//NEXT NODE
//if( value >= T){
addHash(newnode,result) ;
new_node.add(newnode) ;
//}
}
//CHECK SEED
if( sd.equals(node) )
is_seed = 1 ;
}
if( is_seed==0 )
removeHash(node) ;
}
}catch(IOException e){
e.printStackTrace();
}
return new_node ;
}
// build tree
public void buildTree_file(String seed, OutputCollector output) {
try{
//initial file
File f = new File(taskId);
f.delete() ;
FileWriter fw = new FileWriter(taskId);
// add seed to tmp_file
String[] lists = seed.split("\t") ;
for(String s : lists){
String[] tmp = s.split(":") ;
fw.write(tmp[0]);
fw.write("\t");
fw.write(tmp[1]);
fw.write("\n");
this.seed.add(tmp[0]) ;
}
fw.close();
//create level
boolean conti=true ;
int level=0 ;
while(conti){
conti = createLevel(output , seed) ;
}
}catch(IOException e){
e.printStackTrace();
}
}
public boolean createLevel(OutputCollector output , String seed){
boolean hasNext = false ;
try{
FileWriter fwer = new FileWriter(taskId+".next") ;
//read tmp file
BufferedReader in = new BufferedReader(new FileReader(taskId));
String s = null ;
while( (s=in.readLine()) != null ){
// string to array
String[] s2 = s.split("\t") ;
String node = s2[0] ;
boolean[] node_array = stringToArray(s2[1]) ;
// array is ready
for(String sd : this.seed){
boolean[] seed_array = getHash(sd) ;
//S_STEP
String newnode = node+"->"+sd ;
boolean[] result = s_step( node_array , seed_array) ;
int value = sumArray(result) ;
//output
output.collect(new Text(newnode) , new IntWritable(value)) ;
//set condition
if((newnode.split("->")).length <= maxTransSize && value >= T ){
hasNext = true ;
fwer.write(newnode+"\t") ;
printFileArray(fwer , result) ;
}
//I_STEP
String[] i_string = node.split("->") ;
String i_string_part = i_string[i_string.length-1] ;
String[] i_element = i_string_part.split(":") ;
String i_string_ele = i_element[i_element.length-1] ;
if(i_string_part.indexOf(sd) == -1 && seed.indexOf(i_string_ele) < seed.indexOf(sd) ){
result = i_step( node_array , seed_array) ;
newnode = node+":"+sd ;
value = sumArray(result) ;
//output
output.collect(new Text(newnode) , new IntWritable(value)) ;
//set condition
if( value >= T) {
hasNext = true ;
fwer.write(newnode+"\t") ;
printFileArray(fwer,result) ;
}
}
}
}
fwer.close();
}catch(IOException e){
e.printStackTrace();
}
if(hasNext){
File f = new File(taskId+".next");
f.renameTo(new File(taskId)) ;
}
return hasNext ;
}
public boolean[] i_step(boolean[] from , boolean[] to){
boolean[] answer = new boolean[from.length] ;
for(int i=0 ; i<from.length ; i++)
answer[i] = from[i] & to[i] ;
return answer ;
}
public boolean[] s_step(boolean[] from , boolean[] to){
boolean[] answer = s_step_process(from) ;
for(int i=0 ; i<answer.length ; i++)
answer[i] = answer[i] & to[i] ;
return answer ;
}
public boolean[] s_step_process(boolean[] data){
boolean[] answer = new boolean[data.length] ;
boolean flag=false;
for(int index=0 ; index<data.length ; index++){
if(flag){
answer[index] = true ;
}else{ //flag==0
if(data[index] == true){
flag=true ;
}
answer[index] = false ;
}
}
return answer ;
}
// seed file to hash table
public void seedToHash(String seed){
String[] seed_list = seed.split("\t") ;
for(String s : seed_list){
String[] tmp = s.split(":") ;
String node = tmp[0] ;
boolean[] node_array = stringToArray(tmp[1]) ;
hash.put(node , node_array) ;
}
}
// string to boolean array
public boolean[] stringToArray(String s){
String[] str = s.split(" ") ;
boolean[] array = new boolean[str.length] ;
for(int i=0 ; i<str.length ; i++){
if( "1".equals(str[i]) )
array[i] = true ;
else
array[i] = false ;
}
return array ;
}
public boolean[] getHash(String key){
return (boolean[]) hash.get(key) ;
}
public void addHash(String key, boolean[] array){
hash.put(key , array) ;
}
public void removeHash(String key) {
hash.remove(key) ;
}
public int sumArray(boolean[] array){
int sum=0 ;
for(int i=0 ; i<array.length ; i++){
if(array[i])
sum+=1;
}
return sum ;
}
public void printFileArray(FileWriter fw , boolean[] array){
try{
for(int i=0 ; i<array.length ; i++ ){
fw.write(array[i]+" ") ;
}
fw.write("\n") ;
}catch(IOException e){
e.printStackTrace() ;
}
}
}
// Reducer
public static class Reduce extends MapReduceBase implements Reducer {
private int T ;
public void configure(JobConf job) {
super.configure(job);
T = job.getInt("T",0);
}
public void reduce(Object key, Iterator values, OutputCollector output, Reporter reporter)
throws IOException {
int sum=0 ;
while(values.hasNext()){
IntWritable v = (IntWritable) values.next() ;
sum += v.get();
// output.collect((Text)key, new IntWritable(v.get()) ) ;
}
if(sum>=T)
output.collect((Text)key, new IntWritable(sum) );
}
}
// Reducer for debug
public static class Reduce_debug extends MapReduceBase implements Reducer {
public void reduce(Object key, Iterator values, OutputCollector output, Reporter reporter)
throws IOException {
output.collect((Text)key, new IntWritable(0) ) ;
}
}
// Main
public int run(String[] args) throws Exception {
// Deal with argument
String input_file = null ;
String output_file = null ;
int T = 0;
int maxTransSize = 0 ;
int mapper = 1 ;
int reducer = 1 ;
for (int i = 0; i < args.length; i++) {
if ("-i".equals(args[i]))
input_file = args[++i] ;
if ("-o".equals(args[i]))
output_file = args[++i] ;
if ("-T".equals(args[i]))
T = Integer.valueOf(args[++i]) ;
if ("-maxTS".equals(args[i]))
maxTransSize = Integer.valueOf(args[++i]) ;
if ("-mapper".equals(args[i]))
mapper = Integer.valueOf(args[++i]) ;
if ("-reducer".equals(args[i]))
reducer = Integer.valueOf(args[++i]) ;
}
// Configue
JobConf conf = new JobConf(getConf(), SPAM.class);
conf.setJobName("SPAM");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(input_file));
FileOutputFormat.setOutputPath(conf, new Path(output_file));
conf.setNumMapTasks(mapper);
conf.setNumReduceTasks(reducer);
// set parameters
conf.setInt("T" , T) ;
conf.setInt("maxTS" , maxTransSize) ;
// delete output
FileSystem dfs = FileSystem.get(conf);
if (dfs.exists(new Path(output_file)))
dfs.delete(new Path(output_file), true);
// Run Map/Reduce
long start = System.nanoTime();
JobClient.runJob(conf);
long duration = System.nanoTime() - start;
System.err.println( "Job Time : "+ duration/1e+9 + " sec");
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new SPAM(), args);
System.exit(res);
}
}