Titan Console (aka Gremlin)
Read Gremlin Console to learn more about the tool:
The Gremlin Console is an interactive terminal or REPL that can be used to traverse graphs and interact with the data that they contain. It represents the most common method for performing ad-hoc graph analysis, small to medium sized data loading projects and other exploratory functions. The Gremlin Console is highly extensible, featuring a rich plugin system that allows new tools, commands, DSLs, etc. to be exposed to users.
TODO How to start gremlin with gremlin-scala?
TODO Imports in gremlin (via the plugin)
The following applies to Gremlin 3.0.0.M6
:
gremlin> Gremlin.version
==>3.0.0.M6
Use :help
or ?
to learn about the available commands.
gremlin> ?
For information about Groovy, visit:
http://groovy.codehaus.org
Available commands:
:help (:h ) Display this help message
? (:? ) Alias to: :help
:exit (:x ) Exit the shell
:quit (:q ) Alias to: :exit
import (:i ) Import a class into the namespace
:display (:d ) Display the current buffer
:clear (:c ) Clear the buffer and reset the prompt counter.
:show (:S ) Show variables, classes or imports
:inspect (:n ) Inspect a variable or the last result with the GUI object browser
:purge (:p ) Purge variables, classes, imports or preferences
:edit (:e ) Edit the current buffer
:load (:l ) Load a file or URL into the buffer
. (:. ) Alias to: :load
:save (:s ) Save the current buffer to a file
:record (:r ) Record the current session to a file
:history (:H ) Display, manage and recall edit-line history
:alias (:a ) Create an alias
:set (:= ) Set (or list) preferences
:register (:rc ) Registers a new command with the shell
:doc (:D ) Opens a browser window displaying the doc for the argument
:uninstall (:- ) Uninstall a Maven library and its dependencies from the Gremlin Console
:install (:+ ) Install a Maven library and its dependencies into the Gremlin Console
:plugin (:pin) Manage plugins for the Console
:remote (:rem) Define a remote connection
:submit (:> ) Send a Gremlin script to Gremlin Server
For help on a specific command type:
:help command
There are many ways to work with graphs using gremlin.
Go and create some vertices and edges yourself - I copied it verbatim from The Graph Structure where you can find a little more complex graph in Java 8 API.
Please note that the graph created using TinkerGraph.open()
is in no way related to Titan-hosted graphs.
gremlin> g = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> marko = g.addVertex(T.id, 1, "name", "marko", "age", 29)
==>v[1]
gremlin> vadas = g.addVertex(T.id, 2, "name", "vadas", "age", 27)
==>v[2]
gremlin> marko.addEdge("knows", vadas, T.id, 7, "weight", 0.5f)
==>e[7][1-knows->2]
It's that simple!
There's also the alternative to the above empty tinkergraph called TinkerPop Classic with 6 vertices and 6 edges:
gremlin> g = TinkerFactory.createClassic()
==>tinkergraph[vertices:6 edges:6]
The graph is described in Introduction to Graph Computing:
The TinkerPop classic graph is available with
TinkerGraph
viaTinkerFactory.createClassic()
. TinkerGraph is the reference implementation of TinkerPop3 and is used in nearly all the examples in this documentation.
If it were not enough, TinkerFactory.createTheCrew()
returns a toy graph demonstrating all of the new TinkerPop3 graph structure features like multi-properties, meta-properties, hidden properties, and graph variables. (see Vertex Properties):
gremlin> g = TinkerFactory.createTheCrew()
==>tinkergraph[vertices:6 edges:14]
You may also want to learn Graph API using The Graph of the Gods graph:
gremlin> g = GraphOfTheGodsFactory.create('/tmp/titan')
==>standardtitangraph[berkeleyje:/tmp/titan]
If you however want to use the graphs hosted on Titan infrastructure, use TitanFactory.open
with GraphOfTheGodsFactory.load
afterwards as follows:
gremlin> g = TitanFactory.open('../server/conf/titan-cassandra-es.properties')
==>standardtitangraph[cassandra:[127.0.0.1]]
gremlin> GraphOfTheGodsFactory.load(g)
==>null
// you may want to play with the graph as it's loaded
gremlin> hercules = g.V().has("name", "hercules").next()
==>v[1536]
gremlin> cerberus = g.V().has("name", "cerberus").next()
==>v[2816]
gremlin> hercules.query().direction(OUT).labels("battled").adjacent(cerberus).edges()*.valueMap()*.next()
==>[place:point[39.0,22.0], time:12]
gremlin> hercules.query().direction(OUT).labels("battled").adjacent(cerberus).edges().each { edge -> edge.property("time", edge.value("time") + 1) }
==>e[76o-16o-iz9-268][1536-battled->2816]
gremlin> hercules.query().direction(OUT).labels("battled").adjacent(cerberus).edges()*.valueMap()*.next()
==>[place:point[39.0,22.0], time:13]
FIXME Add the below snippet somewhere
gremlin> g.features()
==>FEATURES
> GraphFeatures
>-- Transactions: true
>-- Computer: true
>-- Persistence: true
>-- ThreadedTransactions: true
> VariableFeatures
>-- Variables: true
>-- BooleanValues: true
>-- ByteValues: true
>-- DoubleValues: true
>-- FloatValues: true
>-- IntegerValues: true
>-- LongValues: true
>-- MapValues: true
>-- MixedListValues: true
>-- SerializableValues: true
>-- StringValues: true
>-- UniformListValues: true
>-- BooleanArrayValues: true
>-- ByteArrayValues: true
>-- DoubleArrayValues: true
>-- FloatArrayValues: true
>-- IntegerArrayValues: true
>-- LongArrayValues: true
>-- StringArrayValues: true
> VertexFeatures
>-- AddVertices: true
>-- RemoveVertices: true
>-- MultiProperties: true
>-- MetaProperties: true
>-- UserSuppliedIds: false
>-- AddProperty: true
>-- RemoveProperty: true
>-- NumericIds: true
>-- StringIds: true
>-- UuidIds: true
>-- CustomIds: true
>-- AnyIds: true
> VertexPropertyFeatures
>-- UserSuppliedIds: false
>-- AddProperty: true
>-- RemoveProperty: true
>-- NumericIds: true
>-- StringIds: true
>-- UuidIds: true
>-- CustomIds: true
>-- AnyIds: true
>-- Properties: true
>-- BooleanValues: true
>-- ByteValues: true
>-- DoubleValues: true
>-- FloatValues: true
>-- IntegerValues: true
>-- LongValues: true
>-- MapValues: true
>-- MixedListValues: true
>-- SerializableValues: true
>-- StringValues: true
>-- UniformListValues: true
>-- BooleanArrayValues: true
>-- ByteArrayValues: true
>-- DoubleArrayValues: true
>-- FloatArrayValues: true
>-- IntegerArrayValues: true
>-- LongArrayValues: true
>-- StringArrayValues: true
> EdgeFeatures
>-- AddEdges: true
>-- RemoveEdges: true
>-- UserSuppliedIds: false
>-- AddProperty: true
>-- RemoveProperty: true
>-- NumericIds: true
>-- StringIds: true
>-- UuidIds: true
>-- CustomIds: true
>-- AnyIds: true
> EdgePropertyFeatures
>-- Properties: true
>-- BooleanValues: true
>-- ByteValues: true
>-- DoubleValues: true
>-- FloatValues: true
>-- IntegerValues: true
>-- LongValues: true
>-- MapValues: true
>-- MixedListValues: true
>-- SerializableValues: true
>-- StringValues: true
>-- UniformListValues: true
>-- BooleanArrayValues: true
>-- ByteArrayValues: true
>-- DoubleArrayValues: true
>-- FloatArrayValues: true
>-- IntegerArrayValues: true
>-- LongArrayValues: true
>-- StringArrayValues: true
Executing Gremlin scripts
Use -e
command line option.
./bin/gremlin.sh -e myscript.groovy