Difference between revisions of "Gremlin"
(→Steps) |
|||
Line 45: | Line 45: | ||
http://tinkerpop.apache.org/docs/3.4.1/images/step-types.png | http://tinkerpop.apache.org/docs/3.4.1/images/step-types.png | ||
== General Steps == | == General Steps == | ||
+ | === filter Step === | ||
+ | Continues processing based on the given filter condition. | ||
+ | ==== JUnit Test === | ||
+ | <source lang='java'> | ||
+ | @Test | ||
+ | public void testFilter() { | ||
+ | assertEquals(3,g().V().filter(out()).count().next().longValue()); | ||
+ | assertEquals(4,g().V().filter(in()).count().next().longValue()); | ||
+ | } | ||
+ | </source> | ||
= What links here = | = What links here = |
Revision as of 09:30, 24 April 2019
Gremlin is the graph traversal language of Apache TinkerPop. Gremlin is a functional, data-flow language that enables users to succinctly express complex traversals on (or queries of) their application's property graph. Every Gremlin traversal is composed of a sequence of (potentially nested) steps. A step performs an atomic operation on the data stream. Every step is either a map-step (transforming the objects in the stream), a filter-step (removing objects from the stream), or a sideEffect-step (computing statistics about the stream). The Gremlin step library extends on these 3-fundamental operations to provide users a rich collection of steps that they can compose in order to ask any conceivable question they may have of their data for Gremlin is Turing Complete.
GraphTraversal
One of the core concepts of tinkerpop/gremlin is the GraphTraversal It's interface has a generic definition as:
public interface GraphTraversal<S,E> extends Traversal<S,E>
and at https://markorodriguez.com/ the Author Marko Rodriguez explains the ideas behind using an generic approach vor handling Graphs. The Java implementation is available on github.
S is a generic Start class, and E is a generic End class as explained in the Apache Tinkerpop documentation.
A graph Traversal is the starting point for working with a graph. The convention is to name it
g
or
g()
The Modern example
The "modern" graph is shipped with gremlin as a standard example.
The graph has 6 edges and 6 vertices.
JUnit Testcase
@Test
public void testTraversal() {
Graph graph = TinkerFactory.createModern();
GraphTraversalSource g = graph.traversal();
assertEquals(6,g.E().count().next().longValue());
assertEquals(6,g.V().count().next().longValue());
}
E() gives you access to the edges of a graph traversal. V() gives you access to the vertices of a graph traversal. In the above example we simply cound the edges and vertices and check our assumption that there are 6 edges and 6 vertices in the modern example graph.
Steps
As explained in Gremlin_Basics: "The Gremlin graph traversal language defines approximately 30 steps which can be understood as the instruction set of the Gremlin traversal machine. These steps are useful in practice, with typically only 10 or so of them being applied in the majority of cases. Each of the provided steps can be understood as being a specification of one of the 5 general types enumerated below".
General Steps
filter Step
Continues processing based on the given filter condition.
= JUnit Test
@Test
public void testFilter() {
assertEquals(3,g().V().filter(out()).count().next().longValue());
assertEquals(4,g().V().filter(in()).count().next().longValue());
}
What links here
Links
- That Conf - Graph Database - What, Why, How - Presentation by Andrew Glassmann
- Practical Gremlin: An Apache TinkerPop Tutorial by Kelvin Lawrence see also https://github.com/krlawrence/graph
- https://github.com/bechbd/gremlin-ide
- Tinkerpop
Stackoverflow Questions
Recipes
Practical Gremlin: An Apache TinkerPop Tutorial by Kelvin Lawrence