Neo4j REST API + Extension Points

Neo4j REST API + Extension Points

Introduction

This blog deals with Neo4j – a standalone server with REST API. The REST request for Neo4j is handled with Extension Points implemented using Jersey framework. The Neo4j server also provides the REST interface that allows all modern platforms and programming languages to interoperate with it. The REST API uses HTTP calls and JSON MediaType, so that it can be used from many languages and platforms. Using REST APIs, we can perform ACID operation successfully in two ways ‑ Fine grained and Generic (using CQL). Please refer to our previous blogs on “Embrace Relationships with Neo4j, R & Java” and “Decipher Neo4J Cypher Query Language (CQL)” for more information on this.

Neo4j can be run in the following two modes:

  • An embedded database in a Java application
  • A standalone Neo4j server via REST

Jersey framework has more features than the JAX-RS Reference Implementation. Jersey provides its own API that extends the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development.

Use Case

Let us have the same StackOverflow dataset use case which we had generated in the blog “Embrace Relationships with Neo4j, R & Java”, where you were guided to build the StackOverflow dataset graph store using batch importer 2.0 and load it before Neo4j server gets online. This blog guides you to load all data from the StackOverflow dataset into the Neo4j Server while it is online through REST API calls. For this use case, we will create APIs using the Fine Grained method for creating nodes, relationships, properties and Generic method for retrieval nodes and relationships from graph store.

What we need to do:

  • Download StackOverflow Dataset, Data Manipulation with R, create Nodes & Relationships file with Java.
  • Create a dynamic web project using Jersey Jars.
  • Create Java programs which has APIs to add nodes, relations and execute Cypher Queries.
  • Execute Jersey REST APIs.

Solution

Prerequisites

  • Install JDK 1.7+.
  • Install Tomcat 7 and configure it with Eclipse.
  • Download Neo4j server: http://dist.neo4j.org/neo4j-community-2.1.2-windows.zip.
  • The essential project structure and libs to create the project are given below as a screenshot. Download all the Jersey Jars given here.

package_explorer

Download Stack Overflow Dataset, Data Manipulation with R, Create Nodes & Relationships file with Java

  • Do ignore this section if you are not interested to learn or know R and how the StackOverflow dataset came. I have attached the resulting csv files generated from that blog (“nodes-and-relationship.zip”) here.
  • If you are interested, you can follow the sections given below from the blog “Embrace Relationships with Neo4J, R & Java” to download and install Neo4j and to start Neo4j server web console to perform the use case.

Prerequisites

Create a Dynamic Web Project Using Jersey Jars

We have used Eclipse to create dynamic web projects using the jersey jar and its dependent jar files. We have created the following four API’s:

addNode Used to add nodes with properties like id (mandatory), name, score, views
addRelation Used to add relationship between nodes by assigning type from user
getOrCreateNodeById Used to get or create Node by its id as path param (mandatory)
executeCypherQuery used to execute cipher queries and return results as like from Neo4j REST HTTP calls

Create Java programs which has APIs to add nodes, relations and execute Cypher Queries

Run the web application in Tomcat:

  • Before creating war file, make sure Neo4j server is online in the local system by clicking on the link http://localhost:7474/
  • Create a war file “neo4j_extensionpoint” and load it in Tomcat Server.

Execute Jersey REST APIs

Run the below APIs from any browser. You can get response like the snapshot given below.

Add Nodes

Language Node:

http://localhost:8080/neo4j_extensionpoint/addNode/java?name=Java

http://localhost:8080/neo4j_extensionpoint/addNode/mysql?name=MySQL

Question Node:

http://localhost:8080/neo4j_extensionpoint/addNode/qid_754421?name=qid_754421&score=1&views=130

http://localhost:8080/neo4j_extensionpoint/addNode/qid_607934?name=qid_607934&score=2&views=122

http://localhost:8080/neo4j_extensionpoint/addNode/qid_838528?name=qid_838528&score=4&views=52

Answer Node:

http://localhost:8080/neo4j_extensionpoint/addNode/aid_722767?name=aid_722767&score=1

http://localhost:8080/neo4j_extensionpoint/addNode/aid_761025?name=aid_761025&score=19

User Node:

http://localhost:8080/neo4j_extensionpoint/addNode/uid_100927?name=Nicholas%20Arnold

http://localhost:8080/neo4j_extensionpoint/addNode/uid_82322?name=Blake%20Piper

Response in browser:

node_response_in_browser

Add Relation

http://localhost:8080/neo4j_extensionpoint/addRelation?sourceId=qid_801609&destinationId=java&relation=REFERS

http://localhost:8080/neo4j_extensionpoint/addRelation?sourceId=qid_898628&destinationId=mysql&relation=REFERS

Response in browser:

relation_response_in_browser

Get or Create Node

http://localhost:8080/neo4j_extensionpoint/getOrCreateNodeById/python

Response in browser:

get_response_in_browser

http://localhost:8080/neo4j_extensionpoint/getOrCreateNodeById/java

Response in browser:

get_response

Execute CQL Query

http://localhost:8080/neo4j_extensionpoint/executeCypherQuery?query=MATCH%20(sn)-[r]-(en)%20RETURN%20sn,r,en

Response in browser:

cql_query

Conclusion

  • Neo4j Server is the best choice for interoperability, safety, and monitoring. Also, being a standalone application, it is safer than the embedded configuration (a potential crash in the client wouldn’t affect the server), and it is easier to monitor. If we choose to use this mode, our application will act as a client of the Neo4j server.
  • Jersey framework is more than JAX-RS Reference Implementation. Jersey also exposes numerous extension APIs so developers may extend Jersey to suit their needs.

References

3842 Views 1 Views Today