16.3.1 Exporting a Graph to Disk

You can save a graph loaded into memory to the disk in various formats. Therefore you can make sub-graphs and graph data computed at runtime through analytics persistent, for future use. The resulting file can be used later as input for the graph server (PGX).

Consider the following example where a graph is loaded into memory and PageRank analysis is executed on the graph.

Loading a Graph and Executing PageRank Analysis Using JShell
var g = session.readGraphWithProperties("<path_to_json>")
var rank = analyst.pagerank(g, 0.001, 0.85, 100)
Loading a Graph and Executing PageRank Analysis Using Java
PgxGraph g = session.readGraphWithProperties("<path_to_json>");
Analyst analyst = session.createAnalyst();
VertexProperty<Integer, Double> rank = analyst.pagerank(g, 0.001, 0.85, 100);
Loading a Graph and Executing PageRank Analysis Using Python
g = session.read_graph_with_properties("<path_to_json>")
analyst = session.create_analyst()
rank = analyst.pagerank(g, 0.001, 0.85, 100)

You can now store the graph, together with the result of the PageRank analysis and all original edge properties, as a file in edge-list format, on disk. When a graph is stored, you need to specify the graph format, a path where the file should be stored, the properties to store and a flag that specifies whether or not a file should be overwritten should a file with the same name already exist.

Storing a Graph Using JShell
var config = g.store(Format.EDGE_LIST, "<file-path>", List.of(rank), EdgeProperty.ALL, false)
Storing a Graph Using Java
var config = g.store(Format.EDGE_LIST, "<file-path>", List.of(rank), EdgeProperty.ALL, false);
Storing a Graph Using Python
config = g.store('edge_list', "<file-path>", vertex_properties = [rank], overwrite= False)

The graph data can now be found under the file path. The graph configuration returned by the store method can be used to load the new graph back into memory. To persist the graph configuration to disk as well, you can use the config's toString method to get a JSON representation:

Reloading a Graph Using JShell
var path = Paths.get("<file-path>")
Files.writeString(path, config.toString())
Reloading a Graph Using Java
import apache.commons.io.*; // PGX contains a version of Apache Commons IO
...
FileUtils.write(new File("<file-path>"), config.toString());
Reloading a Graph Using Python
with open("<file-path>","w"):
    f.write(str(config))