Saturday, 29 January 2022

Gremlin: Enable tinkergraph plugin

You can enable tinkergraph plugin by executing below command.

:plugin use tinkerpop.tinkergraph

gremlin> :plugin use tinkerpop.tinkergraph
==>tinkerpop.tinkergraph activated
gremlin>

Once tinkergraph plugin is enabled, you can create a graph instance and work with it.

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin>
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin>

 

 

 

Previous                                                 Next                                                 Home

Gremlin: Get the capabilities of Graph store

Once you got the graph object, you can get the capabilities of graph store using graph.features() method.

gremlin> graph.features()
==>FEATURES
> GraphFeatures
>-- Transactions: true
>-- Persistence: true
>-- ThreadedTransactions: true
>-- Computer: true
>-- IoRead: true
>-- IoWrite: true
>-- ConcurrentAccess: true
> VariableFeatures
>-- Variables: true
>-- BooleanArrayValues: true
>-- StringArrayValues: true
>-- SerializableValues: false
>-- DoubleArrayValues: true
>-- UniformListValues: false
>-- IntegerArrayValues: true
>-- MapValues: true
>-- DoubleValues: true
>-- LongValues: true
>-- IntegerValues: true
>-- ByteValues: true
>-- LongArrayValues: true
>-- ByteArrayValues: true
>-- BooleanValues: true
>-- StringValues: true
>-- MixedListValues: false
>-- FloatArrayValues: true
>-- FloatValues: true
> VertexFeatures
>-- MetaProperties: true
>-- RemoveVertices: true
>-- MultiProperties: true
>-- AddVertices: true
>-- Upsert: false
>-- DuplicateMultiProperties: true
>-- NullPropertyValues: false
>-- NumericIds: true
>-- UserSuppliedIds: false
>-- RemoveProperty: true
>-- StringIds: false
>-- AddProperty: true
>-- CustomIds: false
>-- UuidIds: false
>-- AnyIds: false
> VertexPropertyFeatures
>-- NullPropertyValues: false
>-- NumericIds: false
>-- UserSuppliedIds: false
>-- RemoveProperty: true
>-- StringIds: true
>-- CustomIds: true
>-- UuidIds: false
>-- AnyIds: false
>-- Properties: true
>-- BooleanArrayValues: true
>-- StringArrayValues: true
>-- SerializableValues: false
>-- DoubleArrayValues: true
>-- UniformListValues: false
>-- IntegerArrayValues: true
>-- MapValues: true
>-- DoubleValues: true
>-- LongValues: true
>-- IntegerValues: true
>-- ByteValues: true
>-- LongArrayValues: true
>-- ByteArrayValues: true
>-- BooleanValues: true
>-- StringValues: true
>-- MixedListValues: false
>-- FloatArrayValues: true
>-- FloatValues: true
> EdgeFeatures
>-- Upsert: false
>-- AddEdges: true
>-- RemoveEdges: true
>-- NullPropertyValues: false
>-- NumericIds: false
>-- UserSuppliedIds: false
>-- RemoveProperty: true
>-- StringIds: false
>-- AddProperty: true
>-- CustomIds: true
>-- UuidIds: false
>-- AnyIds: false
> EdgePropertyFeatures
>-- Properties: true
>-- BooleanArrayValues: true
>-- StringArrayValues: true
>-- SerializableValues: false
>-- DoubleArrayValues: true
>-- UniformListValues: false
>-- IntegerArrayValues: true
>-- MapValues: true
>-- DoubleValues: true
>-- LongValues: true
>-- IntegerValues: true
>-- ByteValues: true
>-- LongArrayValues: true
>-- ByteArrayValues: true
>-- BooleanValues: true
>-- StringValues: true
>-- MixedListValues: false
>-- FloatArrayValues: true
>-- FloatValues: true

gremlin>

 

 

 

Previous                                                 Next                                                 Home

Record or save the output from Gremlin console to a file

Using :record command, you can save the output from Gremlin console to a file.

 

Start recording

:record start mylog.txt

 

Stop recording

:record stop

gremlin> :record start mylog.txt
Recording session to: "mylog.txt"
==>mylog.txt
gremlin>
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[cql:[localhost]], standard]
gremlin>
gremlin> g.V().count().next()
20:26:00 WARN  org.janusgraph.graphdb.transaction.StandardJanusGraphTx  - Query requires iterating over all vertices [()]. For better performance, use indexes
==>0
gremlin>
gremlin> g.V()
20:26:25 WARN  org.janusgraph.graphdb.transaction.StandardJanusGraphTx  - Query requires iterating over all vertices [()]. For better performance, use indexes
gremlin>
gremlin> :record stop
Recording stopped; session saved as: "mylog.txt" (297 bytes)
==>mylog.txt

 

mylog.txt contain below data.

 

mylog.txt

 

// OPENED: Tue Jan 11 20:25:38 IST 2022
// RESULT: mylog.txt
g = graph.traversal()
// RESULT: graphtraversalsource[standardjanusgraph[cql:[localhost]], standard]
g.V().count().next()
// RESULT: 0
g.V()
// RESULT: [JanusGraphStep([])]
:record stop
// CLOSED: Tue Jan 11 20:26:45 IST 2022

 

 

 

 

Previous                                                 Next                                                 Home

Get the version of Gremlin console

‘Gremlin.version()’ command prints the version of Gremlin console that you are using.

gremlin> Gremlin.version()
==>1.21.0

 

 

 

Previous                                                 Next                                                 Home

Use Gremlin console as REPL

 Gremlin console is built on top of Groovy, so you can execute any valid Groovy code in the Gremlin console.

gremlin> 2 + 3
==>5
gremlin>
gremlin> 2 * 3
==>6
gremlin>
gremlin> println "Hello World"
Hello World
==>null
gremlin>
gremlin> name = 'Krishna'
==>Krishna
gremlin>
gremlin> print "Hello {name}"
Hello {name}==>null
gremlin>
gremlin> print "Hello ${name}"
Hello Krishna==>null
gremlin>
gremlin>
gremlin>
gremlin> for (a in 1..10) {print "${a} "};println()
1 2 3 4 5 6 7 8 9 10
==>null

 


 

 

Previous                                                 Next                                                 Home

Get list of available commands in Gremlin console

:help command print all the available commands in Gremlin console.


 

Previous                                                 Next                                                 Home

Introduction to Gremlin

Gremlin is part of Apache Tinkerpop project, most graph databases use Gremlin to query the information in graph databases. JnausGraph use Gremlin query language to perform CRUD operations on a Graph.

 

One of the advantage of building applications using Gremlin is that you can avoid vendor-lock in, because application can be easily migrated to other graph databases supporting Gremlin.

Previous                                                 Next                                                 Home