Search code examples
javagremlinjava-11janusgraph

JanusGraph Java unable to add vertex/edge


I've been trying to create interact with my JanusGraph setup in docker. But after many tries I still don't succeed.

How I connect to JG.

    public boolean connect() {
        try {
            graph = traversal().withRemote("path/to/janusgraph-cql-lucene-server.properties");
            return true;
        } catch (Exception e) {
            log.error("Unable to create connection with graph", e);
            return false;
        }
    }

How I try to add a vertex. It looks like this doesn't do anything.

        GraphTraversal<Vertex, Vertex> yt = graph.addV("link")
                .property("url", "https://www.youtube.com/123")
                .property("page_type", "contact");


        GraphTraversal<Vertex, Vertex> fb = graph.addV("link")
                .property("url", "https://www.facebook.com/456");
        
        graph.tx().commit();

I've added a node with the gremlin console. This works, so the setup is not invalid or something like that. And when I fetch all nodes I in my application get a valid response.
System.out.println(graph.V().hasLabel("link").count().next()); //returns 1 (the node I added manually)

My assumptions:

  • Setup is alright because it works in the gremlin console
  • connection
  • connection must be alright because the initialization doesn't throw an exception and we get a valid count response.

The only thing I'm not sure about is if there's a transaction commit that I am missing. I didn't find any other than graph.tx().commit();

Could you please help me and tell me what I am doing wrong?


Solution

  • The GraphTraversal object is only a "plan" to be carried out. To have it take effect, you need a closing method like next, toList, etc., like you did for the count.

    The confusion probably arose from the fact that the gremlin console automatically keeps nexting the traversal a configured number of times.