@@ -40,6 +40,7 @@ data in a single table called 'airports'. In a very simple case (the air routes
4040graph actually stores a lot more data than this about each airport) we could setup
4141our airports table so that it had entries for each airport as follows.
4242
43+ [source,text]
4344----
4445ID CODE ICAO CITY COUNTRY
4546--- ---- ---- --------------- ----------
@@ -51,13 +52,14 @@ ID CODE ICAO CITY COUNTRY
515251 CDG LFPG Paris FR
525352 FRA EDDF Frankfurt DE
535455 SYD YSSY Sydney AU
55+ ----
5456
55- We could then use a SQL query to count the
56- distribution of airports in each country as follows.
57+ We could then use a SQL query to count the distribution of airports in each country
58+ as follows.
5759
5860[source,sql]
5961----
60- select country,count(country) from airports group by country;
62+ SELECT country,count(country) FROM airports GROUP BY country;
6163----
6264
6365We can do this in Gremlin using the 'air-routes' graph with a query like the one
@@ -75,7 +77,7 @@ is in no way required to learn Gremlin.
7577One thing you will not find when working with a graph using Gremlin is the concept of
7678a SQL 'join'. Graph databases by their very nature avoid the need to join things
7779together (as things that need to be connected already are connected) and this is a
78- core reason why, for many use cases, Graph databases are a very good choice and can
80+ core reason why, for many use cases, graph databases are a very good choice and can
7981be more performant than relational databases.
8082
8183Graph databases are usually a good choice for storing and modelling networks. The
@@ -91,6 +93,7 @@ destination airport and the distance between them in miles (SRC,DEST and DIST).
9193would contain entries that looked like this (the real table would of course have
9294thousands of rows but this gives a good idea of what the table would look like).
9395
96+ [source,text]
9497----
9598SRC DEST DIST
9699--- ---- ----
@@ -109,18 +112,19 @@ LHR BOM 4479
109112LHR FRA 406
110113YYZ FRA 3938
111114YYZ LHR 3544
115+ ----
112116
113117If we wanted to write a SQL query to calculate the ways of travelling from Austin
114118(AUS) to Agra (AGR) with two stops, we would end up writing a query that looked
115119something like this:
116120
117121[source,sql]
118122----
119- select a1.code,r1.dest,r2.dest,r3.dest from airports a1
120- join routes r1 on a1.code=r1.src
121- join routes r2 on r1.dest=r2.src
122- join routes r3 on r2.dest=r3.src
123- where a1.code='AUS' and r3.dest='AGR';
123+ SELECT a1.code,r1.dest,r2.dest,r3.dest FROM airports a1
124+ JOIN routes r1 ON a1.code=r1.src
125+ JOIN routes r2 ON r1.dest=r2.src
126+ JOIN routes r3 ON r2.dest=r3.src
127+ WHERE a1.code='AUS' AND r3.dest='AGR';
124128----
125129
126130Using our 'air-routes' graph database the query can be expressed quite simply as
@@ -153,7 +157,7 @@ take to get there.
153157
154158Again, don't worry if some of the Gremlin steps shown here are confusing, we will
155159cover them all in detail a bit later. The key point to take away from this discussion
156- of SQL and Gremlin is that for data that is very connected, Graph databases provide a
160+ of SQL and Gremlin is that for data that is very connected, graph databases provide a
157161very good way to store that data and Gremlin provides a nice and fairly intuitive way
158162to traverse that data efficiently.
159163
@@ -177,14 +181,13 @@ doing. We are traversing the graph from a starting point to an ending point.
177181Traversals consist of one or more 'steps' (essentially methods) that are chained
178182together.
179183
180- As we start to look at some simple traversals here are a few 'steps' that you will
184+ As we start to look at some simple traversals, here are a few 'steps' that you will
181185see used a lot. Firstly, you will notice that almost all traversals start with either
182186a 'g.V()' or a 'g.E()'. Sometimes there will be parameters specified along with those
183187steps but we will get into that a little later. You may remember from when we looked
184188at how to load the 'air-routes' graph in Section 2 we used the following instruction
185189to create a graph traversal source object for our loaded 'graph'.
186190
187-
188191[source,groovy]
189192----
190193g = traversal().with(graph)
@@ -259,10 +262,10 @@ v[8]
259262So, what we actually got back from these queries was a TinkerPop 'Vertex' data
260263structure. Later in this book we will look at ways to store that value into a
261264variable for additional processing. Remember that even though we are working with a
262- Groovy environment while inside the Gremlin Console , everything we are working with
263- here, at its core, is Java code. So we can use the 'getClass' method from Java to
264- introspect the object. Note the call to 'next' which turns the result of the
265- traversal into an object we can work with further.
265+ Groovy environment while inside the console , everything we are working with here, at
266+ its core, is Java code. So we can use the 'getClass' method from Java to introspect
267+ the object. Note the call to 'next' which turns the result of the traversal into an
268+ object we can work with further.
266269
267270[source,groovy]
268271----
@@ -597,7 +600,7 @@ value 'AUS'). Having found that vertex we then go 'out' from there. This will fi
597600all of the vertices connected to Austin by an outgoing edge. Having found those
598601airports we then ask for the values of their 'code' properties using the 'values'
599602step. Finally the 'fold' step puts all of the results into a list for us. This just
600- makes it easier for us to inspect the results in the console .
603+ makes it easier for us to inspect the results in Gremlin Console .
601604
602605[source,groovy]
603606----
@@ -1941,7 +1944,7 @@ the Austin airport vertex.
19411944g.V().has('code','AUS').valueMap().unfold()
19421945----
19431946
1944- If you are using the Gremlin console , the output from running the previous command
1947+ If you are using the Gremlin Console , the output from running the previous command
19451948should look something like this. The unfold step at the end of the query is used
19461949to make the results easier to read.
19471950
@@ -2040,10 +2043,10 @@ runways=[2]
20402043lat=[30.1944999694824]
20412044----
20422045
2043- If you are reading the output of queries that use 'valueMap' on the Gremlin console,
2044- it is sometimes easier to read the output if you add an 'unfold' step to the end of
2045- the query as follows. The 'unfold' step will unbundle a collection for us. You will
2046- see it used in many parts of this book.
2046+ If you are reading the output of queries that use 'valueMap' from the console, it is
2047+ sometimes easier to read the output if you add an 'unfold' step to the end of the
2048+ query as follows. The 'unfold' step will unbundle a collection for us. You will see
2049+ it used in many parts of this book.
20472050
20482051[source,groovy]
20492052----
0 commit comments