Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 3033e47

Browse files
tb06904cn337131p29876
authored
Update Gremlin API docs (#533)
* update docs to include api page * add timeout option docs * Apply suggestions from code review Co-authored-by: cn337131 <[email protected]> Co-authored-by: p29876 <[email protected]> * add gafferpy example --------- Co-authored-by: cn337131 <[email protected]> Co-authored-by: p29876 <[email protected]>
1 parent 2e95fc9 commit 3033e47

File tree

6 files changed

+121
-55
lines changed

6 files changed

+121
-55
lines changed

docs/administration-guide/gaffer-deployment/gremlin.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ A full breakdown of the available properties is as follows:
8585
| `gaffer.schemas` | The path to the directory containing the graph schema files. | No |
8686
| `gaffer.userId` | The default user ID for the Tinkerpop graph. | No (User is always set via the [`UserFactory`](../security/user-control.md).) |
8787
| `gaffer.dataAuths` | The default data auths for the user to specify what operations can be performed | No |
88+
| `gaffer.rest.timeout` | The timeout for gremlin queries submitted to the REST API in ms. Default is 2 mins if not specified. | Yes |
8889
| `gaffer.operation.options` | Default `Operation` options in the form `key:value` (this can be overridden per query see [here](../../user-guide/query/gremlin/custom-features.md)) | Yes |
8990
| `gaffer.elements.getalllimit` | The default limit for unseeded queries e.g. `g.V()`. | Yes |
9091
| `gaffer.elements.hasstepfilterstage` | The default stage to apply any `has()` steps e.g. `PRE_AGGREGATION` | Yes |
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
```

docs/user-guide/query/gremlin/custom-features.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,6 @@ data in that reaches the last step then that step will be missing from the expla
229229
in the Graph schema.
230230
- All submitted Cypher explains will be translated to Gremlin first and have a `.toList()`
231231
appended to the translation so it is actually executed.
232+
- An explanation of a Gremlin `project()` step will not include all the Operations called.
233+
As a Gremlin `project` is essentially a for-each loop the explain will only include the
234+
last iteration of the loop.

docs/user-guide/query/gremlin/gremlin-limits.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ Current known limitations or bugs:
2222
- Edge IDs in GafferPop are not the same as in standard Gremlin. Instead of `g.E(11)`
2323
edge IDs take the format `g.E("[source, dest]")` or `g.E("[source, label, dest]")`.
2424
- The entity group `id` is reserved for an empty group containing only the
25-
vertex ID, this is currently used as a workaround for other limitations.
25+
vertex ID, this is currently used as a workaround for other limitations. One such
26+
use is for holding 'orphaned' vertexes, these are vertexes on an edge that do not
27+
have a Gaffer entity associated with them.
2628
- Chaining `hasLabel()` calls together like `hasLabel("label1").hasLabel("label2")`
2729
will act like an OR rather than an AND in standard Gremlin. This means you
2830
may get results back when you realistically shouldn't.
2931
- Input seeds to Gaffer operations are deduplicated.
30-
Therefore, the results of a query against a GafferPop graph may be different than a standard Gremlin graph.
32+
Therefore, the results of a query against a GafferPop graph may be different than a standard Gremlin graph.
3133
For example, for the Tinkerpop Modern graph:
32-
```
34+
```text
3335
(Gremlin) g.V().out() = [v2, v3, v3, v3, v4, v5]
3436
(GafferPop) g.V().out() = [v2, v3, v4, v5]
3537
```

docs/user-guide/query/gremlin/gremlin.md

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
1-
# Gremlin in Gaffer
2-
3-
!!! warning
4-
GafferPop is still under development and has some [limitations](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-
[Gremlin](https://tinkerpop.apache.org/gremlin.html) is a query language for
9-
traversing graphs. It is a core component of the Apache Tinkerpop library and
10-
allows users to easily express more complex graph queries.
11-
12-
GafferPop is a lightweight Gaffer implementation of the [TinkerPop framework](https://tinkerpop.apache.org/),
13-
where TinkerPop methods are delegated to Gaffer graph operations.
14-
15-
The addition of Gremlin as query language in Gaffer allows users to represent
16-
complex graph queries in a simpler language akin to other querying languages
17-
used in traditional and NoSQL databases. It also has wide support for various
18-
languages so for example, you can write queries in Python via the [`gremlinpython` library](https://pypi.org/project/gremlinpython/)
19-
20-
!!! tip
21-
In-depth tutorials on Gremlin as a query language and its associated libraries
22-
can be found in the [Apache Tinkerpop Gremlin docs](https://tinkerpop.apache.org/gremlin.html).
23-
24-
## Using Gremlin Queries in Gaffer
1+
# Using Gremlin Queries in Gaffer
252

263
Gremlin was added to Gaffer in version 2.1 as a new graph query language and since
274
version 2.3 it has been added as standard into the Gaffer REST API. A full tutorial
@@ -31,35 +8,11 @@ on the configuration of Gremlin in Gaffer is provided in the
318
This guide will use the [Python API for Gremlin](https://pypi.org/project/gremlinpython/)
329
to demonstrate some basic capabilities and how they compare to standard Gaffer syntax.
3310

34-
To start querying in Gremlin we first need a reference to what is known as the
35-
Graph Traversal. To obtain this we need to connect to the Gremlin websocket provided
36-
by the Gaffer REST API (if you have used [`gafferpy`](../../apis/python-api.md)
37-
before this will be quite similar). We can do this by first importing the required
38-
libraries like so (many of these will be needed later for queries):
39-
40-
```python
41-
from gremlin_python.process.anonymous_traversal import traversal
42-
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
43-
from gremlin_python.driver.serializer import GraphSONSerializersV3d0
44-
from gremlin_python.process.graph_traversal import __
45-
```
46-
47-
We can then establish a connection to the Gremlin server and save a reference to
48-
this (typically called `g`):
49-
50-
```python
51-
# Setup a connection with the REST API running on localhost
52-
g = traversal().with_remote(DriverRemoteConnection('ws://localhost:8080/gremlin', 'g', message_serializer=GraphSONSerializersV3d0()))
53-
```
54-
55-
Now that we have the traversal reference this can be used to spawn graph traversals
56-
and get results back.
57-
58-
!!! note
59-
Its important to use the GraphSON v3 serialiser if connecting to the Gaffer
60-
REST API.
11+
!!! tip
12+
For information on how to set up a Gremlin connection please see the
13+
[API guide](../../apis/gremlin-api.md).
6114

62-
### Basic Gremlin Queries
15+
## Basic Gremlin Queries
6316

6417
Gremlin queries (similar to Gaffer queries) usually require a starting set of
6518
entities to query from. Commonly Gremlin queries will be left without any IDs in

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ nav:
9090
- 'Spring REST': 'user-guide/apis/rest-api.md'
9191
- 'Python (gafferpy)': 'user-guide/apis/python-api.md'
9292
- 'Java': 'user-guide/apis/java-api.md'
93+
- 'Gremlin (GafferPop)': 'user-guide/apis/gremlin-api.md'
9394
- Querying:
9495
- Gaffer Query Syntax:
9596
- 'Operations': 'user-guide/query/gaffer-syntax/operations.md'

0 commit comments

Comments
 (0)