From dded5bef6330a3474c2db65b239f06309b0feb54 Mon Sep 17 00:00:00 2001 From: Stephen Mallette Date: Fri, 17 Jan 2025 07:32:27 -0500 Subject: [PATCH] Removed references to or added more context around Graph API --- book/Section-Beyond-Basic-Queries.adoc | 144 +++++++++---------- book/Section-Introducing-Gremlin-Server.adoc | 3 +- book/Section-Introduction.adoc | 2 +- book/Section-Janus-Graph.adoc | 64 +++------ 4 files changed, 88 insertions(+), 125 deletions(-) diff --git a/book/Section-Beyond-Basic-Queries.adoc b/book/Section-Beyond-Basic-Queries.adoc index 049c5fd..b4f28f0 100644 --- a/book/Section-Beyond-Basic-Queries.adoc +++ b/book/Section-Beyond-Basic-Queries.adoc @@ -1433,19 +1433,6 @@ depending upon how the graph store handles this request. g.E().drop() ---- -You could also use the 'graph' object to do this. The code below uses the graph -object to retrieve all of the edges and then iterates over them dropping them one by -one. Again for very large graphs this may not be an ideal approach as this requires -reading all of the edge definitions into memory. Note that in this case we call the -'remove' method rather than use 'drop' as we are not using a graph traversal in this -case. - -[source,groovy] ----- -// Remove all the edges from the graph -graph.edges().each{it.remove()} ----- - You could also delete the whole graph, vertices and edges, by deleting all of the vertices! @@ -4921,70 +4908,6 @@ When run, this time the results do indeed include the edges as well as the verti [v[3],e[5162][3-route->49],v[49],e[8454][49-route->71],v[71]] ---- -[[graphvars]] -Using graph variables to associate metadata with a graph -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -A graph variable is a key/value pair that can be associated with the graph itself. -Graph variables are not considered part of the graph that you would process with -Gremlin traversals but are in essence a way to associate metadata with a graph. -You can set and retrieve graph variables using the 'variables' method of the graph -object. Let's assume we wanted to add some metadata that allowed us to record who -the maintainer of the 'air-routes' graph is and when it was last updated. We could -do that as follows. - -[source,groovy] ----- -graph.variables().set('maintainer','Kelvin') -graph.variables().set('updated','July 18th 2017') ----- - -You can use any string that makes sense to you when naming your graph variable keys. -We can use the 'keys' method to retrieve the names of any keys currently in place as -graph variables. - -[source,groovy] ----- -graph.variables().keys() - -updated -maintainer ----- - -The 'asMap' method will return any graph variables that are currently set as a map of -key/value pairs. - -[source,groovy] ----- -graph.variables().asMap() - -updated=July 18th 2017 -maintainer=Kelvin ----- - -We can use the 'get' method to retrieve the value of a particular key. Note that the -value returned is an instance of the 'java.util.Optional' class. - -[source,groovy] ----- -graph.variables().get('updated') - -Optional[July 18th 2017] ----- - -If you want to delete a graph variable you can use the 'remove' method. In this next -example we will delete the 'maintainer' graph variable and re-query the variable map -to prove it has been deleted. - -[source,groovy] ----- -graph.variables().remove('maintainer') -graph.variables().asMap() - -updated=July 18th 2017 - ----- - [[tre]] Turning graphs into trees ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -6132,6 +6055,69 @@ Later on we will take a look at using an index with JanusGraph and look at exter indexing technologies such as Apache Solr and Elasticsearch that do support more complex types of comparison predicates. +[[graphvars]] +Using graph variables to associate metadata with TinkerGraph +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +A graph variable is a key/value pair that can be associated with a TinkerGraph. +Graph variables are not considered part of the graph that you would process with +Gremlin traversals but are in essence a way to associate metadata with a graph. +You can set and retrieve graph variables using the 'variables' method of the graph +object. Let's assume we wanted to add some metadata that allowed us to record who +the maintainer of the 'air-routes' graph is and when it was last updated. We could +do that as follows. + +[source,groovy] +---- +graph.variables().set('maintainer','Kelvin') +graph.variables().set('updated','July 18th 2017') +---- + +You can use any string that makes sense to you when naming your graph variable keys. +We can use the 'keys' method to retrieve the names of any keys currently in place as +graph variables. + +[source,groovy] +---- +graph.variables().keys() + +updated +maintainer +---- + +The 'asMap' method will return any graph variables that are currently set as a map of +key/value pairs. + +[source,groovy] +---- +graph.variables().asMap() + +updated=July 18th 2017 +maintainer=Kelvin +---- + +We can use the 'get' method to retrieve the value of a particular key. Note that the +value returned is an instance of the 'java.util.Optional' class. + +[source,groovy] +---- +graph.variables().get('updated') + +Optional[July 18th 2017] +---- + +If you want to delete a graph variable you can use the 'remove' method. In this next +example we will delete the 'maintainer' graph variable and re-query the variable map +to prove it has been deleted. + +[source,groovy] +---- +graph.variables().remove('maintainer') +graph.variables().asMap() + +updated=July 18th 2017 +---- + [[olapoltp]] OLTP vs OLAP ~~~~~~~~~~~~ @@ -6275,7 +6261,9 @@ degree test did not come up with. The 'pageRank' step used in the prior query is a nice and convenient way for us to quickly generate some rankings. However, it is important to understand how the query would be written if we were to use the Graph Computer more directly and submit a -Vertex Program. The example below sets up a 'PageRankVertexProgram' and runs it. +Vertex Program. You may find yourself taking this approach for your own custom +'VertexProgram' implementations when working with a graph that supports OLAP in an +embedded mode. The example below sets up a 'PageRankVertexProgram' and runs it. Notice that the result we get back includes a new graph with 3624 vertices and no edges. diff --git a/book/Section-Introducing-Gremlin-Server.adoc b/book/Section-Introducing-Gremlin-Server.adoc index be1f9c6..694cc58 100644 --- a/book/Section-Introducing-Gremlin-Server.adoc +++ b/book/Section-Introducing-Gremlin-Server.adoc @@ -811,7 +811,8 @@ def globals = [:] globals << [hook : [ onStartUp: { ctx -> ctx.logger.info("Loading 'air-routes' graph data.") - graph.io(graphml()).readGraph('data/air-routes.graphml') + g = traversal().with(graph) + g.io('data/air-routes.graphml').read().iterate() } ] as LifeCycleHook] diff --git a/book/Section-Introduction.adoc b/book/Section-Introduction.adoc index 8a789ea..be30bf1 100644 --- a/book/Section-Introduction.adoc +++ b/book/Section-Introduction.adoc @@ -407,7 +407,7 @@ NOTE: Full details of all the new features added in the TinkerPop 3.7.x releases be found at the following link: https://github.com/apache/tinkerpop/blob/master/CHANGELOG.asciidoc#tinkerpop-370-gremfir-master-of-the-pan-flute -[tp40intro]] +[[tp40intro]] TinkerPop 4.0 ^^^^^^^^^^^^^ diff --git a/book/Section-Janus-Graph.adoc b/book/Section-Janus-Graph.adoc index dfac098..bd327f2 100644 --- a/book/Section-Janus-Graph.adoc +++ b/book/Section-Janus-Graph.adoc @@ -266,39 +266,15 @@ https://docs.janusgraph.org/basics/transactions/ In many cases, when using JanusGraph, you do not have to explicitly open a new transaction. Instead, it will be opened for you as needed. Take a look at the example -below. A transaction is opened when 'addVertex' is called and remains open until +below. A transaction is opened when 'addV' is called and remains open until 'commit' is called. Note also that in order to access the JanusGraph transaction -capabilities, we use the 'tx' method associated with our 'graph' instance. The -examples below assume you have the Gremlin Console connected to a JanusGraph -instance. The 'inmemory' JanusGraph we created earlier will work fine for these -examples as transactions are supported even with 'inmemory' JanusGraph instances. -Note that we have not shown the warning message that JanusGraph will display -reminding us that we have not created an index for our new property. We will explore -how to create an index in the "<>" section. - -[source,groovy] ----- -// Start a new transaction -xyz = graph.addVertex() - -v[4344] - -// Add a property -xyz.property('name', 'XYZ') - -// Commit the transaction -graph.tx().commit() - -// Check to make sure our new vertex was created -g.V().has('name','XYZ') - -v[4344] ----- - -The example above used the 'graph' object to add a vertex. As discussed earlier in -this book, the TinkerPop documentation recommends against this. Instead it recommends -adding vertices as part of a traversal as shown below. Note that the 'graph' object -is still used to 'commit' the transaction. +capabilities, we use the 'tx' method associated with our 'g' instance. The examples +below assume you have the Gremlin Console connected to a JanusGraph instance. The +'inmemory' JanusGraph we created earlier will work fine for these examples as +transactions are supported even with 'inmemory' JanusGraph instances. Note that we +have not shown the warning message that JanusGraph will display reminding us that we +have not created an index for our new property. We will explore how to create an +index in the "<>" section. [source,groovy] ---- @@ -340,10 +316,10 @@ otherwise configuring a graph. The JanusGraph management API ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -JanusGraph includes a management API that is made available via the ManagementSystem -class. You can use the management API to perform various important functions that -include querying metadata about the graph, defining the edge, vertex and property -schema types and creating and updating the index. +JanusGraph includes a management API that is made available via the +'ManagementSystem' class. You can use the management API to perform various important +functions that include querying metadata about the graph, defining the edge, vertex +and property schema types and creating and updating the index. You can create an instance of the ManagementSystem object using the 'openManagement' method call as shown below. @@ -558,6 +534,7 @@ g.V(n).valueMap() Now let's try adding a second value of 2 and see what happens. As you can see, our second 2 was not added to our set as there was already a 2 present. + [source,groovy] ---- g.V(n).property(set,'numbers',2) @@ -582,7 +559,7 @@ Finally we can commit our graph transaction as we are all done creating properti [source,groovy] ---- -graph.tx().commit() +g.tx().commit() ---- [[janusschema]] @@ -672,7 +649,6 @@ mgmt.makeVertexLabel('continent').make() mgmt.commit() ---- - Defining vertex property keys ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -773,20 +749,18 @@ Relation Index (VCI) | Type | Direction | Sort Key | Orde --------------------------------------------------------------------------------------------------- ---- - - [[janusload]] Loading air-routes into a JanusGraph instance ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Now that we know how to create a schema for the 'air-routes' graph -we can use the same basic steps to load it into a JanusGraph -instance that we used with TinkerGraph. Note that after loading the graph from the -XML file we then call 'commit' to finalize the transaction. +Now that we know how to create a schema for the 'air-routes' graph we can use the +same basic steps to load it into a JanusGraph instance that we used with TinkerGraph. +Note that after loading the graph from the XML file we then call 'commit' to finalize +the transaction. [source,groovy] ---- -graph.io(graphml()).readGraph('air-routes.graphml') +g.io('air-routes.graphml').read().iterate() graph.tx().commit() ----