-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPAM.java.file
More file actions
312 lines (260 loc) · 7.97 KB
/
SPAM.java.file
File metadata and controls
312 lines (260 loc) · 7.97 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
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
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) ;
}
// 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 ;
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 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() ;
// output.collect((Text)key, new IntWritable(v.get()) ) ;
sum += 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 ;
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]) ;
}
// 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(13);
// 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
JobClient.runJob(conf);
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new SPAM(), args);
System.exit(res);
}
}