Skip to content

Commit 365a8fe

Browse files
committed
Add coverage of dynamic labels for addV and addE #22
1 parent e86d5d6 commit 365a8fe

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

ChangeHistory.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Major new features in this release
1818
- Expanded the `vertex degree` section.
1919
- Added a section to introduce the `dedup` step in Section 3.
2020
- Expanded the "Basic statistical and numerical operations" section.
21+
- Added coverage of dynamically adding labels from `addV` and `addE`
2122
- Improved existing text and examples in section 5.
2223
- Added more examples to section 5
2324
- Improved coverage of `indexed` and `withIndex`

book/Gremlin-Graph-Guide.adoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5421,6 +5421,9 @@ have not attempted to give an example of every single operator being used but th
54215421
examples provided should provide all of the basic building blocks you will need to
54225422
incorporate mathematical operators into your own Gremlin queries.
54235423

5424+
TIP: These features require that the graph database system you are using supports a
5425+
TinkerPop version of 3.3.1 or higher.
5426+
54245427
[[arithmentic]]
54255428
Performing simple arithmetic
54265429
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -6682,6 +6685,67 @@ g.V().has('code','XYZ').as('a').V().has('code','DFW').addE('route').to('a')
66826685
You will see a bigger example that uses 'as' to name steps in the "<<testgraph>>"
66836686
section that is coming up soon.
66846687

6688+
[[addlabeldynamic]]
6689+
Using a traversal to determine a new label name
6690+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6691+
6692+
In TinkerPop 3.3.1 a new capability was added to the 'addV' and 'addE' steps. This
6693+
new capability allows us to use a traversal to determine what the label used by a new
6694+
vertex or edge should be. Take a look at the query below. We have seen this type of
6695+
query used earlier in the book. It simply tells us what label the vertex representing
6696+
the Austin (AUS) airport has.
6697+
6698+
[source,groovy]
6699+
----
6700+
g.V().has('code','AUS').label()
6701+
6702+
airport
6703+
----
6704+
6705+
What the new capability added in TinkerPop 3.3.1 allows us to do is include the
6706+
traversal above inside of an 'addV' step as shown below. The first string result
6707+
returned by the provided traversal will be used as the label name.
6708+
6709+
[source,groovy]
6710+
----
6711+
g.addV(V().has('code','AUS').label()).property('code','XYZ')
6712+
6713+
v[53768]
6714+
----
6715+
6716+
We can inspect the new vertex using 'valueMap' to make sure that our label was
6717+
correctly assigned.
6718+
6719+
[source,groovy]
6720+
----
6721+
g.V(53768).valueMap(true)
6722+
6723+
[id:53768,code:[XYZ],label:airport]
6724+
----
6725+
6726+
TIP: These features require that the graph database system you are using to supports
6727+
a TinkerPop version of 3.3.1 or higher.
6728+
6729+
We can now do something similar to dynamically work out what the label should be for
6730+
an edge between our new airport and Austin.
6731+
6732+
[source,groovy]
6733+
----
6734+
g.V(53768).addE(V().has('code','AUS').outE().limit(1).label()).
6735+
to(V().has('code','AUS'))
6736+
6737+
e[53770][53768-route->3]
6738+
----
6739+
6740+
Again, we can use a 'valueMap' step to make sure our new edge label looks OK.
6741+
6742+
[source,groovy]
6743+
----
6744+
g.E(53770).valueMap(true)
6745+
6746+
[id:53770,label:route]
6747+
----
6748+
66856749
[[proptraversal]]
66866750
Using a traversal to seed a property with a list
66876751
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)