[go: up one dir, main page]

0% found this document useful (0 votes)
102 views32 pages

Java and Neo4j Integration Guide

Uploaded by

Achraf Hachhach
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views32 pages

Java and Neo4j Integration Guide

Uploaded by

Achraf Hachhach
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

white title

Creating a Graph in Neo4j using Java and IntelliJ


IDEA
Réalisé par : Boukhari Walid & Oqaidi Anass

Encadré par : Pr. ZIYATI Houssaine


Abstract
This project explores the integration of Java with Neo4j, a leading graph database, using the IntelliJ IDEA inte-
grated development environment. The primary objective was to demonstrate how to establish a connection from a
Java application to a Neo4j database and to create a graph structure within Neo4j using Java code.

The report outlines the methodology, tools, and techniques employed to achieve this integration. We describe
the step-by-step process of connecting a Java project to Neo4j, including setting up connection parameters and
establishing a connection from within Java. Subsequently, we delve into creating a graph in Neo4j by executing
Cypher queries from Java. Example code snippets are provided, along with explanations, to illustrate the process.

The results section discusses the challenges encountered during development and presents the outcome of the
project, including the structure and content of the graph. The report concludes by highlighting the significance of
this integration for graph-based applications and suggests potential areas for future work and enhancements.

This project serves as a practical guide for developers and data enthusiasts looking to leverage Neo4j’s graph
database capabilities within Java applications, opening up opportunities for the development of graph-based solu-
tions in various domains.

Key words :
— Java
— Neo4j
— IntelliJ IDEA
— Graph database
— Cypher query language
— Neo4j Java driver
— Database integration
— Graph creation
— Data modeling
— Java development
Table des matières

Chapitre 1 : Methodology 2

1 Tools and Technologies 3


1.1 IntelliJ IDEA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Neo4j . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Project Setup Steps 5

Chapitre 2 : Java to Neo4j 6

1 Connecting Java to Neo4j : 7


1.1 Importing Neo4j Java Driver Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 Setting up Connection Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Establishing a Connection to the Neo4j Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2 Creating a Graph in Neo4j 9


2.1 Creating a Neo4j Session or Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Defining and Executing Cypher Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3 Handling Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3 Example Code 11

Chapitre 3 : The Main Project 13

1 The project 14

2 The Proposed Java Code 15

3 Neo4j Outcome 22

4 Dijkstra Algorithm 23

Chapitre 4 : Conclusion 26

1 Main Achievements of the Project 27

2 Significance of Linking Java and Neo4j for the Use Case 27


Table des figures

1 IntelliJ IDEA Icon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


2 IDE Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Neo4j Logo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4 Neo4j Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5 Visualization On Neo4j browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7 Neo4j Outcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
8 Dijkstra Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
9 Dijkstra Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Listings

1 Example of how to include the Neo4j Java driver library in your Maven project’s ’pom.xml’ file . . . 7
2 Define the connection parameters in your Java code . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Neo4j Connector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Neo4j session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Cypher queries in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7 The Proposed Java code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
8 Dijkstra Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
9 findFriends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Project Rapport

Introduction
In a progressively interconnected and data-driven world, effective data management and retrieval are of para-
mount importance to the success of modern software applications. The advent of graph databases has revolutionized
the way we model, store, and query complex, interconnected data structures, making them an essential tool for va-
rious domains. Graph databases excel at representing complex relationships between data points, which is essential
for many applications such as fraud detection, social network analysis, and knowledge graph construction.

Java is a widely used and versatile programming language that is well-suited for developing graph database
applications. Neo4j is a leading graph database management system that offers a powerful and scalable platform
for storing and querying graph data. The integration of Java and Neo4j can provide significant advantages for
developers, including :

• Improved performance : Neo4j is a native Java database, which means that it is optimized for Java
applications. This can lead to significant performance improvements for graph database applications developed
in Java.

• Reduced development complexity : The integration of Java and Neo4j provides a seamless development
experience, with libraries and tools that make it easy to develop and deploy graph database applications.

• Increased productivity : Developers who are already familiar with Java can quickly learn to develop graph
database applications using Neo4j. This can lead to increased productivity and faster time to market for graph
database applications.

This project aims to explore the seamless integration of Java and Neo4j using the IntelliJ IDEA integrated develop-
ment environment. We will discuss the benefits of using Java and Neo4j together, and provide practical examples
of how to develop and deploy graph database applications using this integration. We will also cover some of the
challenges that can be encountered when developing graph database applications, and provide solutions to these
challenges.

1. Purpose of the Project :


The fundamental purpose of this project is to serve as the student’s final exam, demonstrating their proficiency
in Java and Neo4j integration. As the project creator, the student aims to provide comprehensive evidence of
their ability to apply theoretical knowledge and practical skills in linking Java with Neo4j.

2. Significance of the Project :


In the context of a student’s final exam, the significance of this project is twofold. Firstly, it assesses the
student’s understanding of Java and Neo4j integration. Secondly, it showcases the student’s readiness to apply
this knowledge in real-world scenarios, which is a crucial aspect of an effective education.

