-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreducer.java
More file actions
113 lines (101 loc) · 3.23 KB
/
reducer.java
File metadata and controls
113 lines (101 loc) · 3.23 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
package ryosuke;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class reducer extends Reducer<Text, Text, Text, FloatWritable> {
static int START = 0, END = 1;
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
// mapから受け取った値をEdgeクラスに格納する
Iterator<Text> iterator = values.iterator();
List<Edge> edges = new ArrayList<Edge>();
while (iterator.hasNext())
{
String value = iterator.next().toString();
//System.out.println(value);
List<String> edge = Arrays.asList(value.split("\t"));
//System.out.println(Arrays.asList(edge));
Iterator<String> i = edge.iterator();
while(i.hasNext()){
String[] temp = i.next().split(",");
edges.add(new Edge(temp[START], temp[END]));
}
}
// クラスター係数の計算
String origin = key.toString();
// 隣接するノードを調べる
LinkedList<String> adjnode = new LinkedList<String>();
List<Edge> otheredges = new ArrayList<Edge>();
Iterator<Edge> e = edges.iterator();
while(e.hasNext()){
Edge tempe = e.next();
//System.out.printf("start: " + tempe.start + "end: " + tempe.end + "\n");
//System.out.println(tempe.start);
//System.out.println(origin);
if(tempe.start.equals(origin)){
adjnode.add(tempe.end);
}else{
otheredges.add(tempe);
//System.out.printf("start: " + tempe.start + "end: " + tempe.end + "\n");
}
}
System.out.println(adjnode);
//System.out.println(Arrays.asList(otheredges));
// 隣接するノード同士の組み合わせを見つける
int trianglecount = 0;
List<Edge> combination = combi((LinkedList<String>)adjnode.clone());
// クラスタ係数の三角形をカウントする
for(Edge ed : edges){
for(Edge com : combination){
if(ed.start.equals(com.start) && ed.end.equals(com.end)){
trianglecount++;
}
}
}
//System.out.println(key+": "+trianglecount);
System.out.println("size: "+adjnode.size());
float cluster = (float)trianglecount / (adjnode.size()*(adjnode.size()-1)/2);
System.out.println(key+": "+trianglecount + ": "+cluster);
//System.out.println(adjnode);
context.write(key, new FloatWritable(cluster));
}
private List<Edge> combi(LinkedList<String> adjnode) {
List<Edge> combi = new LinkedList<reducer.Edge>();
//Iterator<String> i = adjnode.iterator();
//System.out.println("=======start========");
//System.out.println(Arrays.asList(adjnode));
while(!adjnode.isEmpty()){
//System.out.printf("*");
String s = adjnode.pop();
for(String adj : adjnode){
combi.add(new Edge(s, adj));
}
}
//System.out.printf("\n");
/*
while(i.hasNext()){
String one = i.next();
Iterator<String> j = i;
while(j.hasNext()){
//combi.add(new Edge(i.next(), j.next()));
System.out.println(one + j.next());
}
//i.remove();
}
*/
return combi;
}
class Edge{
String start, end;
Edge(String start, String end){
this.start = start;
this.end = end;
}
}
}