1
|
1 package sample;
|
|
2
|
2
|
3 import java.io.File;
|
|
4 import java.io.FileInputStream;
|
|
5 import java.io.FileNotFoundException;
|
|
6 import java.io.FileOutputStream;
|
|
7 import java.io.IOException;
|
|
8
|
1
|
9 import com.tinkerpop.blueprints.Direction;
|
|
10 import com.tinkerpop.blueprints.Edge;
|
|
11 import com.tinkerpop.blueprints.Graph;
|
|
12 import com.tinkerpop.blueprints.Vertex;
|
|
13 import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
|
|
14 import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory;
|
2
|
15 import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader;
|
|
16 import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter;
|
1
|
17
|
|
18 public class CreateTinkerGraph {
|
|
19
|
2
|
20 public static final String filename = "./resources/tinkerpopDB";
|
|
21
|
1
|
22 public static void main(String[] args) {
|
2
|
23
|
|
24
|
|
25 try {
|
|
26
|
|
27 outputGraph();
|
|
28 readGraph();
|
|
29
|
|
30 } catch (IOException e) {
|
|
31 e.printStackTrace();
|
|
32 }
|
|
33
|
1
|
34 }
|
|
35
|
2
|
36 public static void outputGraph() throws IOException {
|
|
37 Graph graph = new TinkerGraph();
|
|
38 FileOutputStream out = new FileOutputStream(new File(filename));
|
|
39
|
|
40 Vertex a = graph.addVertex(null);
|
|
41 Vertex b = graph.addVertex(null);
|
|
42 Vertex c = graph.addVertex(null);
|
|
43 Vertex d = graph.addVertex(null);
|
|
44 a.setProperty("name", "maro");
|
|
45 b.setProperty("name", "Peter");
|
|
46 c.setProperty("name", "smith");
|
|
47 d.setProperty("name", "black");
|
|
48 Edge e = graph.addEdge(null, a, b, "knows");
|
|
49 Edge e2 = graph.addEdge(null, c, a, "knows");
|
|
50 Edge e3 = graph.addEdge(null, d, b, "knows");
|
|
51 System.out.println(e.getVertex(Direction.OUT).getProperty("name")
|
|
52 + "--" + e.getLabel() + "-->"
|
|
53 + e.getVertex(Direction.IN).getProperty("name"));
|
|
54 for (Edge edge : b.getEdges(Direction.IN, "knows")) {
|
|
55 Vertex v =edge.getVertex(Direction.OUT);
|
|
56 System.out.println(v.getProperty("name"));
|
|
57 }
|
|
58
|
|
59
|
|
60 GraphMLWriter.outputGraph(graph, out);
|
|
61
|
|
62 }
|
|
63
|
|
64 public static void readGraph() throws IOException {
|
|
65 Graph graph = new TinkerGraph();
|
|
66 FileInputStream in = new FileInputStream(new File(filename));
|
1
|
67
|
2
|
68 GraphMLReader.inputGraph(graph, in);
|
|
69 Vertex aa = graph.getVertex("1");
|
|
70 System.out.println("vertex " + aa.getId() + " has name " + aa.getProperty("name"));
|
|
71 if (aa.getProperty("aaa") != null)
|
|
72 System.out.println(aa.getProperty("aaa"));
|
|
73 for(Edge ee : aa.getEdges(Direction.OUT)) {
|
|
74 System.out.println(ee);
|
|
75 }
|
1
|
76
|
|
77 }
|
|
78
|
|
79
|
|
80 }
|