3. Overview of Neo4j and Java Integration for Student Assessment :


This project focuses on assessing the student’s proficiency in the seamless integration of Neo4j with Java. It
serves as a practical demonstration of the student’s command over key concepts and their capacity to convey
complex technical knowledge effectively.

1
Project Rapport

CHAPITRE 1 :
Methodology :

2
Project Rapport

1 Tools and Technologies


In the pursuit of integrating Java with Neo4j, several key tools and technologies played a pivotal role in the
development and validation of this project.

1.1 IntelliJ IDEA

Figure 1 – IntelliJ IDEA Icon

The choice of IntelliJ IDEA as the integrated development environment (IDE) for Java development was deli-
berate and well-considered. IntelliJ IDEA is a widely acclaimed IDE that stands out for its advanced code analysis
and coding assistance features. It provides an intuitive user interface, extensive libraries, and seamless integration
with build tools like Maven and Gradle. This IDE’s robustness and efficiency make it an ideal platform for Java
development. Its intelligent code completion, refactoring, and debugging capabilities ensure productivity and code
quality. As the selected IDE, IntelliJ IDEA offered a conducive environment for designing, coding, and testing the
Java-Neo4j integration, ultimately contributing to the project’s success.

Figure 2 – IDE Interface

3
Project Rapport

Figure 3 – Neo4j Logo

1.2 Neo4j
Neo4j is a leading graph database management system renowned for its exceptional capabilities in managing
data with complex, interconnected relationships. It employs a native graph storage and processing engine, which
is essential for efficient traversal and querying of intricate graph structures. Neo4j introduces the Cypher query
language, designed specifically for graph traversal and manipulation. This choice of Neo4j aligns with the project’s
objective to demonstrate the integration of Java with a graph database system. Neo4j’s native graph storage,
powerful query language, and comprehensive set of graph database features make it an ideal platform for working
with graph data.

Figure 4 – Neo4j Interface

4
Project Rapport

2 Project Setup Steps


The successful execution of this integration project hinged on setting up the necessary tools and technologies.
The following steps detail the setup process :
Step 1 : Installing Neo4j on Your System
The initial step in this project was the installation of Neo4j on the local system. Neo4j provides installer packages
for various operating systems, ensuring accessibility to a broad user base. The installation process typically involves
downloading the installer for the specific operating system, executing it, and configuring essential parameters, such
as setting an initial password for the Neo4j database. These setup steps were customized to align with the specific
operating system used, ensuring a smooth and hassle-free installation. The installation of Neo4j on the local system
laid the foundation for establishing the linkage between Java and Neo4j.
Step 2 : Creating a Java Project in IntelliJ IDEA
With Neo4j installed and ready for use, the subsequent pivotal step was the creation of a Java project within the
IntelliJ IDEA IDE. The project was structured to serve as a practical demonstration of the seamless integration
between Java and Neo4j, encompassing Java classes, code, and external dependencies. This step encompassed the
selection of an appropriate project template within IntelliJ IDEA, defining the project structure, configuring build
tools, and ensuring that the development environment was conducive to Java development. The creation of the Java
project within IntelliJ IDEA marked the initiation of the practical integration and coding phase.
In summary, the methodology for this project outlines the careful selection of tools and technologies, including
IntelliJ IDEA and Neo4j, and delineates the essential setup steps, involving the installation of Neo4j on the local
system and the creation of a Java project within the chosen IDE. These foundational steps served as the cornerstone
for the subsequent phases of code development and integration.

5
Project Rapport

CHAPITRE 2 :
Java to Neo4j :

6
Project Rapport

1 Connecting Java to Neo4j :


1.1 Importing Neo4j Java Driver Library
The first step in connecting your Java project to Neo4j is to import the Neo4j Java driver li-
brary. This library provides the necessary classes and methods to interact with the Neo4j database
from your Java application.

You can include the Neo4j Java driver library in your Java project by adding it to your project’s
dependencies. This can typically be done through build management tools like Maven or Gradle,
or by manually downloading the library and including it in your project’s classpath.
Here’s an example of how to include the Neo4j Java driver library in your Maven project’s ’pom.xml’
file :
1 < dependencies >
2 < dependency >
3 < groupId > org . neo4j . driver </ groupId >
4 < artifactId > neo4j - java - driver </ artifactId >
5 < version >4.3.3 </ version > <! - - Replace with the desired
version -->
6 </ dependency >
7 </ dependencies >
Listing 1 – Example of how to include the Neo4j Java driver library in your Maven project’s ’pom.xml’ file

1.2 Setting up Connection Parameters


Once you’ve imported the Neo4j Java driver library, you need to set up the connection para-
meters, including the Neo4j server URL, username, and password. These parameters are essential
for establishing a connection to the Neo4j database.

Here’s how you can define the connection parameters in your Java code :

1 String uri = " bolt :// localhost :7687 " ; // Replace with the
appropriate server URL
2 String username = " neo4j " ; // Replace with your Neo4j
username
3 String password = " your_password " ; // Replace with your Neo4j
password
Listing 2 – Define the connection parameters in your Java code

