Search This Blog

Monday 3 February 2020

Neo4j - create multiple nodes and connect with graph

In the last post I worked on creating a single node using cypher. There are other things that can be achieved using cypher when it comes to creation.
Consider that I want to add a Movie and its lead actors to the database.

public void createBulkNodes() {
    Movie newMovie = null;
    Actor a1, a2;

    final String QUERY = "CREATE (a1:Person { name:'Andrew Garfield', born:1983 })"
        + " -[r1:ACTED_IN { roles: ['Peter Parker', 'Spiderman']}]->"
        + " (m:Movie { title:'The Amazing Spider-Man 2',released:2014 }) "
        + " CREATE (a2:Person { name:'Emma Stone', born:1988 })" + " -[r2:ACTED_IN { roles: ['Gwen Stacy']}]->(m) "
        + " RETURN a1,a2,r1,r2,m";
    try (Transaction tx = graphDb.beginTx()) {
      try {
        Result result = graphDb.execute(QUERY);
        if (result.hasNext()) {
          while (result.hasNext()) {
            Map<String, Object> row = result.next();
            Node nodeProxy = (Node) row.get("m");
            newMovie = nodeProxy != null ? extractMovie(nodeProxy) : null;
            nodeProxy = (Node) row.get("a1");
            a1 = nodeProxy != null ? extractActor(nodeProxy) : null;
            nodeProxy = (Node) row.get("a2");
            a2 = nodeProxy != null ? extractActor(nodeProxy) : null;
            Relationship relationshipProxy = (Relationship) row.get("r1");
            String[] roles = (String[]) relationshipProxy.getProperty("roles", null);
            if (roles != null) {
              System.out.println(a1.getName() + " acted in " + newMovie.getTitle() + " in " + roles.length + " roles.");
            }
            relationshipProxy = (Relationship) row.get("r2");
            roles = (String[]) relationshipProxy.getProperty("roles", null);
            if (roles != null) {
              System.out.println(a2.getName() + " acted in " + newMovie.getTitle() + " in " + roles.length + " roles.");
            }
          }
         
        }
        tx.success();
      } catch (Exception e) {
        tx.failure();
      }
    }
  }
The above code will execute the two Cypher statements. As seen, the second Cypher statement refers to the Movie node created in the first statement. The output indicates the nodes returned by the CREATE statement.
Andrew Garfield acted in The Amazing Spider-Man 2 in 2 roles.
Emma Stone acted in The Amazing Spider-Man 2 in 1 roles.
It is also possible to connect nodes to an existing graph. For instance I would like to add a reviewer for the just created movie. For this we need the MATCH statement.
public void attachNodeToGraph() {
    final String QUERY = "MATCH (m:Movie{title:'The Amazing Spider-Man 2',released:2014})"
        + "CREATE (p:Person { name:'Hank Egbert Jones' })"
        + "CREATE (p)-[r:REVIEWED { summary: 'Fun, but not there yet', rating:65}]->(m)";
    try (Transaction tx = graphDb.beginTx()) {
      try {
        Result result = graphDb.execute(QUERY);
        if (result.hasNext()) {
          while (result.hasNext()) {
            System.out.println(result.next());
          }
        } else {
          System.out.println("Nothing Returned");
        }
        tx.success();
      } catch (Exception e) {
        tx.failure();
      }
    }
  }
Here the MATCH query returns the Movie node and then creates a new Person node and attaches it to the Graph. Also our Cypher query does not have to return anything. In this case, the Result instance returns no records.
The output can be seen in Neo4j:

1 comment:

  1. Thanks a lot for sharing this update. Hope you will not get tired of making posts as informative as this.  Redspider graphic design company dubai  is about to tell you that I appreciate your effort and thanks for sharing.

    ReplyDelete