Import / Export graph as json from remote server

1,400 views
Skip to first unread message

James Render

unread,
Jul 2, 2019, 11:53:44 AM7/2/19
to Gremlin-users
Please may I have some example code on how to export an entire graph from a remote server, and then how to import.

I'm using Java and the Gremlin console.  I'm using an older version of the server, 3.3.4 (as I'm also working with Neptune that uses 3.3.2).

All the examples I've seen use the in-memory graphs (Tinkergraph).

My attempt is:

var traversal = EmptyGraph.instance().traversal().withRemote(DriverRemoteConnection.using(getCluster()))
traversal.getGraph().io(graphson()).writeGraph("my-graph.json");


results in an empty file.

thanks


Kelvin Lawrence

unread,
Jul 2, 2019, 5:05:43 PM7/2/19
to Gremlin-users
Hi James, here's some code that will do it for you with Neptune and should work with most Gremlin Server implementations I would think.


The results of the export can be used to load via Neptune's bulk loader.

Hope that is useful

If that is more than you needed hopefully it will at least give you some pointers that help.

With  hosted graphs, including Neptune, it is not uncommon to find that they do not expose the Graph object or give access to the io() classes.

Cheers
Kelvin

Kelvin Lawrence

unread,
Jul 2, 2019, 5:59:13 PM7/2/19
to Gremlin-users
I should have added that the bulk loader expects CSV and not JSON so to load the results back into Neptune export as CSV rather than JSON.

James Render

unread,
Jul 3, 2019, 3:20:56 AM7/3/19
to Gremlin-users
thanks for the responses Kelvin.

I was looking for examples for use with Gremlin server.

My use case is to do integration testing by using the testcontainers framework and a docker image of gremlin server. Then I would like to load in some pre-defined data that I can write tests against.  I was looking to use Json as I find that most easily comprehended.

You're absolutely right about Neptune not giving access to the io() classes , probably to stop newbies like me trying to export a petabyte production graph!  I could export my graph from Neptune as JSON but not sure if it would then be compatible with Gremlin server (presumably this is backed by Tinkergraph in-memory by default).

Kelvin Lawrence

unread,
Jul 3, 2019, 8:52:51 AM7/3/19
to Gremlin-users
Ah I see, I had understood that you wanted to export from Neptune.

If you are using Gremlin server you have access to all of the io() classes.

From the Gremlin console (using the older graph object based approach) it would be as simple as:

gremlin> graph.io(graphson()).writeGraph("my-graph.json")

or using the newer TinkerPop 3.4 way

g.io("my-graph.json").write().iterate()

If you want to load that data into a different graph database (like Neptune) you will need to convert that JSON into a format that the graph DB can bulk import/load  or alternatively write a small program to load the data.

Kelvin Lawrence

unread,
Jul 3, 2019, 8:56:40 AM7/3/19
to Gremlin-users
One additional note, if you export your data as GraphML rather than JSON, there is a simple to run Python script/utility [1] that can convert GraphML into CSV

James Render

unread,
Jul 3, 2019, 9:31:33 AM7/3/19
to Gremlin-users
Thanks Kelvin, that's not working for me think I'm lacking the understanding of something fundamental.

I've a gremlin-server running on localhost:8182
I fire up the console and try the command but it doesn't know the graph object locally. When I remotely execute it does know the graph but doesn't export.

~/apache-tinkerpop-gremlin-console-3.3.7/bin/gremlin.sh
gremlin> :remote connect tinkerpop.server conf/remote.yaml
gremlin> graph.io(graphson()).writeGraph("my-graph.json")
No such property: graph for class: groovysh_evaluate
gremlin> :> graph
==>tinkergraph[vertices:357 edges:883]
gremlin> :> graph.io(graphson()).writeGraph("yooblr-graph.json")
==>null

James Render

unread,
Jul 3, 2019, 9:46:28 AM7/3/19
to Gremlin-users
Ah my bad!
logged onto the docker container and there sitting in the
/opt/gremlin-server
was a lovely graph.json

I shall now try to import!  thank you very much, helpful having someone work through the process

Fabio Lorenzi

unread,
Jul 3, 2019, 10:57:39 AM7/3/19
to Gremlin-users
Just as a clarification, if you plan to move to a managed cloud graphDB provider (e.g. Compose) It's not a given that you'll get access to the gremlin server or have privileges to navigate the filesystem hence importing/exporting to files could not be a feasible option

James Render

unread,
Jul 3, 2019, 11:08:24 AM7/3/19
to Gremlin-users
Agreed.
This is for integration testing against a testcontainer running gremlin-server such that I can exercise my repository/dao layer.
Reply all
Reply to author
Forward
0 new messages