Inserting Data Using Graph

DSE Version: 6.0


In this unit, we’ll be talking about all the different ways you can load and insert data into DataStax Enterprise Graph.


In this section, we’ll be talking about all the different ways you can load and insert data into DataStax Enterprise Graph.

To start, we’ll be exploring the graph API.

The Graph API is just one of five different ways you can create and load data into existing graphs.

We will cover the graph API in this presentation. This method is to be used when creating small to medium size graphs. One can execute individual statements in gremlin-console or DataStax Studio or write a script that issues many statements programmatically.

This method is generally slower than Gremlin I/O utlity. However, Gremlin I/O is more suitable when exporting and importing existing graphs.

Gremlin mutating traversals are  more suitable for mutating existing graphs.

The Graph Loader utility is capable of parsing existing collections of text files and datasets stored in JDBC-compatible databases, and loading such data as graphs into DataStax Enterprise. The utility is designed for loading small to medium size graphs.

Spark-based bulk loading is ideal for large graphs.

Let’s talk more about the Graph API. The two main methods you should be aware of to start creating your own graphs are addVertex and addEdge. There are more methods that can be useful in the graph API which can be found in our documentation.

addVertex can be specified with a label to add a labeled vertex to the current graph or yo ucan simple add a vertex where the vertex label and properties are key-value pairs.

Similarly, the addEdge method allows you add an edge directed from the current vertex to the inVertex with the label and properties as key value pairs.

Two more important methods to know about are the property and remove methods. The property method allows you to add or set a property for a vertex, edge, or vertex properties. This is applicable to to regular, multi, and meta properties as well.

The remove method allows you to remove a vertex, edge, vertex property or even edge property from the graph.

Ok so now that we know the four main methods for interacting with the graph api, lets put it to good use. Here’s an example for creating vertices and vertex properties.

You can see that we define a vertex u (u for user) and we call the addVertex method and provide a label and property key value pairs. Simple.

Let’s take a look at another example. This time, we’re creating a new movie vertex in our killrvideo reference graph. Just like before, we define a vertex m (m for movie) and provide the label and property key value pairs.

Notice at the bottom, we can add more properties to our vertex m by using the property method and proving the property key value pair.

In this case, production is a multi-property therefore, has multiple production companies involved.

It wouldn’t be a graph use case if we didn’t connect vertices together so let’s go ahead and do that. Here we’re defining an edge E. Notice how we call the u (user) vertex from before and add the addEdge method with an edge label called rated and the inVertex.

Our relationship here is a user rated a movie so we start at the user vertex, addEdge method, edgeLabel, then add the inVertex, in this case, movie.

We can then take that edge and add the properties associated with it using the property method. A user rated a movie with a rating of 8.

Deleting properties, edges, and vertices is pretty simple. You add the remove method to thing you’d like to remove. At the top here, we’re removing the age and gender property from the user vertex we created earlier.

At the bottom, we’re removing the entire rated edge and movie vertex that we created earlier.

Here’s where things get interesting. Let’s say you want to connect a new vertex to an existing one already in your graph. You can easily mix graph mutations with traversals.

In our example here, we want to connect two users that know each other. We define our first user vertex and u1. Just like in the prior example, this one is using the addVertex method to create a new user.

Next, we want to define our second user u2 which already exists in the graph so we define the traveral that will return our u2 vertex.

Just like before, we can take the first user, call the addEdge method with the label and then the inVertex which is our u2 veretex.

At the bottom here, you can see a more compact version of the above example where we start by adding our vertex, chaining the addEdge method where we define the traversal that will return the existing vertex we’d like to connect our new vertex to.

Let’s take what you’ve learned about the graph api and put it to good use

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