Serializing basic gremlin queries

261 views
Skip to first unread message

Mark Melling

unread,
Apr 24, 2018, 5:19:49 PM4/24/18
to Gremlin-users
Hi,

I would like to be able serialize some basic gremlin queries (nothing too elaborate), so that they can be stored to file, retrieved and re-run. 

Is there a standard way of doing this, and if so how? Or is this something just not done!

Thanks

Mark
 

Stephen Mallette

unread,
Apr 24, 2018, 6:26:46 PM4/24/18
to Gremlin-users
It can be done a couple of ways:

1. Store it as a string and then just use GremlinGroovyScriptEngine to eval() it as a script
2. Store it as bytecode serialized to GraphSON then rehydrate it back to a traversal with JavaTranslator

The second option requires a bit more explanation maybe:

gremlin> bytecode = g.V().has('name','marko').asAdmin().getBytecode()
==>[[], [V(), has(name, marko)]]
gremlin> mapper = GraphSONMapper.build().create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@1eaf1e62
gremlin> json = mapper.writeValueAsString(bytecode)
==>{"@type":"g:Bytecode","@value":{"step":[["V"],["has","name","marko"]]}}
gremlin> t = org.apache.tinkerpop.gremlin.jsr223.JavaTranslator.of(g).translate(mapper.readValue(json,Bytecode.class))
==>v[1]




--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/969d29f3-655f-4cec-a7c7-9ad9a789789c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kelvin Lawrence

unread,
Apr 24, 2018, 6:27:28 PM4/24/18
to Gremlin-users
Hi Mark, you can save the results of a console session to a file and reload it later using the :load command.

Here is an example taken from my tutorial:

gremlin> :record start mylog.txt
Recording session to: "mylog.txt"

gremlin> g.V().count().next()
==>3618
gremlin> :record stop
Recording stopped; session saved as: "mylog.txt" (157 bytes)

This will create a file that looks like this

// OPENED: Tue Sep 12 10:43:40 CDT 2017
// RESULT: mylog.txt
g.V().count().next()
// RESULT: 3618 :record stop
// CLOSED: Tue Sep 12 10:43:50 CDT 2017

Here is a link to the part of the tutorial that covers this concept:

http://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#gremlinsave
Hope this helps
Kelvin
Enter code here...



Mark Melling

unread,
Apr 25, 2018, 5:09:02 AM4/25/18
to gremli...@googlegroups.com
Hi,

Thanks for the answers, I really appreciate it.

In my particular situation I think storing the bytecode serialized to GraphSON is the right approach.

Can I also take this opportunity to thank Kelvin for your really excellent tutorial, which I have started to work through.

Thanks

Mark




--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/e170b43f-7573-4880-9487-8fd376a0c9fa%40googlegroups.com.

Kelvin Lawrence

unread,
Apr 25, 2018, 9:25:44 AM4/25/18
to Gremlin-users
Thanks Mark - glad you got a good solution to your problem. This community is awesome and really responsive!

I have opened an issue against myself to add coverage of Gremlin ByteCode to the book. Just need to find the time :-)

Cheers
Kelvin
Reply all
Reply to author
Forward
0 new messages