Inserting data using Gremlin

How to insert data into a graph database.

The discussion below describes the detailed view of submitting individual commands to create vertices and edges. However, due to the sessionless nature of the interaction between the Gremlin console and the remote Gremlin server, all steps must be submitted in a single script. To load the data shown in the steps below, run the generateRecipe.groovy script.

Two methods are shown below, using graph methods and using graph traversal methods. The graph methods have been benchmarked as several times faster for insertions in informal testing. The graph traversal methods are useful for on-the-fly insertions during development.

Although DSE Graph will insert data without previously creating schema, it is best practice to create schema prior to data insertion. If inserted without schema, the data type will often be incorrect. For instance, in the examples below, ISBN is a UUID. However, if inserted as shown, the data type assigned to ISBN will be Text.

Procedure

Using the graph methods addVertex() and addEdge()

  1. Add an author vertex using addVertex().
    juliaChild = graph.addVertex(label,'author', 'name','Julia Child', 'gender','F')

    The vertex is given a descriptive name juliaChild, and is given a vertex label of author. The name and gender key-value pairs for the property keys are listed.

  2. Add a book vertex using addVertex().
    frenchChefCookbook = graph.addVertex(label, 'book', 'name, 'The French Chef Cookbook', 'year' , 1968, 'ISBN', '0-394-40135-2')

    A book vertex includes a book name, publishing year, and an ISBN code, if available.

  3. Add an edge between an author and a book using addEdge().
    juliaChild.addEdge('authored', frenchChefCookbook)

    The vertex for Julia Child has an authored edge to the book The French Chef Cookbook.

  4. Edges can also have properties, as this example of an edge between a recipe and an ingredient demonstrates.
    beefBourguignon.addEdge('includes', beef, 'amount','2 lbs')

    The recipe Beef Bourguignon includes 2 pounds of beef. The amount is stored as an edge property. The recipe and the ingredient vertices must be inserted prior to the edge.

Using the graph traversal methods addV() and add()

  1. Add an author vertex using addV(). and property(). Note the use of multiple property() steps, one for each property set.
    g.addV('author').property('name','Julia Child').property('gender','F')

    A vertex label of author is defined in the addV() step. The name and gender key-value pairs for the property keys are created using two property() steps.

  2. Add a book vertex using addV().
    g.addV('book').property('name', 'The French Chef Cookbook').property('year' , 1968).property('ISBN', '0-394-40135-2')

    A book vertex includes a book name, publishing year, and an ISBN code, if available.

  3. Add an edge between an author and a book using addE().
    g.V().has('author','name','Julia Child').as('a').V().has('book','name','The French Chef Cookbook').addE('authored').to('a')

    The vertex for Julia Child has an authored edge to the book The French Chef Cookbook. Each vertex is identified by its vertex label and property name. The author vertex is labeled as a and used in the to() step.

  4. Edges can also have properties, as this example of an edge between a recipe and an ingredient demonstrates.
    g.V().has('recipe','name','Beef Bourguignon').as('a').
        V().has('ingredient','name','beef').
        addE('includes').from('a').property('amount','2 lbs')

    The recipe Beef Bourguignon includes 2 pounds of beef. The amount is stored as an edge property. The recipe and the ingredient vertices must be inserted prior to the edge.