In the code above, replace "bolt ://localhost :7687" with the actual Neo4j server URL, "neo4j"
with your Neo4j username, and "your_password" with your Neo4j password.

7
Project Rapport

1.3 Establishing a Connection to the Neo4j Database


With the connection parameters set, you can establish a connection to the Neo4j database from
your Java code using the Neo4j Java driver. Here’s an example of how to do this :

1 import org . neo4j . driver .*;


2

3 public class Neo4jConnector {


4 public static void main ( String [] args ) {
5 String uri = " bolt :// localhost :7687 " ;
6 String username = " neo4j " ;
7 String password = " your_password " ;
8

9 // Create a Neo4j driver


10 Driver driver = GraphDatabase . driver ( uri ,
AuthTokens . basic ( username , password ) ) ;
11

12 // Establish a session for database interactions


13 try ( Session session = driver . session () ) {
14 // Your Neo4j database interactions go here
15 } catch ( Exception e ) {
16 e . printStackTrace () ;
17 } finally {
18 driver . close () ; // Close the driver when done
19 }
20 }
21 }
Listing 3 – Neo4j Connector

In the code above :


• We create a Neo4j driver using the provided connection parameters.
• We establish a session using try-with-resources to ensure proper resource management.
• Inside the session, you can perform Neo4j database interactions, such as executing Cypher
queries.
By following these steps, you can successfully connect your Java project to a Neo4j database,
enabling you to interact with the graph database from your Java code.

8
Project Rapport

2 Creating a Graph in Neo4j


The process of creating a graph in Neo4j using Java involves several key steps, including esta-
blishing a Neo4j session or transaction, defining and executing Cypher queries to create nodes and
relationships, and handling errors and exceptions that may arise during the graph creation process.

2.1 Creating a Neo4j Session or Transaction


To interact with a Neo4j database from a Java application, it is essential to establish a session
or transaction. This provides a connection to the database and allows for the execution of Cypher
queries.
In Java, this process typically involves the following steps :
• Importing the necessary Neo4j Java driver library in your Java project.
• Setting up the connection parameters, including the Neo4j server URL, username, and pass-
word.
• Creating a session or transaction object to interact with the database.
Here’s a simplified example of how you might set up a Neo4j session in Java :
1 import org . neo4j . driver .*;
2

3 public class Neo4jGraphCreator {


4 public static void main ( String [] args ) {
5 // Define the connection parameters
6 String uri = " bolt :// localhost :7687 " ;
7 String username = " neo4j " ;
8 String password = " your_password " ;
9

10 // Create a Neo4j driver and session


11 Driver driver = GraphDatabase . driver ( uri ,
AuthTokens . basic ( username , password ) ) ;
12 try ( Session session = driver . session () ) {
13 // Your graph creation code goes here
14 } catch ( Exception e ) {
15 // Handle any exceptions
16 e . printStackTrace () ;
17 } finally {
18 driver . close () ;
19 }
20 }
21 }
Listing 4 – Neo4j session

9
Project Rapport

2.2 Defining and Executing Cypher Queries


Cypher is the query language used in Neo4j for creating, updating, and querying graph data.
To create nodes and relationships in the graph, you’ll need to define and execute Cypher queries.
Cypher is expressive and well-suited for graph-related operations.
Here’s a simplified example of how you might use Cypher to create a node and a relationship in a
Neo4j graph :
1 try ( Session session = driver . session () ) {
2 // Define a Cypher query to create a node
3 String createNodeQuery = " CREATE ( n : Person { name : ’ Mark ’, age :
30}) " ;
4 session . writeTransaction ( tx -> tx . run ( createNodeQuery ) ) ;
5

6 // Define a Cypher query to create a relationship between nodes


7 String createRelationshipQuery =
8 " MATCH ( a : Person { name : ’ Mark ’}) ,( b : Person { name : ’ Ann ’}) " +
9 " CREATE ( a ) -[: FRIENDS_WITH ] - >( b ) " ;
10 session . writeTransaction ( tx ->
tx . run ( createRelationshipQuery ) ) ;
11 } catch ( Exception e ) {
12 e . printStackTrace () ;
13 }
Listing 5 – Cypher queries in Java

2.3 Handling Errors and Exceptions


Handling errors and exceptions is a critical aspect of creating a graph in Neo4j using Java.
You should wrap your code in appropriate try-catch blocks to handle potential exceptions, such
as connection errors, query execution errors, or constraint violations. This ensures that your ap-
plication behaves gracefully when issues occur during graph creation.

In the provided code examples, we catch exceptions and print the stack trace. Depending on
your specific project requirements, you may want to implement more sophisticated error handling
and logging mechanisms.

By following these steps and best practices, you can effectively create a graph in Neo4j using
Java, define the structure of your graph with nodes and relationships, and handle errors that
may arise during the process. This allows you to harness the power of Neo4j’s graph database
capabilities within your Java application.

10
Project Rapport

