Mutating Traversals

DSE Version: 6.0


 The mutating traversal adds or removes vertices, edges, or properties to the graph. In this unit, you will learn about mutating traversals.


Hey everyone. I am Denise Gosnell and this is the mutating traversal.

Now the mutating traversal adds or removes vertices, edges, or properties to the graph. There is a separate graph API to do this directly, and that API is more efficient with DSE Graph. So, if you have a lot of mutating to do, mutating traversals might not be what you want. But there are some kinds of mutations that you need to do in terms of a traversal.

We will show you what those are, and in that case, the mutating traversal is exactly what you need.

We have four new steps to get these mutations done.

There is addV that simple adds a vertex. At the time of adding a vertex, we can give it a label.

We have addE that adds an edge. Now, to add an edge, we also need to tell the vertices which it is connecting. So, this will create a new labeled edge, but we will also need to use a modulator to say where the edge is coming from and where it is going to.  

There is also the property step that let’s us add a new property to a vertex or an edge.

Finally, there is drop, to remove a vertex, an edge, or a property.

Dropping a property just removes the key and value from the vertex or edge. When you drop an edge, it just removes that object from the graph. But, when you drop a vertex, it will also drop all of the incident edges of that vertex. So, that drop might do a little more work than you expect. But, When we think about property graph structure, that extra work when dropping a vertex is necessary to ensure we have the correct graph structure after a vertex has been removed.

As we mentioned, adding an edge requires that we come from a vertex and go to a vertex. The from and to modulators do that. When we use the addE step, we want to use that on a vertex or vertices in a traversal. We have to then specify where that edge is going to come from or where that edge will go to.  Of course, however we use from or to establishes the direction of the edge. We can come from or two a labeled step where that step is one or more vertices. And, we can also go from or to a new traversal so that we can explicitly specify that traversal that generates the vertices that the edge should go to, or come from.

The mutating traversals are pretty straight forward, but examples always help.

Let’s remind ourselves of the killrVideo schema.  Persons act, direct….

So, let’s see what we can do to mutate our graph according to this schema.

Let’s say that we want to go to all of the Tom Hank’s movies in our schema, compute the average ratings, and store those ratings in the movie vertices themselves.

Let’s look at the code.

We will first find the tom hanks vertex. That is g.V().has(“person”, name, tom hanks). From there, we will traverse the incoming actor edges. That is going to take us to all of the movie vertices that have had tom hanks as an actor. From there, we are going to mutate those vertices by creating a new property. That is the property mutating step you see on the second line. We are going to create a property called average. The value of this property is going to be equal to the 3rd line. Here, we have an annonymous traversal. And, we need a reality check as to where we are in the stream at this point. At this point, we are at the movie vertices. Next, we need to traverse the incoming edges with the rated label and calculate the mean of the rating value of those edges. That is the traversal we see on the 3rd line. It computes the average rating for the tom hanks movies.

At this point, the mutation is done. We have added the “avg” property to the tom hanks movie vertices. The last line is just providing the output we want to see.   It is just getting the avg property and the title property and emitting those in the list that you see.

Cast away was rated pretty highly -- about a 7.1 Then Philadelphia and Polar express.

So, how about we add a new genre into the graph? A genre for biographies.

This is a pretty simple mutating traversal.

We are going to add a vertex called genre, and we are going to give it a couple of new properties.  We are going to give it a new id and then a name which is piography. Then, the value map prints that out to see the vertex and all of its properties as a map.

This is just a new way to add a vertex to this graph. It is an isolate vertex, but it is in our graph.

Let’s see what we can do with it.

Now, “bridge of spies” is arguably a bio pic of attorney james donathan. So, let’s see if we can add an edge from this new biograpies genre vertex that we have created. Let’s walk through the code.

We begin by adding the movie itself. Bridge of spies is not in this sample data set. So, we add a new vertex with the movie label and fill it up with properties. We give it a movie id, the title bridge of spies, its year, duration, country of origin, and there we have it. Then, we are going to label this vertex with the as step, and give it the label “new” to refer later on in the traversal.

Then, we are going to add an edge. This edge will have the actor label. There is more to say about this film, but we only want to add the “actor” edge over to the person vertex with the name tom hanks. So, we addE(“actor”) to the tom hanks person vertex.

Then, we have the line that says select new. This effectively will go back into the traversal to the vertex that we labeled “new”. So, going forward after the select new line, the stream will see that vertex. We will add another edge from that vertex to connect the “bridge of spies” movie to the newly created genre vertex: “biography”. That is the last two lines of this mutation.

