Gremlin is a Groovy based Graph Traversal Language and provides a very expressive way of explicitly scripting traversals through a Neo4j graph.
The Neo4j Gremlin Plugin provides an endpoint to send Gremlin scripts to the Neo4j Server, have them executed
on the server database and return the results as Neo4j Node and Relationship representations, thus keeping
the types throughout the REST API consistent. This results is quite verbose listings when returning Neo4j Node
,
Relationship
or Graph
representations. On the other hand, just return properties like in the Section 6.11.4, “Send a Gremlin Script - JSON encoded with table result”
example for returns tailored to specific needs.
Send a Gremlin Script, URL-encoded with UTF-8 encoding, e.g.
the equivalent of the Gremlin Script i = g.v(1);i.outE.inV
Example request
POST
http://localhost:7474/db/data/ext/GremlinPlugin/graphdb/execute_script
Accept:
application/json
Content-Type:
application/x-www-form-urlencoded
script=i+%3D+g.v%282%29%3Bi.outE.inV
Example response
200:
OK
Content-Type:
application/json
[ { "outgoing_relationships" : "http://localhost:7474/db/data/node/1/relationships/out", "data" : { "name" : "you" }, "traverse" : "http://localhost:7474/db/data/node/1/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/1/properties/{key}", "self" : "http://localhost:7474/db/data/node/1", "properties" : "http://localhost:7474/db/data/node/1/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/1/relationships/in", "extensions" : { "FunctionalTestPlugin" : { "connected_nodes" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/1/connected_nodes", "pathToReference" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/1/pathToReference", "getThisNodeOrById" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/1/getThisNodeOrById", "createRelationships" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/1/createRelationships", "getRelationshipsBetween" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/1/getRelationshipsBetween" }, "GraphCloner" : { "clonedSubgraph" : "http://localhost:7474/db/data/ext/GraphCloner/node/1/clonedSubgraph" } }, "create_relationship" : "http://localhost:7474/db/data/node/1/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/1/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}" } ]
Import a graph form a GraphML file can be achieved through the Gremlin GraphMLReader. The following script imports 3 nodes into Neo4j and then returns a list of all nodes in the graph.
Example request
POST
http://localhost:7474/db/data/ext/GremlinPlugin/graphdb/execute_script
Accept:
application/json
Content-Type:
application/json
{"script":"GraphMLReader.inputGraph(g, new URL('https://raw.github.com/neo4j/gremlin-plugin/master/src/data/graphml1.xml').openStream());g.V"}
Example response
200:
OK
Content-Type:
application/json
[ { "outgoing_relationships" : "http://localhost:7474/db/data/node/0/relationships/out", "data" : { }, "traverse" : "http://localhost:7474/db/data/node/0/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/0/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/0/properties/{key}", "self" : "http://localhost:7474/db/data/node/0", "properties" : "http://localhost:7474/db/data/node/0/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/0/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/0/relationships/in", "extensions" : { "FunctionalTestPlugin" : { "connected_nodes" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/0/connected_nodes", "pathToReference" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/0/pathToReference", "getThisNodeOrById" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/0/getThisNodeOrById", "createRelationships" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/0/createRelationships", "getRelationshipsBetween" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/0/getRelationshipsBetween" }, "GraphCloner" : { "clonedSubgraph" : "http://localhost:7474/db/data/ext/GraphCloner/node/0/clonedSubgraph" } }, "create_relationship" : "http://localhost:7474/db/data/node/0/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/0/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/0/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/0/relationships/in/{-list|&|types}" }, { "outgoing_relationships" : "http://localhost:7474/db/data/node/7/relationships/out", "data" : { "name" : "I" }, "traverse" : "http://localhost:7474/db/data/node/7/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/7/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/7/properties/{key}", "self" : "http://localhost:7474/db/data/node/7", "properties" : "http://localhost:7474/db/data/node/7/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/7/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/7/relationships/in", "extensions" : { "FunctionalTestPlugin" : { "connected_nodes" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/7/connected_nodes", "pathToReference" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/7/pathToReference", "getThisNodeOrById" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/7/getThisNodeOrById", "createRelationships" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/7/createRelationships", "getRelationshipsBetween" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/7/getRelationshipsBetween" }, "GraphCloner" : { "clonedSubgraph" : "http://localhost:7474/db/data/ext/GraphCloner/node/7/clonedSubgraph" } }, "create_relationship" : "http://localhost:7474/db/data/node/7/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/7/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/7/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/7/relationships/in/{-list|&|types}" }, { "outgoing_relationships" : "http://localhost:7474/db/data/node/8/relationships/out", "data" : { "name" : "you" }, "traverse" : "http://localhost:7474/db/data/node/8/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/8/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/8/properties/{key}", "self" : "http://localhost:7474/db/data/node/8", "properties" : "http://localhost:7474/db/data/node/8/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/8/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/8/relationships/in", "extensions" : { "FunctionalTestPlugin" : { "connected_nodes" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/8/connected_nodes", "pathToReference" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/8/pathToReference", "getThisNodeOrById" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/8/getThisNodeOrById", "createRelationships" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/8/createRelationships", "getRelationshipsBetween" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/8/getRelationshipsBetween" }, "GraphCloner" : { "clonedSubgraph" : "http://localhost:7474/db/data/ext/GraphCloner/node/8/clonedSubgraph" } }, "create_relationship" : "http://localhost:7474/db/data/node/8/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/8/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/8/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/8/relationships/in/{-list|&|types}" }, { "outgoing_relationships" : "http://localhost:7474/db/data/node/9/relationships/out", "data" : { "name" : "him" }, "traverse" : "http://localhost:7474/db/data/node/9/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/9/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/9/properties/{key}", "self" : "http://localhost:7474/db/data/node/9", "properties" : "http://localhost:7474/db/data/node/9/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/9/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/9/relationships/in", "extensions" : { "FunctionalTestPlugin" : { "connected_nodes" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/9/connected_nodes", "pathToReference" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/9/pathToReference", "getThisNodeOrById" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/9/getThisNodeOrById", "createRelationships" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/9/createRelationships", "getRelationshipsBetween" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/9/getRelationshipsBetween" }, "GraphCloner" : { "clonedSubgraph" : "http://localhost:7474/db/data/ext/GraphCloner/node/9/clonedSubgraph" } }, "create_relationship" : "http://localhost:7474/db/data/node/9/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/9/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/9/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/9/relationships/in/{-list|&|types}" } ]
The following script returns a sorted list
of all nodes connected via outgoing relationships
to node 1, sorted by their name
-property.
Example request
POST
http://localhost:7474/db/data/ext/GremlinPlugin/graphdb/execute_script
Accept:
application/json
Content-Type:
application/json
{"script":"g.v(12).outE.inV.sort{it.name}.toList()"}
Example response
200:
OK
Content-Type:
application/json
[ { "outgoing_relationships" : "http://localhost:7474/db/data/node/11/relationships/out", "data" : { "name" : "him" }, "traverse" : "http://localhost:7474/db/data/node/11/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/11/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/11/properties/{key}", "self" : "http://localhost:7474/db/data/node/11", "properties" : "http://localhost:7474/db/data/node/11/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/11/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/11/relationships/in", "extensions" : { "FunctionalTestPlugin" : { "connected_nodes" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/11/connected_nodes", "pathToReference" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/11/pathToReference", "getThisNodeOrById" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/11/getThisNodeOrById", "createRelationships" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/11/createRelationships", "getRelationshipsBetween" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/11/getRelationshipsBetween" }, "GraphCloner" : { "clonedSubgraph" : "http://localhost:7474/db/data/ext/GraphCloner/node/11/clonedSubgraph" } }, "create_relationship" : "http://localhost:7474/db/data/node/11/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/11/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/11/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/11/relationships/in/{-list|&|types}" }, { "outgoing_relationships" : "http://localhost:7474/db/data/node/10/relationships/out", "data" : { "name" : "you" }, "traverse" : "http://localhost:7474/db/data/node/10/traverse/{returnType}", "all_typed_relationships" : "http://localhost:7474/db/data/node/10/relationships/all/{-list|&|types}", "property" : "http://localhost:7474/db/data/node/10/properties/{key}", "self" : "http://localhost:7474/db/data/node/10", "properties" : "http://localhost:7474/db/data/node/10/properties", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/10/relationships/out/{-list|&|types}", "incoming_relationships" : "http://localhost:7474/db/data/node/10/relationships/in", "extensions" : { "FunctionalTestPlugin" : { "connected_nodes" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/10/connected_nodes", "pathToReference" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/10/pathToReference", "getThisNodeOrById" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/10/getThisNodeOrById", "createRelationships" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/10/createRelationships", "getRelationshipsBetween" : "http://localhost:7474/db/data/ext/FunctionalTestPlugin/node/10/getRelationshipsBetween" }, "GraphCloner" : { "clonedSubgraph" : "http://localhost:7474/db/data/ext/GraphCloner/node/10/clonedSubgraph" } }, "create_relationship" : "http://localhost:7474/db/data/node/10/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/10/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/10/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/10/relationships/in/{-list|&|types}" } ]
To send a Script JSON encoded, set the payload Content-Type Header.
In this example, find all the things that my friends like,
and return a table listing my friends by their name,
and the names of the things they like in a table with two columns,
ignoring the third named step variable I
.
Remember that everything in Gremlin is an iterator - in order
to populate the result table t
, iterate through the pipes with
>> -1
.
Example request
POST
http://localhost:7474/db/data/ext/GremlinPlugin/graphdb/execute_script
Accept:
application/json
Content-Type:
application/json
{"script":"i = g.v(15);t= new Table();i.as('I').out('know').as('friend').out('like').as('likes').table(t,['friend','likes']){it.name}{it.name} >> -1;t;"}
Example response
200:
OK
Content-Type:
application/json
{ "data" : [ [ "Joe", "cats" ], [ "Joe", "dogs" ] ], "columns" : [ "friend", "likes" ] }
Copyright © 2011 Neo Technology