3 Example Code
Here are code snippets that demonstrate how to connect to Neo4j using Java and create a simple
graph. I’ve included comments within the code to explain each step :
1 package org . example ;
2

3 import org . neo4j . driver .*;


4

5 public class Neo4jGraph {


6 public static void main ( String [] args ) {
7 // Define the connection parameters
8 String uri = " bolt :// localhost :7687 " ;
9 String username = " neo4j " ;
10 String password = " 12345678 " ;
11

12 // Create a Neo4j driver and session


13 Driver driver = GraphDatabase . driver ( uri ,
AuthTokens . basic ( username , password ) ) ;
14

15 try ( Session session = driver . session () ) {


16 // Define Cypher queries to create nodes and
relationships
17 String createNodeQuery = " CREATE ( n : Person { name :
’ Alice ’, age : 30}) " ;
18 String createNodeQuery2 = " CREATE ( n : Person { name :
’ Bob ’, age : 30}) " ;
19 String createFriendQuery = " MATCH ( a : Person { name :
’ Alice ’}) , ( b : Person { name : ’ Bob ’}) " +
20 " CREATE ( a ) -[: FRIENDS_WITH ] - >( b ) " ;
21

22 // Start a write transaction to execute the queries


23 session . writeTransaction ( tx -> {
24 // Create a node for ’ Alice ’
25 tx . run ( createNodeQuery ) ;
26 // Create a node for ’ Bob ’
27 tx . run ( createNodeQuery2 ) ;
28 // Create a relationship between ’ Alice ’ and ’ Bob ’
29 tx . run ( createFriendQuery ) ;
30 return null ;
31 }) ;
32 } catch ( Exception e ) {
33 e . printStackTrace () ;
34 } finally {
35 driver . close () ;
36 }
37 }
38 }
Listing 6 – Example

11
Project Rapport

Explanation :
1. Import the necessary Neo4j Java driver classes to establish a connection to the Neo4j database.
2. Define the connection parameters, including the Neo4j server URL (uri), username, and pass-
word. Replace "your_password" with your actual password.
3. Create a Neo4j driver and session to interact with the database. The try-with-resources block
ensures that the session is automatically closed when the block is exited.
4. Define two Cypher queries : createNodeQuery to create a node labelled as "Person" with
properties "name" and "age" for Alice, and createFriendQuery to create a relationship labelled
"FRIENDS_WITH" between Alice and Bob.
5. Start a write transaction within the session to execute the Cypher queries. The queries are
executed within a single transaction for data consistency.
6. Inside the transaction, execute the createNodeQuery to create a node for Alice and the crea-
teFriendQuery to create a friendship relationship between Alice and Bob.
7. Handle any exceptions that may occur during the transaction and print the stack trace for
debugging.
8. Finally, close the Neo4j driver and release any associated resources.
This code demonstrates how to connect to Neo4j, create nodes, and define relationships between
them using Cypher queries. It provides a basic example of graph creation in Neo4j using Java.

Figure 5 – Visualization On Neo4j browser

12
Project Rapport

CHAPITRE 3 :
The Main Project :

13
Project Rapport

1 The project

Figure 6 – The Project

14
Project Rapport

2 The Proposed Java Code

1 import org . neo4j . driver .*;


2 import org . neo4j . driver . types .*;
3

4 public class Neo4jGraphCreator {


5

6 public static void main ( String [] args ) {


7 // Create a driver to connect to Neo4j
8 Driver driver =
GraphDatabase . driver ( " bolt :// localhost :7687 " ,
AuthTokens . basic ( " neo4j " , " 12345678 " ) ) ;
9

10 // Create a session to interact with the database


11 try ( Session session = driver . session () ) {
12 // Create the nodes
13 Node mark = session . run ( " CREATE ( p : Person { name :
’ Mark ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
14 Node diana = session . run ( " CREATE ( p : Person { name :
’ Diana ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
15 Node application = session . run ( " CREATE ( t : Thing { name :
’ Application ’}) RETURN
t " ) . single () . get ( " t " ) . asNode () ;
16 Node Company2 = session . run ( " CREATE ( c : Company { name :
’ Company2 ’}) RETURN c " ) . single () . get ( " c " ) . asNode () ;
17 Node Company1 = session . run ( " CREATE ( c : Company { name :
’ Company1 ’}) RETURN c " ) . single () . get ( " c " ) . asNode () ;
18 Node Dan = session . run ( " CREATE ( p : Person { name :
’ Dan ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
19 Node Ann = session . run ( " CREATE ( p : Person { name :
’ Ann ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
20 Node Saily = session . run ( " CREATE ( p : Person { name :
’ Saily ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
21 Node John = session . run ( " CREATE ( p : Person { name :
’ John ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
22 Node Jennifer = session . run ( " CREATE ( p : Person { name :
’ Jennifer ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
23 Node joe = session . run ( " CREATE ( p : Person { name :
’ Joe ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
24 Node melissa = session . run ( " CREATE ( p : Person { name :
’ Melissa ’}) RETURN p " ) . single () . get ( " p " ) . asNode () ;
25 Node integration = session . run ( " CREATE ( t : Thing { name :
’ Integration ’}) RETURN
t " ) . single () . get ( " t " ) . asNode () ;
26 Node Data = session . run ( " CREATE ( t : Thing { name : ’ Data
ETL ’}) RETURN t " ) . single () . get ( " t " ) . asNode () ;
27 Node QUERY = session . run ( " CREATE ( t : Thing { name :
’ Query Language ’}) RETURN

15
Project Rapport

t " ) . single () . get ( " t " ) . asNode () ;


28 Node Graphs = session . run ( " CREATE ( t : Thing { name :
’ Graphs ’}) RETURN t " ) . single () . get ( " t " ) . asNode () ;
29 Node Java = session . run ( " CREATE ( t : Thing { name :
’ Java ’}) RETURN t " ) . single () . get ( " t " ) . asNode () ;
30 Node XYZ = session . run ( " CREATE ( c : Company { name :
’ XYZ ’}) RETURN c " ) . single () . get ( " c " ) . asNode () ;
31 Node Neo4j = session . run ( " CREATE ( c : Company { name :
’ Neo4j ’}) RETURN c " ) . single () . get ( " c " ) . asNode () ;
32 Node ABC = session . run ( " CREATE ( c : Company { name :
’ ABC ’}) RETURN c " ) . single () . get ( " c " ) . asNode () ;
33

