What's Gremlin?
Gremlin is used as the graph traversal language on Apache TinkerPop, which is a graph computing framework.
Basic Concepts
-
Graph:
Collection of Vertex and Edge.
-
Element:
Collection of Property.
-
Vertex:
Inherits from Element. Vertex is generally used to store the Property of entities.
-
Edge:
Inherits from Element. Edge is generally used to store the Property of relationship.
-
Property
A k-v pair, where the key is just like the column name in relational database, and the value is stored data in the Edge or Vertex.
Note that the key must be a string.
-
Label
The class for Vertex or Edge. Vertexes and Edges having the same label means they have the same properties. Label can be regarded as the table name in a workplace in relational database.
Usage of Gremlin
Graph, Edge and Vertex
All query must start with a graph.
|
|
Property
|
|
ID
Get vertex or edge by id, or get id
|
|
Label
Get vertex or edge by label
|
|
Filter
In Gremlin, we can use hasXXX
to filter specific edges or vertexes. Gremlin provides the following commands:
-
has(key, value)
: filter by property's key and value -
has(label, key, value)
: filter by label, and property's key and value -
has(key, expr)
: filter by label and an expr. For example, usingg.V().has('age', gt(20))
we can get vertexes which haveage
property and whose value ofage
is larger than 20. -
hasLabel(label1, label2, ...)
: filter by label. Elements with label in any one of the label list will pass the filter. -
hasId(id1, id2, ...)
: filter by id, similar withhasLabel
-
hasKey(key1, key2, ...)
andhas(key)
: filter by key