Inserting data using Gremlin

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.

  1. Using the graph methods addVertex() and addEdge()

  2. 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.

  3. 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.

  4. 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.

  5. 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.

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

  7. 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.

  8. 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.

  9. 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.

  10. 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.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com