34 // Create the relationships


35 String createRelationshipQuery1 = " MATCH ( mark : Person
{ name : ’ Mark ’}) , ( Graphs : Thing { name : ’ Graphs ’}) " +
36 " CREATE ( mark ) -[: LIKES ] - >( Graphs ) " ;
37 String createRelationshipQuery2 = " MATCH ( joe : Person
{ name : ’ Joe ’}) , ( mark : Person { name : ’ Mark ’}) " +
38 " CREATE ( joe ) -[: IS_FRIENDS_WITH ] - >( mark ) " ;
39 String createRelationshipQuery3 = " MATCH ( joe : Person
{ name : ’ Joe ’}) , ( diana : Person { name : ’ Diana ’}) " +
40 " CREATE ( joe ) -[: IS_FRIENDS_WITH ] - >( diana ) " ;
41 String createRelationshipQuery4 = " MATCH ( joe : Person
{ name : ’ Joe ’}) , ( QUERY : Thing { name : ’ Query
Language ’}) " +
42 " CREATE ( joe ) -[: LIKES ] - >( QUERY ) " ;
43 String createRelationshipQuery5 = " MATCH ( diana : Person
{ name : ’ Diana ’}) , ( Graphs : Thing { name : ’ Graphs ’}) "
+
44 " CREATE ( diana ) -[: LIKES ] - >( Graphs ) " ;
45 String createRelationshipQuery6 = " MATCH ( diana : Person
{ name : ’ Diana ’}) , ( QUERY : Thing { name : ’ Query
Language ’}) " +
46 " CREATE ( diana ) -[: LIKES ] - >( QUERY ) " ;
47 String createRelationshipQuery7 = " MATCH ( diana : Person
{ name : ’ Diana ’}) , ( Company1 : Company { name :
’ Company1 ’}) " +
48 " CREATE ( diana ) -[: WORKS_FOR ] - >( Company1 ) " ;
49 String createRelationshipQuery8 = " MATCH
( melissa : Person { name : ’ Melissa ’}) ,
( Company2 : Company { name : ’ Company2 ’}) " +
50 " CREATE ( melissa ) -[: WORKS_FOR ] - >( Company2 ) " ;
51 String createRelationshipQuery9 = " MATCH
( melissa : Person { name : ’ Melissa ’}) , ( Data : Thing
{ name : ’ Data ETL ’}) " +
52 " CREATE ( melissa ) -[: LIKES ] - >( Data ) " ;
53 String createRelationshipQuery10 = " MATCH
( melissa : Person { name : ’ Melissa ’}) , ( QUERY : Thing

16
Project Rapport

{ name : ’ Query Language ’}) " +


