cassandradatastax-enterprisedatastax-java-driverdatastax-enterprise-graph

DataStax Enterprise Graph adding Edge using Fluent API


i was able to add an edge in DSE graph using the native api from java. but when i try to add the edge using the fluent api in java, it doesnt work. i went through the documentation for fluent api and there was nothing to show how to add an edge. When i try to add the edge in java it gives a run time error telling Edge cannot be added

is there any way to add an edge using the fluent api from java

want to use the fluent api since it is more readable from the java code

Sample Code:

lets say there is a vertex called user. another vertex called movie. i want an edge between user and movie. this is what i am trying in java. i am able to add the vertex (the code shows adding only one vertex ) and it worked fine. not really sure about the edge part , when i try to add the edge i get the run time error

 GraphTraversal<Vertex,Vertex> traversal =  g.addV("user").property("name", 
 "abcd");
 GraphStatement graphStatement = DseGraph.statementFromTraversal(traversal);
 GraphResultSet grs = dseSession.executeGraph(graphStatement);
 Vertex user1 = g.V().has("user","name","abcd").next();
 g.V().has("movie","name","movie1").next().addEdge("ratedBy",user1)

Solution

  • It will not work quite that way. As soon as you do this (i.e. call next()):

    g.V().has("movie","name","movie1").next().addEdge("ratedBy",user1)
    

    you are no longer using the Traversal API. Everything after that is the Graph API and the Graph API is not supported for remote execution. To be more clear, calling next() emits a Vertex which is not part of the Traversal API. Looking at the javadocs might yield more clarity on the distinction there. The Graph API is for providers (those who implement the TinkerPop interfaces) and are here. The Traversal API is for users and it internally utilizes the Graph API to execute Gremlin against different graph implementations. The Traversal API largely consists of the GraphTraversal and GraphTraversalSource classes shown here.

    You should simply execute a single traversal to construct the edge:

    g.addV('user').property('name','abcd').as('user').
      V().has('movie','name','movie1').
      addE('ratedBy').to('user')