Check whether a particular vertex exists or not in gremlin traversal

1,184 views
Skip to first unread message

Monel Gupta

unread,
Oct 12, 2015, 2:32:49 AM10/12/15
to Aurelius
I am trying to check first that whether a particular Vertex Exists in a graph or not if, it doesn't exists then i am creating a new vertex, the checker is based on properties , Here is the following code :

    public Vertex addVertex(Enum label, Map properties){
Vertex x = null;int fl=0,f=0;
if(properties.size()==0)
return null;
GraphTraversalSource g = graph.traversal();
Set<Enum> keys = properties.keySet();
for(Enum key: keys){
f++;
       properties.get(key);
       System.out.println("enter");
       if(g.V().hasLabel(label.toString()).has(key.toString(),properties.get(key)).next()!=null  ){
       fl++;
       System.out.println("exit");
       x=  g.V().hasLabel(label.toString()).has(key.toString(),properties.get(key)).next();
       System.out.println("exit2");
       }
}
if(fl==f){
return x;
}
x=graph.addVertex(T.label,label.toString(),properties);
return x;
}


But i am getting the following error .

    Exception in thread "main" org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException

How can this be solved, any leads will be really appreciated

Daniel Kuppitz

unread,
Oct 12, 2015, 7:19:12 AM10/12/15
to aureliu...@googlegroups.com
Wow, you should really take your time and read the docs to learn what Gremlin can do what not. We could go through your code, but I don't think it's worth it.

public Vertex getOrCreateVertex(final GraphTraversalSource g, final Enum label, final Map<Enum, Object> properties) {

    if (properties == null || properties.size() == 0) {
        return null;
    }

    final String vlabel = label.toString();
    GraphTraversal<Vertex, Vertex> getter = g.V().hasLabel(vlabel);
    GraphTraversal<Vertex, Vertex> creator = g.addV(T.label, vlabel);

    for (final Enum key : properties.keySet()) {
        final String pkey = key.toString();
        final Object pval = properties.get(key);
        getter = getter.has(pkey, pval);
        creator = creator.property(pkey, pval);
    }

    final GraphTraversal<Vertex, Vertex> factory = creator; 
    return getter.tryNext().orElseGet(factory::next);
}

Seriously, please read the docs.

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Aurelius" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aureliusgraph...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/aureliusgraphs/5247f389-8dd3-4ee4-9e8a-7bb842a8b97a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Plurad

unread,
Oct 12, 2015, 10:49:43 AM10/12/15
to Aurelius
Quick inspection of your code...

if (g.V().hasLabel(label.toString()).has(key.toString(),properties.get(key)).next() != null)

This can throw a FastNoSuchElementException. The graph traversal is an Iterator, so you should check hasNext() instead of next() != null.
Reply all
Reply to author
Forward
0 new messages