54 " CREATE ( melissa ) -[: LIKES ] - >( QUERY ) " ;
55 String createRelationshipQuery11 = " MATCH
( melissa : Person { name : ’ Melissa ’}) , ( Graphs : Thing
{ name : ’ Graphs ’}) " +
56 " CREATE ( melissa ) -[: LIKES ] - >( Graphs ) " ;
57 String createRelationshipQuery12 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) ,
( melissa : Person { name : ’ Melissa ’}) " +
58 " CREATE
( Jennifer ) -[: IS_FRIENDS_WITH ] - >( melissa ) " ;
59 String createRelationshipQuery13 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) , ( Ann : Person
{ name : ’ Ann ’}) " +
60 " CREATE ( Jennifer ) -[: IS_FRIENDS_WITH ] - >( Ann ) " ;
61 String createRelationshipQuery14 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) , ( Saily : Person
{ name : ’ Saily ’}) " +
62 " CREATE
( Jennifer ) -[: IS_FRIENDS_WITH ] - >( Saily ) " ;
63 String createRelationshipQuery15 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) , ( John : Person
{ name : ’ John ’}) " +
64 " CREATE ( Jennifer ) -[: IS_FRIENDS_WITH ] - >( John ) " ;
65 String createRelationshipQuery16 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) , ( mark : Person
{ name : ’ Mark ’}) " +
66 " CREATE ( Jennifer ) -[: IS_FRIENDS_WITH ] - >( mark ) " ;
67 String createRelationshipQuery17 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) , ( Graphs : Thing
{ name : ’ Graphs ’}) " +
68 " CREATE ( Jennifer ) -[: LIKES ] - >( Graphs ) " ;
69 String createRelationshipQuery18 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) , ( Java : Thing
{ name : ’ Java ’}) " +
70 " CREATE ( Jennifer ) -[: LIKES ] - >( Java ) " ;
71 String createRelationshipQuery19 = " MATCH
( Jennifer : Person { name : ’ Jennifer ’}) ,
( Neo4j : Company { name : ’ Neo4j ’}) " +
72 " CREATE ( Jennifer ) -[: WORKS_FOR ] - >( Neo4j ) " ;
73 String createRelationshipQuery20 = " MATCH ( John : Person
{ name : ’ John ’}) , ( XYZ : Company { name : ’ XYZ ’}) " +
74 " CREATE ( John ) -[: WORKS_FOR ] - >( XYZ ) " ;
75 String createRelationshipQuery21 = " MATCH ( John : Person
{ name : ’ John ’}) , ( Java : Thing { name : ’ Java ’}) " +
76 " CREATE ( John ) -[: LIKES ] - >( Java ) " ;
77 String createRelationshipQuery22 = " MATCH ( John : Person
{ name : ’ John ’}) , ( Saily : Person { name : ’ Saily ’}) " +

17
Project Rapport

78 " CREATE ( John ) -[: IS_FRIENDS_WITH ] - >( Saily ) " ;


79 String createRelationshipQuery23 = " MATCH ( John : Person
{ name : ’ John ’}) , ( application : Thing { name :
’ Application ’}) " +
80 " CREATE ( John ) -[: LIKES ] - >( application ) " ;
81 String createRelationshipQuery24 = " MATCH
( Saily : Person { name : ’ Saily ’}) , ( XYZ : Company { name :
’ XYZ ’}) " +
82 " CREATE ( Saily ) -[: WORKS_FOR ] - >( XYZ ) " ;
83 String createRelationshipQuery25 = " MATCH
( Saily : Person { name : ’ Saily ’}) , ( integration : Thing
{ name : ’ Integration ’}) " +
84 " CREATE ( Saily ) -[: LIKES ] - >( integration ) " ;
85 String createRelationshipQuery26 = " MATCH ( Ann : Person
{ name : ’ Ann ’}) , ( application : Thing { name :
’ Application ’}) " +
86 " CREATE ( Ann ) -[: LIKES ] - >( application ) " ;
87 String createRelationshipQuery27 = " MATCH ( Ann : Person
{ name : ’ Ann ’}) , ( Dan : Person { name : ’ Dan ’}) " +
88 " CREATE ( Ann ) -[: IS_FRIENDS_WITH ] - >( Dan ) " ;
89 String createRelationshipQuery28 = " MATCH ( Ann : Person
{ name : ’ Ann ’}) , ( ABC : Company { name : ’ ABC ’}) " +
90 " CREATE ( Ann ) -[: WORKS_FOR ] - >( ABC ) " ;
91 String createRelationshipQuery29 = " MATCH ( Dan : Person
{ name : ’ Dan ’}) , ( Data : Thing { name : ’ Data ETL ’}) " +
92 " CREATE ( Dan ) -[: LIKES ] - >( Data ) " ;
93 String createRelationshipQuery30 = " MATCH ( Dan : Person
{ name : ’ Dan ’}) , ( ABC : Company { name : ’ ABC ’}) " +
94 " CREATE ( Dan ) -[: WORKS_FOR ] - >( ABC ) " ;
95 String createRelationshipQuery31 = " MATCH ( Dan : Person
{ name : ’ Dan ’}) , ( integration : Thing { name :
’ Integration ’}) " +
96 " CREATE ( Dan ) -[: LIKES ] - >( integration ) " ;
97

98

