-
Notifications
You must be signed in to change notification settings - Fork 105
/
WriterTest.java
192 lines (148 loc) · 6.4 KB
/
WriterTest.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
package translation;
import com.google.gson.Gson;
import config.ConfigurationLoader;
import hdfs.FileUtil;
import jobs.PartitionedAnalysis;
import junit.framework.TestCase;
import messaging.Worker;
import models.PartitionDescription;
import models.ProcessorMessage;
import models.ProcessorMode;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.test.TestGraphDatabaseFactory;
import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import static org.neo4j.graphdb.DynamicRelationshipType.withName;
public class WriterTest extends TestCase {
@Test
public void testWriteToHadoop() throws Exception {
GraphDatabaseService db = setUpDb();
// Use test configurations
ConfigurationLoader.testPropertyAccess = true;
createSampleGraph(db);
Writer.exportSubgraphToHDFS(db);
}
private void createSampleGraph(GraphDatabaseService db) {
Transaction tx = db.beginTx();
List<Node> nodes = new ArrayList<>();
int max = 10000;
Node partitionNode = db.createNode();
partitionNode.addLabel(DynamicLabel.label("Category"));
int count = 0;
int partitionBlockCount = 50;
// Create nodes
for (int i = 0; i < max; i++) {
nodes.add(db.createNode());
nodes.get(i).addLabel(DynamicLabel.label("Node"));
partitionNode.createRelationshipTo(nodes.get(i), withName("HAS_CATEGORY"));
count++;
if(count >= partitionBlockCount && i != max - 1) {
count = 0;
partitionNode = db.createNode();
partitionNode.addLabel(DynamicLabel.label("Category"));
}
}
// Create PageRank test graph
for (int i = 0; i < (max / 2) - 1; i++) {
nodes.get(i).createRelationshipTo(nodes.get(i + (max / 2)), withName("CONNECTED_TO"));
nodes.get(i + (max / 2)).createRelationshipTo(nodes.get(i + 1), withName("CONNECTED_TO"));
if(i == (max / 2) - 2) {
nodes.get((i + 1) + (max / 2)).createRelationshipTo(nodes.get(0), withName("CONNECTED_TO"));
nodes.get(i + 1).createRelationshipTo(nodes.get((max / 2)), withName("CONNECTED_TO"));
}
}
tx.success();
tx.close();
}
@Test
public void testParallelWriteToHadoop() throws Exception {
GraphDatabaseService db = setUpDb();
// Use test configurations
ConfigurationLoader.testPropertyAccess = true;
createSampleGraph(db);
Writer.exportSubgraphToHDFSParallel(db);
}
@Test
public void testPartitionedAnalysis() throws Exception {
GraphDatabaseService db = setUpDb();
// Use test configurations
ConfigurationLoader.testPropertyAccess = true;
createSampleGraph(db);
// Export graph to HDFS and send message to Spark when complete
PartitionedAnalysis partitionedAnalysis = new PartitionedAnalysis("pagerank", "Category", "HAS_CATEGORY", "CONNECTED_TO", db);
partitionedAnalysis.analyzePartitions();
}
@Test
public void testParallelUpdate() throws Exception {
GraphDatabaseService db = setUpDb();
Transaction tx = db.beginTx();
// Use test configurations
ConfigurationLoader.testPropertyAccess = true;
Node nodePartition = db.createNode();
nodePartition.addLabel(DynamicLabel.label("Category"));
PartitionDescription partitionDescription = new PartitionDescription(nodePartition.getId(), "Category");
// Create sample PageRank result
String nodeList = "";
for(int i = 0; i < 100; i++)
{
db.createNode();
nodeList += i + " .001\n";
}
tx.success();
tx.close();
// Create test path
String path = ConfigurationLoader.getInstance().getHadoopHdfsUri() + "/test/propertyNodeList.txt";
writeListFile(path, nodeList);
ProcessorMessage processorMessage = new ProcessorMessage(path, "pagerank", ProcessorMode.Partitioned);
processorMessage.setPartitionDescription(partitionDescription);
BufferedReader br = FileUtil.readGraphAdjacencyList(processorMessage);
BufferedReader br2 = FileUtil.readGraphAdjacencyList(processorMessage);
// Test parallel update
PartitionedAnalysis.updatePartition(processorMessage, br, db);
PartitionedAnalysis.updatePartition(processorMessage, br2, db);
}
/**
* Write a file to HDFS line by line.
* @param path The path to the HDFS file to be created.
* @param nodeList The list of node IDs and properties to update in Neo4j.
* @throws java.io.IOException
* @throws java.net.URISyntaxException
*/
public static void writeListFile(String path, String nodeList) throws IOException, URISyntaxException {
FileSystem fs = FileUtil.getHadoopFileSystem();
Path updateFilePath = new Path(path);
BufferedWriter br=new BufferedWriter(new OutputStreamWriter(fs.create(updateFilePath,true)));
br.write("# Node Property Value List");
BufferedReader nodeListReader = new BufferedReader(new StringReader(nodeList));
String line;
while((line = nodeListReader.readLine()) != null) {
br.write( "\n" + line);
}
br.flush();
br.close();
}
private static GraphDatabaseService setUpDb()
{
return new TestGraphDatabaseFactory().newImpermanentDatabase();
}
public void testSendProcessorMessage() throws Exception {
ConfigurationLoader.testPropertyAccess=true;
// Serialize processor message
ProcessorMessage message = new ProcessorMessage("", "strongly_connected_components", ProcessorMode.Partitioned);
PartitionDescription partitionDescription = new PartitionDescription((long) 200, "Category");
message.setPartitionDescription(partitionDescription);
message.setPath(ConfigurationLoader.getInstance().getHadoopHdfsUri() + Writer.EDGE_LIST_RELATIVE_FILE_PATH);
Gson gson = new Gson();
String strMessage = gson.toJson(message);
// Send message to the Spark graph processor
Worker.sendMessage(strMessage);
}
}