1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.commands;
21
22 import com.tinkerpop.blueprints.Vertex;
23 import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode;
24 import com.tinkerpop.blueprints.util.io.graphson.GraphSONReader;
25 import com.tinkerpop.blueprints.util.io.graphson.GraphSONWriter;
26 import com.tinkerpop.frames.FramedGraph;
27 import eu.ehri.project.core.impl.Neo4jGraphManager;
28 import eu.ehri.project.core.impl.neo4j.Neo4j2Graph;
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.cli.Options;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.util.zip.GZIPInputStream;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class GraphSON extends BaseCommand {
63
64 final static String NAME = "graphson";
65
66 @Override
67 public String getHelp() {
68 return "Load or dump GraphSON data.";
69 }
70
71 @Override
72 public String getHelpFooter() {
73 return "Default is to dump to stdout";
74 }
75
76 @Override
77 public String getUsage() {
78 return String.format("%s [OPTIONS] [--load <filename>|--dump <filename>]", NAME);
79 }
80
81 @Override
82 protected void setCustomOptions(Options options) {
83 options.addOption(Option.builder("l")
84 .hasArg().type(String.class)
85 .longOpt("load")
86 .desc("Load a dump file").build());
87 options.addOption(Option.builder("d")
88 .hasArg().type(String.class)
89 .longOpt("dump")
90 .desc("Save a dump file").build());
91 options.addOption(Option.builder("b")
92 .hasArg().type(Integer.class)
93 .longOpt("buffer-size")
94 .desc("Transaction buffer size").build());
95 options.addOption(Option.builder()
96 .longOpt("skip-setting-labels")
97 .desc("Initialize indices after load").build());
98 }
99
100 @Override
101 public int execWithOptions(FramedGraph<?> graph,
102 CommandLine cmdLine) throws Exception {
103
104
105 if (cmdLine.hasOption("dump")) {
106 saveDump(graph, cmdLine.getOptionValue("dump"), cmdLine);
107 } else if (cmdLine.hasOption("load")) {
108 loadDump(graph, cmdLine.getOptionValue("load"), cmdLine);
109 } else {
110 saveDump(graph, "-", cmdLine);
111 }
112
113 return 0;
114 }
115
116 private void saveDump(FramedGraph<?> graph,
117 String filePath, CommandLine cmdLine) throws IOException {
118
119
120 if (filePath.contentEquals("-")) {
121
122 GraphSONWriter.outputGraph(graph, System.out, GraphSONMode.EXTENDED);
123 } else {
124
125 OutputStream out = Files.newOutputStream(Paths.get(filePath));
126 GraphSONWriter.outputGraph(graph, out, GraphSONMode.EXTENDED);
127 out.close();
128 }
129 }
130
131 private void loadDump(FramedGraph<?> graph,
132 String filePath, CommandLine cmdLine) throws Exception {
133 GraphSONReader reader = new GraphSONReader(graph);
134
135 InputStream readStream = System.in;
136 if (!filePath.equals("-")) {
137 InputStream inputStream = Files.newInputStream(Paths.get(filePath));
138 readStream = filePath.toLowerCase().endsWith(".gz")
139 ? new GZIPInputStream(inputStream)
140 : inputStream;
141 }
142
143 int bufferSize = cmdLine.hasOption("buffer-size")
144 ? Integer.parseInt(cmdLine.getOptionValue("buffer-size"))
145 : 1000;
146
147 try {
148 reader.inputGraph(readStream, bufferSize);
149 if (!cmdLine.hasOption("skip-setting-labels")) {
150 if (graph.getBaseGraph() instanceof Neo4j2Graph) {
151
152 @SuppressWarnings("unchecked")
153 FramedGraph<Neo4j2Graph> neo4j2Graph = ((FramedGraph<Neo4j2Graph>) graph);
154 Neo4jGraphManager manager = new Neo4jGraphManager<>(neo4j2Graph);
155 int i = 0;
156 for (Vertex v : graph.getVertices()) {
157 manager.setLabels(v);
158 i++;
159 if (i % 10000 == 0) {
160 neo4j2Graph.getBaseGraph().commit();
161 }
162 }
163 System.err.println("Labelled " + i + " vertices");
164 }
165 }
166 } finally {
167 readStream.close();
168 }
169 }
170 }