99 session . writeTransaction ( tx -> {


100 tx . run ( createRelationshipQuery1 ) ;
101 return null ;
102 }) ;
103 session . writeTransaction ( tx -> {
104 tx . run ( createRelationshipQuery2 ) ;
105 return null ;
106 }) ;
107 session . writeTransaction ( tx -> {
108 tx . run ( createRelationshipQuery3 ) ;
109 return null ;
110 }) ;
111 session . writeTransaction ( tx -> {

18
Project Rapport

112 tx . run ( createRelationshipQuery4 ) ;


113 return null ;
114 }) ;
115 session . writeTransaction ( tx -> {
116 tx . run ( createRelationshipQuery5 ) ;
117 return null ;
118 }) ;
119 session . writeTransaction ( tx -> {
120 tx . run ( createRelationshipQuery6 ) ;
121 return null ;
122 }) ;
123 session . writeTransaction ( tx -> {
124 tx . run ( createRelationshipQuery7 ) ;
125 return null ;
126 }) ;
127 session . writeTransaction ( tx -> {
128 tx . run ( createRelationshipQuery8 ) ;
129 return null ;
130 }) ;
131 session . writeTransaction ( tx -> {
132 tx . run ( createRelationshipQuery9 ) ;
133 return null ;
134 }) ;
135 session . writeTransaction ( tx -> {
136 tx . run ( createRelationshipQuery10 ) ;
137 return null ;
138 }) ;
139 session . writeTransaction ( tx -> {
140 tx . run ( createRelationshipQuery11 ) ;
141 return null ;
142 }) ;
143 session . writeTransaction ( tx -> {
144 tx . run ( createRelationshipQuery12 ) ;
145 return null ;
146 }) ;
147 session . writeTransaction ( tx -> {
148 tx . run ( createRelationshipQuery13 ) ;
149 return null ;
150 }) ;
151 session . writeTransaction ( tx -> {
152 tx . run ( createRelationshipQuery14 ) ;
153 return null ;
154 }) ;
155 session . writeTransaction ( tx -> {
156 tx . run ( createRelationshipQuery15 ) ;
157 return null ;
158 }) ;
159 session . writeTransaction ( tx -> {

19
Project Rapport

160 tx . run ( createRelationshipQuery16 ) ;


161 return null ;
162 }) ;
163 session . writeTransaction ( tx -> {
164 tx . run ( createRelationshipQuery17 ) ;
165 return null ;
166 }) ;
167 session . writeTransaction ( tx -> {
168 tx . run ( createRelationshipQuery18 ) ;
169 return null ;
170 }) ;
171 session . writeTransaction ( tx -> {
172 tx . run ( createRelationshipQuery19 ) ;
173 return null ;
174 }) ;
175 session . writeTransaction ( tx -> {
176 tx . run ( createRelationshipQuery20 ) ;
177 return null ;
178 }) ;
179 session . writeTransaction ( tx -> {
180 tx . run ( createRelationshipQuery21 ) ;
181 return null ;
182 }) ;
183 session . writeTransaction ( tx -> {
184 tx . run ( createRelationshipQuery22 ) ;
185 return null ;
186 }) ;
187 session . writeTransaction ( tx -> {
188 tx . run ( createRelationshipQuery23 ) ;
189 return null ;
190 }) ;
191 session . writeTransaction ( tx -> {
192 tx . run ( createRelationshipQuery24 ) ;
193 return null ;
194 }) ;
195 session . writeTransaction ( tx -> {
196 tx . run ( createRelationshipQuery25 ) ;
197 return null ;
198 }) ;
199 session . writeTransaction ( tx -> {
200 tx . run ( createRelationshipQuery26 ) ;
201 return null ;
202 }) ;
203 session . writeTransaction ( tx -> {
204 tx . run ( createRelationshipQuery27 ) ;
205 return null ;
206 }) ;
207 session . writeTransaction ( tx -> {

20
Project Rapport

208 tx . run ( createRelationshipQuery28 ) ;


209 return null ;
210 }) ;
211 session . writeTransaction ( tx -> {
212 tx . run ( createRelationshipQuery29 ) ;
213 return null ;
214 }) ;
215 session . writeTransaction ( tx -> {
216 tx . run ( createRelationshipQuery30 ) ;
217 return null ;
218 }) ;
219 session . writeTransaction ( tx -> {
220 tx . run ( createRelationshipQuery31 ) ;
221 return null ;
222 }) ;
223

224

225 // Close the driver


226 driver . close () ;
227 }
228 }
229 }
Listing 7 – The Proposed Java code

21
Project Rapport

3 Neo4j Outcome

Figure 7 – Neo4j Outcome

22
Project Rapport

4 Dijkstra Algorithm

1 package org . example ;


2 import org . neo4j . driver .*;
3 import org . neo4j . driver . Record ;
4

5 import java . util .*;


6

