similar edges and how to prevent them

433 views
Skip to first unread message

yair...@gmail.com

unread,
Sep 11, 2017, 2:53:49 AM9/11/17
to JanusGraph users

I have an app where I am migration a d non graph DB graph to Janus. So I create the graph when I first start the app. Using cassandra backend.

I have an issue where 2 vertices have "identical" edges (besides the auto-generated id of course) are created in my code causing me to get too many IN and OUT edges.

I read my vertex like this:


javaGraph.traversal().V().hasLabel(instanceId).has("nodeId", lineageNodeId).toList();




I read Both edges:

vertex.edges(org.apache.tinkerpop.gremlin.structure.Direction.IN, "link");


vertex
.edges(org.apache.tinkerpop.gremlin.structure.Direction.OUT, "link");


each time I run my app it doesn't "see" the existing edges and adds to it. so I get edges multiplies while I only want "link" edge between any 2 given vertices

in order to make sure I don't create duplicate vertices:


        GraphTraversal<Vertex, Vertex> vertexNode = javaGraph.traversal().V().hasLabel(instanceId).has("nodeId", myNodeId);
        if (vertexNode.hasNext()) {
            return vertexNode.next();
        } else {            

            JanusGraphVertex node = javaGraph.addVertex(instanceId);
            node.property("nodeId", myNodeId);
            return node;
        }



But I don't know once I have vertex A and B at hand - how to verify they are not already connected.






yair...@gmail.com

unread,
Sep 11, 2017, 3:25:42 AM9/11/17
to JanusGraph users




One Option I thought of that should have worked but doesn't is:

Assuming I have a method to create between 'from' vertex and 'to' vertex:



       
Iterator<Edge> edgesIter = from.edges(Direction.OUT, "link");


       
while (edgesIter.hasNext()) {
           
Edge edge = edgesIter.next();
           
Vertex outVertex = edge.outVertex();
           
if(outVertex.property("nodeId").value().equals(to.property("nodeId").value())){
               
return edge;
           
}


       
}
        org
.apache.tinkerpop.gremlin.structure.Edge linkedEdge = from.addEdge("link", to);
       
return linkedEdge;


I still get duplicate edges when I re-run my app. and yes I do commit the tx after i'm done.

Any ideas?

tpr...@gmail.com

unread,
Sep 11, 2017, 6:32:04 AM9/11/17
to JanusGraph users
You can use coalesce step to do a check or insert query.
http://tinkerpop.apache.org/docs/current/reference/#coalesce-step
Something like g.V(idTo).as('to').V(idFrom).coalesce(__.outE('labelE').has('id',idValue),
__.addE('labelE').property('id',idvalue).to('to'))

Daniel Kuppitz

unread,
Sep 11, 2017, 9:37:18 AM9/11/17
to JanusGraph users
I guess the point is, that the edge's direction doesn't matter. Hence it should be more like:

g.V().has(instanceId, "nodeId", nodeA).as("a").
  V().has(instanceId, "nodeId", nodeB).
  coalesce(bothE("link").where(otherV().as("a")),
           addE("link").to("a"))

...or, if performance matters, don't enable path computations (using otherV):

g.V().has(instanceId, "nodeId", nodeA).as("a").
  V().has(instanceId, "nodeId", nodeB).
  coalesce(outE("link").where(inV().as("a")),
           inE("link").where(outV().as("a")),
           addE("link").to("a"))

Cheers,
Daniel



--
You received this message because you are subscribed to the Google Groups "JanusGraph users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to janusgraph-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/janusgraph-users/020a1758-af67-4510-9860-90bc7b470709%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

than...@gmail.com

unread,
Jan 19, 2019, 9:10:16 PM1/19/19
to JanusGraph users
Hi Daniel,
I want to create both vertex and edge if they are not exist. 
I do as the following but it's not work (vertices have been created but no relation between them)

g.V().hasLabel("V1")
.has("userId", userId).fold()
.coalesce(
        unfold
(),
        addV
("V1").property("userId", userId1)
).as("a")
.V().hasLabel("V1").has("userId", userId2).fold()
.coalesce(
        unfold
(),
        addV
("V1").property("userId", userId2)
)
.coalesce(
        bothE
("link").where(outV().as("a")),
        addE
("link").from("a")
)

Do you know why?

Vào 20:37:18 UTC+7 Thứ Hai, ngày 11 tháng 9 năm 2017, Daniel Kuppitz đã viết:
I guess the point is, that the edge's direction doesn't matter. Hence it should be more like:

g.V().has(instanceId, "nodeId", nodeA).as("a").
  V().has(instanceId, "nodeId", nodeB).
  coalesce(bothE("link").where(otherV().as("a")),
           addE("link").to("a"))

...or, if performance matters, don't enable path computations (using otherV):

g.V().has(instanceId, "nodeId", nodeA).as("a").
  V().has(instanceId, "nodeId", nodeB).
  coalesce(outE("link").where(inV().as("a")),
           inE("link").where(outV().as("a")),
           addE("link").to("a"))

Cheers,
Daniel

On Mon, Sep 11, 2017 at 3:32 AM, <tpr...@gmail.com> wrote:
You can use coalesce step to do a check or insert query.
http://tinkerpop.apache.org/docs/current/reference/#coalesce-step
Something like g.V(idTo).as('to').V(idFrom).coalesce(__.outE('labelE').has('id',idValue),
     __.addE('labelE').property('id',idvalue).to('to'))

--
You received this message because you are subscribed to the Google Groups "JanusGraph users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to janusgraph-use...@googlegroups.com.

Daniel Kuppitz

unread,
Jan 20, 2019, 11:08:19 AM1/20/19
to janusgra...@googlegroups.com
There are two issues in your query. The first one is the reason why it doesn't work as expected: the fold() step. Using fold() will destroy the path history, but you can easily work around it, by doing that part in a child traversal:

g.V().has("V1","userId", userId).fold().
  coalesce(unfold(),
           addV("V1").property("userId", userId1)).as("a").
  map(V().has("V1","userId", userId2).fold()).
  coalesce(unfold(),
           addV("V1").property("userId", userId2))
  coalesce(bothE("link").where(outV().as("a")),
           addE("link").from("a"))

The second issue is the combination of bothE and outV. You should rather use bothE/otherV, outE/inV or inE/outV.

Cheers,
Daniel



Reply all
Reply to author
Forward
0 new messages