Skip to content

Instantly share code, notes, and snippets.

@jrr
Last active February 3, 2021 22:20
Show Gist options
  • Save jrr/7ec736d830a87658cbb5333da8a39f9e to your computer and use it in GitHub Desktop.
Save jrr/7ec736d830a87658cbb5333da8a39f9e to your computer and use it in GitHub Desktop.
Single gremlin query from command line.md
version: "2"
services:
gremlin-server:
image: tinkerpop/gremlin-server
ports:
- "8182:8182"
#! /usr/bin/env bash
SERVER=$1
QUERY=$2
if [ "$#" -ne 2 ]; then
echo "Usage:"
echo "./gremlin-query.sh localhost 'g.V().elementMap()'"
exit 1
fi
./apache-tinkerpop-gremlin-console-3.4.10/bin/gremlin.sh -e query.groovy "$SERVER" "$QUERY"
println "Server: "+args[0]
cluster = Cluster.build(args[0]).create()
client = cluster.connect()
println "Query: "+args[1]
r = client.submit(args[1]).toList()
println "Result:"
r.each { println it }

I was looking for the easiest way to execute a single gremlin query from the command line, and cobbled this together using the gremlin console:

To use:

  • docker-compose up to fire up a local Tinkerpop
  • ./gremlin.query.sh host "query"

Example:

> ./gremlin-query.sh localhost "g.addV('foobar')"
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/path/to/apache-tinkerpop-gremlin-console-3.4.10/lib/groovy-2.5.14-indy.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Server: localhost
Query: g.addV('foobar')
Result:
result{object=v[0] class=org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex}
> ./gremlin-query.sh localhost "g.V().elementMap()"
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/path/to/apache-tinkerpop-gremlin-console-3.4.10/lib/groovy-2.5.14-indy.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Server: localhost
Query: g.V().elementMap()
Result:
result{object={id=0, label=foobar} class=java.util.LinkedHashMap}
>

It'll do for now, but I'd prefer:

  • to get rid of those warnings
  • more legible serialization of the result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment