Search This Blog

Monday 3 February 2020

Neo4j - Create a node with Cypher

I decided to use Cypher to create a simple record. Here I tried to create a Movie instance

public Movie createMovie(String title, int released, String tagline) {
    try (Transaction tx = graphDb.beginTx()) {
      Map<String, Object> params = new HashMap<>();
      params.put("mName", title);
      params.put("releasedYear", released);
      params.put("tagline", tagline);
      Result result = graphDb.execute(
          "CREATE (m :Movie { title:{mName},released:{releasedYear},tagline:{tagline} }) " 
                + "RETURN m", params);
      if (result.hasNext()) {
        while (result.hasNext()) {
          Map<String, Object> row = result.next();
          Node nodeProxy = (Node) row.get("m");
          return extractMovie(nodeProxy);
        }
      }
    }
    return null;
  }
The code executes a Cypher query that creates a node and returns the resultant Node.
public static void main(String[] args) {
    MovieDAO movieDAO = new MovieDAO();
    Movie newMovie = movieDAO.createMovie("Spectre", 2015, "");
    System.out.println("New Movie added with id " + newMovie.getId());
  }
The output of the created instance is :
New Movie added with id 171
However if I look into Neo4j I do not see any new movies added. In fact if I ran the same code again, I would see a new movie created with the same id - this happens only if the id is available. This means that my transaction was not committed. Accordingly I changed the code:
  Result result = graphDb.execute(
      "CREATE (m :Movie { title:{mName},released:{releasedYear},tagline:{tagline} }) " + "RETURN m", params);
  tx.success();
When the transaction closes, the success call will make sure the commit occurs.In case of failure we have the close method rolling it back if the failure method had been invoked on the transaction.
public Movie createMovie(String title, int released, String tagline) {
    Movie newMovie = null;

    final String QUERY = "CREATE (m :Movie { title:{mName},released:{releasedYear},tagline:{tagline} }) RETURN m";
    Map<String, Object> params = new HashMap<>();
    params.put("mName", title);
    params.put("releasedYear", released);
    params.put("tagline", tagline);

    try (Transaction tx = graphDb.beginTx()) {
      try {
        Result result = graphDb.execute(QUERY, params);
        if (result.hasNext()) {
          while (result.hasNext()) {
            Map<String, Object> row = result.next();
            Node nodeProxy = (Node) row.get("m");
            newMovie = nodeProxy != null ? extractMovie(nodeProxy) : null;
          }
        }
        if (newMovie != null) {
          // Movie was not returned
          tx.success();
        }
      } catch (Exception e) {
        tx.failure();
      }
    }
    return newMovie;
  }
I verified the node presence by running a query on its id.

There are other ways to create nodes. This is just one way.

1 comment:

  1. An obligation of appreciation is all together for putting aside the push to grant us such a mind-blowing article. Also visit here: Feeta.pk property for sale in islamabad . Keep Sharing..

    ReplyDelete