7 public class ShortestPathDijkstra {


8

9 public static void main ( String [] args ) {


10 // Create a driver to connect to Neo4j
11 Driver driver =
GraphDatabase . driver ( " neo4j :// localhost :7687 " ,
AuthTokens . basic ( " neo4j " , " 12345678 " ) ) ;
12

13 // Create a session to interact with the database


14 try ( Session session = driver . session () ) {
15 String startPerson = " Jennifer " ; // Replace with the
name of the starting person
16 String endPerson = " Diana " ; // Replace with the name
of the ending person
17

18 List < String > shortestPath = Dijkstra ( session ,


startPerson , endPerson ) ;
19

20 if (! shortestPath . isEmpty () ) {
21 System . out . println ( " Shortest Path : " +
shortestPath ) ;
22 } else {
23 System . out . println ( " No path found between " +
startPerson + " and " + endPerson ) ;
24 }
25 } catch ( Exception e ) {
26 e . printStackTrace () ;
27 } finally {
28 // Close the driver
29 driver . close () ;
30 }
31 }
32

33 private static List < String > Dijkstra ( Session session , String
startPerson , String endPerson ) {
34 String query = " MATCH ( start : Person { name : $startName }) ,
( end : Person { name : $endName }) " +
35 " CALL algo . shortestPath . stream ( start , end ,
’ weightProperty ’, { direction : ’ OUTGOING ’}) " +
36 " YIELD nodeId , cost " +

23
Project Rapport

37 " RETURN algo . asNode ( nodeId ) . name AS nodeName ,


cost " ;
38

39 Map < String , Object > parameters = new HashMap < >() ;
40 parameters . put ( " startName " , startPerson ) ;
41 parameters . put ( " endName " , endPerson ) ;
42

43 List < String > shortestPath = new ArrayList < >() ;


44

45 Result result = session . run ( query , parameters ) ;


46 while ( result . hasNext () ) {
47 Record record = result . next () ;
48 shortestPath . add ( record . get ( " nodeName " ) . asString () ) ;
49 }
50

51 return shortestPath ;
52 }
53 }
Listing 8 – Dijkstra Algorithm

This code it doesn’t consider the bidirectional nature of the "IS_FRIENDS_WITH" relationship,
To account for bidirectional relationships, I can modify your findFriends function to retrieve friends
who are either incoming or outgoing friends. I can modify the query to return both incoming and
outgoing friends like this :
1 private static List < String > findFriends ( Session session ,
String node ) {
2 List < String > friends = new ArrayList < >() ;
3 String query = " MATCH ( p : Person { name :
$nodeName }) -[: IS_FRIENDS_WITH ] -( friend ) RETURN friend . name
AS friend " ;
4 Result result = session . run ( query ,
Collections . singletonMap ( " nodeName " , node ) ) ;
5 while ( result . hasNext () ) {
6 Record record = result . next () ;
7 friends . add ( record . get ( " friend " ) . asString () ) ;}
8 // Now , retrieve incoming friends
9 String incomingQuery = " MATCH ( p : Person { name :
$nodeName }) < -[: IS_FRIENDS_WITH ] -( friend ) RETURN friend . name
AS friend " ;
10 Result incomingResult = session . run ( incomingQuery ,
Collections . singletonMap ( " nodeName " , node ) ) ;
11 while ( incomingResult . hasNext () ) {
12 Record record = incomingResult . next () ;
13 friends . add ( record . get ( " friend " ) . asString () ) ;}
14 return friends ;}
Listing 9 – findFriends

This modification will ensure that I consider both directions of the "IS_FRIENDS_WITH" rela-
tionship when finding friends.

24
Project Rapport

Figure 8 – Dijkstra Graph

Figure 9 – Dijkstra Output

25
Project Rapport

CHAPITRE 4 :
Conclusion :

26
Project Rapport

The successful completion of this project marks significant achievements in the realm of graph
database integration with Java. It provides an essential bridge between the powerful graph database
capabilities of Neo4j and the versatility of the Java programming language. This section summarizes
the main accomplishments and highlights the significance of linking Java and Neo4j for this specific
use case.

1 Main Achievements of the Project


The primary achievements of this project are as follows :
• Seamless Integration : The project demonstrated the successful and seamless integration of
Java with Neo4j. It established a connection to a Neo4j database, allowing for the execution
of Cypher queries from a Java application, resulting in the creation of a graph.
• Graph Creation : The project showcased the creation of a graph in Neo4j using Java.
It encompassed the creation of nodes, relationships, and the definition of graph structure,
providing a practical example of graph database utilization.
• Error Handling : Robust error handling and exception management were implemented to
ensure that the application behaves gracefully when issues occur during graph creation. This
reinforces the reliability of the integration.

2 Significance of Linking Java and Neo4j for the Use Case


The significance of linking Java and Neo4j for this specific use case cannot be understated.
Here’s why this integration is important :
— Practical Application : The integration of Neo4j with Java enables the development of
real-world graph-based applications. In scenarios where data relationships are complex and
vital, such as social networks, recommendation systems, and network analysis, this integration
proves invaluable.
— Educational Value : In an educational context, this project provides a valuable resource for
both teachers and students. It demonstrates the practical application of graph databases and
how Java can be used to interact with them. It equips students with hands-on experience and
teachers with a practical teaching tool.
— Validation of Skills : For the student who created this project, it serves as a comprehensive
validation of skills and knowledge. It showcases proficiency in Java, Neo4j, and the ability to
bridge the gap between theory and practice.
In conclusion, this project exemplifies the successful link between Java and Neo4j, demonstrating
its practical application, educational value, and validation of skills. It underscores the importance
of graph databases in solving real-world problems and the significance of integrating them with a
versatile and widely-used language like Java. The project sets the stage for future developments
and innovations in the field of graph database application development and paves the way for new
possibilities in education and beyond.

27

You might also like