Inserting data with graph traversal API

The Traversal API can be used to insert data into DataStax Graph (DSG).

Procedure

  • Add a vertex:

    g.addV('person').
       property('person_id', 'adb8744c-d015-4d78-918a-d7f062c59e8f' as UUID).
       property('name', 'Simone BECK').
       property('gender','F').
       property('nickname', ['Simca', 'Simone'] as Set).
       property('country', [['France', '1904-07-07' as LocalDate, '1991-12-20' as LocalDate] as Tuple])

    The addV() step must identify the previously created vertex label, and can be followed by key and value pairs in property() steps for any properties that are added.

  • A property can also be added to an existing vertex, like person:

    g.V('dseg:/person/e1388dd1-1af8-481b-8504-c1e1457072cf').
      property('name', 'Jamie OLIVER').
      property('gender', 'M')

    A property() value can also be modified by doing a similar traversal with a different value.

  • Add an edge using two existing vertices:

    g.V('dseg:/person/e7cd5752-bc0d-4157-a80f-7523add8dbcd').as('a').
      V('dseg:/book/1001').as('b').
      addE('authored').from('a').to('b')

    The outgoing vertex, Julia CHILD, is connected to the incoming vertex, The Art of French Cooking, with from() and to() steps to create an authored edge with addE().

    If the edge has properties, the key and value pairs are appended to the addE() statement, similar to the addV() statement with property():

    g.V().has('person', 'person_id', 'daa02698-df4f-4436-8855-941774f4c3e0' as UUID).as('a').
       V().has('recipe', 'recipe_id', 2001).as('b').
       addE('reviewed').
       property('time', '12:00:00' as LocalTime).
       property('year', '2015-12-31' as LocalDate).
       property('stars', 3).
       property('comment', 'It was okay.').
       from('a').to('b')
  • A property can also be added to an existing edge by constructing a query that finds the edge and changes or adds the property with property():

    g.V('dseg:/person/e7cd5752-bc0d-4157-a80f-7523add8dbcd').
        outE('created').
        has('create_date', '1956-01-10' as LocalDate).
        property('create_date', '1956-09-09' as LocalDate)

    Altenatively:

    g.E().
      hasLabel('created').
      has('create_date', '1961-01-01' as LocalDate).
      property('create_date', '1956-09-09' as LocalDate)

    The first query starts with a vertex and traverses to edges to find the matching edge property create_date. The second query starts with the edges and matches the edge property create_date. Compare the profile() of each query and the structure of your data to determine which query to use.

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