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
- Log in to the GES graph editor. For details, see Accessing the GES Graph Editor.
- In the Gremlin text box, enter the query commands and press Enter to run the commands.
Common query commands are as follows:
- Vertex query
g.V().limit(100): This command is used to query all vertices but the returned vertices are restricted to 100. You can also use the range (x, y) operator to obtain vertices within the specified quantity range.
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.
The g.V () syntax is not recommended because it affects the display effect if the vertex scale is large.
- Edge query
g.E(): This command is used to query all edges. You are not advised to use this command because you need to add filter criteria or limit the returned results when the edge volume is too large.
g.E('55-81-5'): This command is used to query the edge whose ID is 55-81-5.
g.E().hasLabel('rate'): This command is used to query edges whose label value is rate.
g.V('46').outE('rate'): This command is used to query the edge whose ID is 48 and all labels are rate.
- Property query
g.V().limit(3).valueMap(): This command is used to query all properties of a vertex. (This is an optional parameter and only one vertex is queried. All properties of the vertex are 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. (This parameter can be left blank and all properties are queried. Each property is displayed in one row, containing only the value).
- Adding a vertex
- Method 1:
a = graph.addVertex(label,'user',id,'500','age','18-24'): This command is used to add a vertex whose label is user, ID is 500, and age is 18 to 24.
- Method 2:
g.addV('user').property(id,'600').property('age','18-24'): This command is used to add a vertex whose label is user, ID is 500, and age is 18 to 24.
- Method 1:
- Deleting a vertex
g.V('600').drop(): This command is used to delete the vertex whose ID is 600.
- Adding an edge
- Method 1:
a = graph.addVertex(label,'user',id,'501','age','18-24');
b = graph.addVertex(label,'movie',id,'502','title','love');
a.addEdge('rate',b,'Rating','4'): This command is used to add an edge. IDs of the edge's two endpoints are 501 and 502.
- Method 2:
a = g.addV('user').property(id,'501').property('age','18-24');
b = g.addV('movie').property(id,'502').property('title','love');
g.addE('rate').property('Rating', '4').from(a).to(b): This command is used to add an edge. IDs of the edge's two endpoints are 501 and 502.
- Method 1:
- Deleting an edge
g.E('501-502-0').drop(): This command is used to delete the edge whose ID is 501-502-0.
- Vertex query
Related Information
Table 1 shows the differences between the Gremlin in GES and that in the open source community.
| 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. For details about the features, visit the Gremlin official website.
gremlin> graph.features() ==>FEATURES
Currently, the following step commands are not supported:
- tryNext()
- explain()
- tree()
Last Article: Editing Schema
Next Article: Analyzing Graphs Using Algorithms
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.