Updated on 2023-03-03 GMT+08:00

Gremlin Query

Scenario

Gremlin is a graph traversal language in the open source graph calculation framework of Apache TinkerPop. You can use Gremlin to query, modify, and traverse graph data as well as filter properties.

Procedure

  1. Log in to the GES graph editor. For details, see Accessing the GES Graph Editor.
  2. In the graph data query area, click the drop-up button to choose to Gremlin query. Enter a query statement and press Enter to run the statement.
    Figure 1 Switching to Gremlin query

Gremlin Statement

Typical query commands are as follows:

  • Querying vertices

    g.V().limit(100): This command is used to query all vertices and return only 100 vertices. You can also use the range (x, y) operator to obtain vertices within the specified quantity.

    g.V().hasLabel('movie'): This command is used to query vertices whose label value is movie.

    g.V('11'): This command is used to query the vertex whose ID is 11.

    1. The g.V () is not recommended because the query result cannot be completely displayed if the vertex scale is large.
    2. To prevent query timeout due to a large data volume, add the limit parameter and set it less than 1,000.
  • Querying edges

    g.E(): This command is used to query all edges. You are not advised using this command without filter criteria or limit to the returned results.

    g.E('55-81-5'): This command queries the edge whose ID is 55-81-5.

    g.E().hasLabel('rate'): This command queries edges whose label value is rate.

    g.V('46').outE('rate'): This command queries the edge whose ID is 46 and all its labels are rate.

  • Querying properties

    g.V().limit(3).valueMap(): This command is used to query all properties of a vertex. (You can specify a parameter to query only one vertex. All properties of the vertex will be displayed in one row.)

    g.V().limit(1).label(): This command is used to query the label of a vertex.

    g.V().limit(10).values('userid'): This command is used to query the name property of a vertex. (You can leave the parameter blank to query all properties. Each property value is displayed in one row, without the key).

  • Adding a vertex

    g.addV('user').property(id,'600').property('age','18-24'): This command adds a vertex whose label is user, ID is 600, and age ranges from 18 to 24.

  • Deleting a vertex

    g.V('600').drop(): This command deletes the vertex whose ID is 600.

  • Adding an edge

    g.addV('user').property(id,'501').property('age','18-24')

    g.addV('movie').property(id,'502').property('title','love')

    g.addE('rate').property('Rating', '4').from(V('501')).to(V('502'))

    The preceding commands add two vertices and an edge. The two vertex IDs are 501 and 502.

  • Deleting an edge

    g.E('501-502-0').drop(): This command deletes the edge whose ID is 501-502-0.

  1. You can press the up and down arrow keys in the text box to view historical query commands.
  2. When you enter a syntax keyword, the system automatically displays historical statements with the same keyword.
    Figure 2 Historical queries
  3. Keywords in the text box are displayed in different colors.
    • Reserved words in gray

      Note: A reserved word is predefined in the syntax system of a programming language. Reserved words vary depending on programming languages.

    • String values in orange
    • Delimiters in red. Regular delimiters including square brackets [], curly brackets {}, parenthesis (), commas (,), and semicolons (;).
    • Variables in green
    Figure 3 Gremlin keywords

Gremlin Syntax Optimization

GES integrates the OLTP function of Gremlin, enhances some features, and optimizes the strategy.

  • Enhanced Text Predicate

    g.V().has('name', Text.textSubString('xx'))

    Predicate

    Description

    textSubString

    Substring

    textCISubString

    Substring that ignores cases

    textFuzzy

    Fuzzy match

    textPrefix

    Prefix query

    textRegex

    Regular expression match

    When you specify a schema, do not name the attributes id, label, property, or properties.

    When you do Gremlin queries with many steps, the results will be converted into a map. Two identical keys are not allowed in a map structure. If multiple identical keys are inserted into a map, the key value will be overwritten or this operation is canceled. If you set an attribute name to id, label, property, or properties, the returned results will be incomplete because in many queries the graph ID is returned together with the attribute ID.

Reference

Table 1 shows how Gremlin in GES differs from open source Gremlin.

Table 1 GES Gremlin differences

Difference

Description

Vertex and Edge IDs

An edge ID consists of the source vertex ID, target vertex ID, and index that distinguishes duplicate edges. The three parts are connected by hyphens (-), for example, sid-tid-index. Edge and vertex IDs must be the string type.

User Supplied IDs

Users can only provide vertex IDs without hyphens (-).

Vertex Property IDs

Both edge and vertex properties do not have IDs. The returned IDs are vertex IDs.

Vertex and Edge Property

Vertex and edge properties are defined by metadata files in GES. Therefore, you cannot add or delete properties, but you can use property() and remove() to modify property values. The value set by property() is determined by the corresponding parameter. remove() converts string properties into empty strings, digital properties into 0, and list properties into empty lists.

Variables

The GES graph structure does not support the variables feature.

Cardinality

GES supports the single and list cardinality. The value type of a vertex property is defined by the metadata file. Therefore, no new property is added when you set the property value.

Transactions

During GES Gremlin implementation, transactions are not explicitly used.

You can use the feature function to view the supported Gremlin features. If false is displayed, GES does not support the feature. If true is displayed, GES supports the feature.

gremlin> graph.features()
==>FEATURES

Currently, the following step commands are not supported:

  • tryNext()
  • explain()
  • tree()