|
| 1 | +# Gremlin API |
| 2 | + |
| 3 | +!!! warning |
| 4 | + The Gremlin API is still under development and has some [limitations](../query/gremlin/gremlin-limits.md). |
| 5 | + The implementation may not allow some advanced features of Gremlin and it's |
| 6 | + performance is unknown in comparison to standard Gaffer `OperationChains`. |
| 7 | + |
| 8 | +## What is Gremlin? |
| 9 | + |
| 10 | +[Gremlin](https://tinkerpop.apache.org/gremlin.html) is a query language for |
| 11 | +traversing graphs. It is a core component of the Apache Tinkerpop library and |
| 12 | +allows users to easily express more complex graph queries. |
| 13 | + |
| 14 | +GafferPop is a lightweight Gaffer implementation of the [TinkerPop framework](https://tinkerpop.apache.org/), |
| 15 | +where TinkerPop methods are delegated to Gaffer graph operations. |
| 16 | + |
| 17 | +The addition of Gremlin as query language in Gaffer allows users to represent |
| 18 | +complex graph queries in a simpler language, akin to other querying languages |
| 19 | +used in traditional and NoSQL databases. It also has wide support for various |
| 20 | +languages; for example you can write queries in Python via the [`gremlinpython`](https://pypi.org/project/gremlinpython/) |
| 21 | +library. |
| 22 | + |
| 23 | +!!! tip |
| 24 | + In-depth tutorials on Gremlin as a query language and its associated libraries |
| 25 | + can be found in the [Apache Tinkerpop Gremlin docs](https://tinkerpop.apache.org/gremlin.html). |
| 26 | + |
| 27 | +## How to Query a Graph |
| 28 | + |
| 29 | +There are two main methods of using Gremlin in Gaffer, these are via a websocket |
| 30 | +similar to a typical [Gremlin Server](https://tinkerpop.apache.org/docs/current/reference/#connecting-gremlin-server) |
| 31 | +or by submitting queries via the REST Endpoints like standard Gaffer Operations. |
| 32 | +Once connected, the [Gremlin in Gaffer](../query/gremlin/gremlin.md) page |
| 33 | +provides a simple comparison of Gremlin compared to Gaffer Operations. |
| 34 | + |
| 35 | +!!! note |
| 36 | + Both methods require a running [Gaffer REST API](./rest-api.md) instance. |
| 37 | + |
| 38 | +### Websocket API |
| 39 | + |
| 40 | +The websocket provides the most _standard_ way to use the Gremlin API. The |
| 41 | +Gaffer REST API provides a Gremlin server-like experience via a websocket at |
| 42 | +`/gremlin`. This can be connected to to provide a graph traversal source for |
| 43 | +spawning queries. |
| 44 | + |
| 45 | +The websocket should support all standard Gremlin tooling and uses GraphSONv3 |
| 46 | +serialisation for communication. To connect a tool like [`gremlinpython`](https://pypi.org/project/gremlinpython/) |
| 47 | +we can do something similar to [`gafferpy`](./python-api.md). First import the |
| 48 | +required libraries (many of these will be needed later for queries): |
| 49 | + |
| 50 | +```python |
| 51 | +from gremlin_python.process.anonymous_traversal import traversal |
| 52 | +from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection |
| 53 | +from gremlin_python.driver.serializer import GraphSONSerializersV3d0 |
| 54 | +from gremlin_python.process.graph_traversal import __ |
| 55 | +``` |
| 56 | + |
| 57 | +We can then establish a connection to the Gremlin server and save the reference |
| 58 | +(typically called `g`): |
| 59 | + |
| 60 | +```python |
| 61 | +# Setup a connection with the REST API running on localhost |
| 62 | +g = traversal().with_remote(DriverRemoteConnection('ws://localhost:8080/gremlin', 'g', message_serializer=GraphSONSerializersV3d0())) |
| 63 | +``` |
| 64 | + |
| 65 | +Now that we have the traversal reference this can be used to spawn graph traversals |
| 66 | +and get results back. |
| 67 | + |
| 68 | +### REST API Endpoints |
| 69 | + |
| 70 | +The Gremlin endpoints provide a similar interface to running Gaffer Operations. |
| 71 | +They accept a plaintext Gremlin Groovy or OpenCypher query and will return |
| 72 | +the results in [GraphSONv3](https://tinkerpop.apache.org/docs/current/dev/io/#graphson-3d0) |
| 73 | +format. |
| 74 | + |
| 75 | +The two endpoints are: |
| 76 | + |
| 77 | +- `/rest/gremlin/execute` - Runs a Gremlin Groovy script and outputs the result |
| 78 | + as GraphSONv3 JSON. |
| 79 | +- `/rest/gremlin/cypher/execute` - Translates a Cypher query to Gremlin and |
| 80 | + executes it returning a GraphSONv3 JSON result. Note will always append a |
| 81 | + `.toList()` to the translation. |
| 82 | + |
| 83 | +A query can be submitted via the Swagger UI or simple POST request such as: |
| 84 | + |
| 85 | +```bash |
| 86 | +curl -X 'POST' \ |
| 87 | + 'http://localhost:8080/rest/gremlin/execute' \ |
| 88 | + -H 'accept: application/x-ndjson' \ |
| 89 | + -H 'Content-Type: text/plain' \ |
| 90 | + -d 'g.V().hasLabel('\''something'\'').toList()' |
| 91 | +``` |
| 92 | + |
| 93 | +You can also utilise [Gafferpy](./python-api.md) to connect and run queries |
| 94 | +using the endpoints. |
| 95 | + |
| 96 | +```python |
| 97 | +from gafferpy import gaffer_connector |
| 98 | + |
| 99 | +gc = gaffer_connector.GafferConnector("http://localhost:8080/rest") |
| 100 | + |
| 101 | +# Execute and return gremlin |
| 102 | +gremlin_result = gc.execute_gremlin("g.V('1').toList()") |
| 103 | + |
| 104 | +# Execute and return cypher |
| 105 | +cypher_result = gc.execute_cypher("MATCH (n) WHERE ID(n) = '1' RETURN n") |
| 106 | +``` |
0 commit comments