Ask Your Question

Revision history [back]

To execute query strings in TinkerGraph, you can use the gremlin() method provided by the GraphTraversalSource object. This method takes a string parameter representing the Gremlin query to be executed, and returns a GraphTraversal object that can be used to iterate over the results of the query.

Here's an example of how to use the gremlin() method to execute a query in TinkerGraph:

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource
import org.apache.tinkerpop.gremlin.structure.T
import org.apache.tinkerpop.gremlin.structure.Vertex

// Create a new TinkerGraph instance
def graph = TinkerGraph.open()

// Add some vertices to the graph
def v1 = graph.addVertex(T.label, 'person', 'name', 'Alice', 'age', 30)
def v2 = graph.addVertex(T.label, 'person', 'name', 'Bob', 'age', 40)

// Create a GraphTraversalSource object
def g = graph.traversal()

// Execute a Gremlin query using the gremlin() method
def result = g.gremlin('g.V().hasLabel("person").has("age", gt(35)).values("name")')

// Iterate over the results and print them
result.each { println it }

This query uses Gremlin syntax to find all vertices in the graph with the "person" label and an "age" property greater than 35, and extracts their "name" property. The results are then printed to the console.