Inserting data with traversal API
Inserting data with traversal API.
The Traversal API can be used to insert data into DSE Graph.
Procedure
-
Add a vertex:
Theg.addV('person').property('personId', 1).property('name','Julia Child').property('gender', 'F')
addV()
step must identify the vertex label, and can be followed by key and value pairs inproperty()
steps for any properties that are added. -
To add an edge using only the graph API, the two vertices that are connected by
the edge can be assigned variables that can be used in the
add()
statement:
or alternatively, the query can be constructed without variables:juliaChild = g.V().has('person', 'personId', 1).next() artOfFrenchCookingVolOne = g.V().has('book', 'bookId', 1001).next() g.V(juliaChild).addE('authored').to(artOfFrenchCookingVolOne)
The outgoing vertex, juliaChild, is connected to the incoming vertex, artOfFrenchCookingVolOne with ag.V().has('person','name','Julia Child') .addE('authored') .to(g.V().has('book','name','The Art of French Cooking, Vol. 1')
to()
step, to create andauthored
edge withaddE()
,If the edge has properties, the key and value pairs are appended to theaddE()
statement, similar to theaddV()
statement withproperty()
:g.V().has('person','name','Julia Child') .addE('created') .to(g.V().has('recipe','name','Beef Bourguignon')) .property('createDate', '1956-01-10')
-
A property can also be added to a previously created vertex, like
jamieOliver
:
AjamieOliver = g.V().has('person', 'name', 'Jamie Oliver').next() jamieOliver.property('gender', 'M').property('nickname', 'jimmy')
property()
value can also be modified by simply doing a similar traversal with a different value. -
A property can also be added to a previously created edge by constructing a
query that finds the edge and changes or adds the property with
property()
:g.V().has('person','name','Julia Child') .outE('created') .has('createDate', '1956-01-10') .property('createDate', '1956-09-09')