Gremlin Recipes: 7 – Variables Handling
In this tutorial, we will explore some advanced Gremlin traversal techniques such as in-stream variables, side effect steps, and labeled steps. We will also discuss the use of subgraph() and select() steps. These techniques are useful for performing complex graph queries and data manipulations. 1. In-Stream Variables: - Create intermediate variables during a traversal to reuse them later in subsequent traversal steps. - Use aggregate() or as(): - aggregate("var_name") saves the result of a previous step into an in-stream variable "var_name". - as("var_name") labels the current step with "var_name" and can be recalled later using select(). 2. Side Effect Steps: - Use sideEffect() to perform computations or data manipulations without affecting the path history. - The result of a side effect step is not included in the output of the traversal, but it can be reused later in subsequent steps. 3. Labeled Steps: - Use as("var_name") to label a step with "var_name" and recall it later using select(). - The result of a labeled step is included in the output of the traversal, but it can be reused later in subsequent steps. 4. Subgraph() Step: - Use subgraph("subgraph_id") to save a partial edge-induced subgraph. - The subgraph() step can only be placed after an edge step, not after a vertex step. 5. Select() Step: - Use select("var_name1", "var_name2", ...) to pick side effect variables or saved labels in the path history. - Any reducer step (sum(), mean(), ...) will destroy path history and thus you lose all previously labeled steps. 6. Cap() Step: - Use cap("var_name") to save a vertex or edge into an in-stream variable "var_name". - The result of a cap() step is not included in the output of the traversal, but it can be reused later in subsequent steps. By mastering these advanced Gremlin traversal techniques, you will be able to perform complex graph queries and data manipulations more efficiently and effectively.
Company
DataStax
Date published
Sept. 25, 2017
Author(s)
Duy Hai Doan
Word count
2618
Language
English
Hacker News points
None found.