Neo4j connector in MuleSoft 4
In this tutorial we will demonstrate how to use neo4j connector in mule 4. But before that we should understand what is Graph Database.
Graph database is a database, that uses graph structure for storing data. It also treats relationship between data as equally important to the data itself. Graph databases are becoming popular database store. Since they store the relationship about data as well; traversing in graph database is easy and fast as compared to other database where you need to use expensive joins to traverse the data. Graph databases are also efficient in terms of searching the data irrespective of the size of the data set. They explore the data only within the starting points and leaves any data outside the search parameter.
Here is an example of a graph database. It considers the relationship various nodes as data also.
Image credit: https://neo4j.com/developer/graph-database/
There are various important aspects in a graph database.
- Node: Nodes are often used to represent entities. Nodes can contain properties that hold name-value pairs of data. Nodes can be assigned roles or types using one or more labels.
- Relationship: A relationship connects two nodes and allows us to find related nodes of data. It has a source node and a target node that shows the direction of the arrow.
- Label: To assign nodes to group or to categorize them
- Properties: To define attributes of these entities as key-value properties.
Image credit: https://neo4j.com/developer/graph-database/
If you want to read more on the graph database please refer to the link below https://neo4j.com/developer/graph-database/
Prerequisite
- Neo4j Graph database – Please use only neo4j 3.x database. Since the mulesoft neo4j connector version 3.0.4 has support for neo4j java driver 1.7.5, which supports only neo4j 3.x. If you use neo4.x with the current mulesoft neo4j connector you would get exceptions below.
“org.neo4j.driver.v1.exceptions.ServiceUnavailableException: Connection to the database terminated. This can happen due to network instabilities, or due to restarts of the database”
- Neo4j Desktop – Install Neo4j desktop to browse and connect to database using a GUI, command line options.
- MuleSoft Neo4j connector – Download the MuleSoft Neo4j connector version 3.0.4 from exchange. you can refer to https://docs.mulesoft.com/neo4j-connector/3.0/ for more details on the connector.
Steps
Install Neo4j 3.x community edition.
Download the Neo4j community edition graph database from and unzip the downloaded file https://neo4j.com/artifact.php?name=neo4j-community-3.5.22-windows.zip
Run the command “neo4j.bat console” to start the database
Download the Neo4j Desktop 4.1 from https://neo4j.com/download/ and install it. Connect to the local graph community database using “Add Database” as shown below in Neo4j desktop
Click on “Connect to remote DBMS” to connect to the neo4j database.
Enter the details of the database and click on Next as shown below.
Enter the details using “username/password” authentication option. Use default “neo4j” as username and password. Click on connect to connect to your local neo4j database.
Click on the Open button to open neo4j browser.
Enter the default credentials “neo4j” for username and password
It will prompt you to change the password. Otherwise you will not be able to do any operation with the database. Update the new password as below.
You are now connected to graph database with user “neo4j” and new password. You should be good to connect to the database from mulesoft now.
We will now show how to use neo4j connector in mule 4. Create a sample MuleSoft project and add the Neo4j connector from “Add Dependencies to Project” menu
Add the connection configuration as below and click on test connection. The connection test should be successful. If not, please check if your database has started or not.
There are various operations which you can perform using the connector for CRUD operations on nodes and execute CQL (command query language) in Neo4j graph database. Here is the detail below.
Create Node
use “create node” neo4j palette as below and payload as below
%dw 2.0 output application/json --- { "name": "John", "location": "US" }
You can verify the node and its properties in the Neo4j Desktop browser as below. we created two nodes with “Mulesy”, “India” and “John”, “US” as properties under the label “employee”.
Select Node
use “select node” neo4j palette as below and payload as below
%dw 2.0 output application/json --- { "name": "John", "location": "US" }
Delete Node
use “delete node” neo4j palette as below and payload as below
%dw 2.0 output application/json --- { "name": "John", "location": "US" }
Execute
You can execute CQL (cypher query language) using the Execute palette as below
CQL commands
"CREATE (a:ACTOR:PERSON { name:\"Tom Hanks\", born:1956 })" "CREATE (john:Person {name: 'John'})" "CREATE (joe:Person {name: 'Joe'})" "CREATE (steve:Person {name: 'Steve'})" "CREATE (sara:Person {name: 'Sara'})" "CREATE (maria:Person {name: 'Maria'})" "CREATE (john)-[:FRIEND]->(joe)-[:FRIEND]->(steve)" "CREATE (john)-[:FRIEND]->(sara)-[:FRIEND]->(maria)"
You can verify the results in the Neo4j browser as below.
Sample Demo Application – mulesy-neo4j