So we created a movie, gave it properties, associated the movie with an actor, and associated it with a genre.

In the previous example, we added two examples. But we added them explicitly.

This time, we are going to use a traversal to add edges to a collection of vertices.

We begin by getting the tom hanks person vertex and label it tom. On the second line, we traverse the incoming actor edge. This gets us back to all of the movies in which tom hanks has been an actor. From there, we traverse the outgoing actor edge to get all of the other actors who have acted in movies with Tom Hanks. So, this is potentially now a large set of people vertices at the end of the second line. Then, we will put a constraint saying that they can’t be equal to tom.

So finding tom, going to tom’s movies, and going back to all of the actor’s of tom’s movies will of course find tom more than once.  We don’t want him in this set and we don’t want any duplicates, like in the case of meg ryan who appeared in sleepless in seattle and you’ve got mail. The use of dedup will remove the multiple versions of the same actor from the set. Now, in this 4th line, we are going to actually mutate the graph. Coming into it, we have a collection of vertices and we are going to add an edge from all of these vertices with the label “knows”. That edge will be coming from the”tom” vertex, which we labeled in the first line. We have now created many edges from all of the actors who have been in movies with tom hanks and we are asserting that they know tom.

The purpose of the 5th line is just for display. At this point in the stream, we are on edges. But, we need to traverse to the people and then look at there names. And we can see that meg ryan appears in the output.

In this next example, we are going to play with adding edges and removing them. what we will do is that we are going to turn all of the actor edges into “acted in” edges. In our schema, we have movies that  have actors that are people. People don’t have any notion of having acted in any movie. But, for this case, for the tom hanks movies, we will remove the actor edges and add in “acted in” edges in the reverse direction.

We start again on the first line finding the tom hanks vertex and labeling it as tom. We will be able to use it later on in the traversal.

Then, we go back along the incoming actor edges and we label those vertices as “movie”. That will be all of the movies that tom hanks acted in. We refer to those collectively as “movie”/ From those movies, we are goign to add an edge called “acted in” that comes from tom. So, the directionality here is opposite the normal direction of our schema. The tom vertex has an edge that points to his films and it says that tom acted in those films.

At the end of the 3rd line, what we have in the traversal are edges. The mutation is complete, but it is going to pass those edges on to the next step. From those edges, we are going to walk to the outgoing vertex, which will happen to be the tom person. Then, we will select the incoming “actor” edges and drop them. So, the normal “actor” edges we have in the schema. We have reversed them and replaced them with “Acted in” edges. Now, let’s take a look at the results.

In the 2nd traversal, we get the tom hanks vertex and we want to count how many incoming actor edges there are. Sure enough, there are zero because we removed them. In the 3rd traversal, we will go to the tom hanks vertex, and we will count the outgoing acted in edges and we see 10, which reflects the data that we have in our sample set.

So we have removed the actor edges, and replaced them with acted in edges , but in the reverse direction.

In this next example, let’s combine a mutating traversal with a declarative traversal. we covered declarative traversals elsewhere, but let’s still take a moment to see what is going on.

We begin again by finding the tom hanks person vertex. Then, the next two lines are our declarative traversal, that is the match step. The match step contains two patterns. Both patterns start off with anonymous traversals. Also, both patterns contain variables: tom, m and x. In these patterns, we are simply declaring the relationships that these things have but we are not specifically tell.

So, tom, whatever it is, has an incoming actor edge to a thing called m. in the second pattern, m has an outgoing actor edge to a thing called x. Now, those assertions will match a certain part of our graph and we will later find out what they are.

Now, on our 4th line, we are going to say that the x variable cannot be equal to the tom variable. And that is an additional constraint, where x is not equal to tom. If you think about it. If you go from tom to the movies he has acted in, we are going to say that we do not care about the times that tom has acted in the same movies with tom. We only want to know when tom acted in movies with other people. So we say that x cannot be equal to tom.

Then, we remove the duplicate actors. Then, we get to our mutating traversal. At the end of dedup x, we have a collection of vertices. From those vertices we will add edges with the “knows” label. Those edges will come from the tom vertex, and go to the vertices in “x”. And those edges will all go to the set of people identified from our declarative traversal earlier on.

Lastly, go back to the incoming vertices. Pick out the names, and see which actors we have.

This is the same example as before, it just does it in a slightly different way by combining the declarative traversal with the mutating traversal.

No write up.
No Exercises.
No FAQs.
No resources.
Comments are closed.