I'm using Gremlin console with AWS Neptune server.
I want to add custom DSL to shorten data analysis as described here: DSL
So I can run queries as:
g.persons('marko').knows('josh').hasNext()
Instead of:
g.V().hasLabel('person').has('name','marko').out('knows').hasLabel('person').has('name','josh').hasNext()
If I understood correctly the documentation they describe how to extend the TinkerPop server, but since Neptune doesn't support that, I want to extend the Gremlin Console.
Is there an easy way to do so?
The process for developing DSLs is the same for any case. Write your DSL as described in the documentation you referenced and then compile it to a jar file. Copy the jar file to the Gremlin Console /lib
directory. Start the console and use the import
command to reference your DSL classes so that you can use them.
From there you will need to connect to your graph using remote/bytecode-based traversals so assuming your DSL constructs a SocialTraversalSource
as shown in the example you can:
gremlin> import com.mycompany.gremlin.dsl.SocialTraversalSource
...
gremlin> social = traversal(SocialTraversalSource.class).withRemote(...)
...
gremlin> social.persons("marko").knows("josh")
If you wanted to develop your DSL purely in the Gremlin Console then recall that you are just working in a Groovy environment and therefore have access to its full scope:
gremlin> g = traversal().withRemote('conf/remote-graph.properties')
==>graphtraversalsource[emptygraph[empty], standard]
gremlin> GraphTraversal.metaClass.knows = { String personName ->
......1> return delegate.out("knows").hasLabel("person").has("name", personName)
......2> }
==>groovysh_evaluate$_run_closure1@6a638c79
gremlin> g.V().has('person','name','marko').knows('josh')
==>v[4]
gremlin> g.V().has('person','name','marko').knows('josh').out().elementMap()
==>[id:5,label:software,name:ripple,lang:java]
==>[id:3,label:software,name:lop,lang:java]
With a bit of metaprogramming you can hack in a DSL method without too much trouble.