0% found this document useful (0 votes)
9 views16 pages

Aplicatii

Uploaded by

Marian Soryn
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)
9 views16 pages

Aplicatii

Uploaded by

Marian Soryn
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/ 16

Aplicatie 1

Neo4j project for managing person related information.

Create new project

Follow the steps to create a new project in Neo4j.

 Click New button and select Create project option from the
project window.
 Click the edit icon to provide a valid project name.

 Click Add button and select Local DBMS option.


 Provide name, password and version of the DBMS. Click the
Create button to create the project.

 Start the project by clicking on the Start button in the DBMS list.
 Click the Open button to switch to the Neo4j browser window.

Write CQL commands

Let’s begin with the person node creation.

 Execute the following statements to create 4 person nodes.


CREATE (a:Person {name:'Ajith', born: 'Apr 10, 1996', linkedin:'@ajith'}) RETURN a
CREATE (a:Person {name:'Ishani', born: 'Mar 8, 1998', linkedin:'@ishani'}) RETURN a
CREATE (a:Person {name:'Gowri', born: 'Oct 16, 1997', linkedin:'@gowri'}) RETURN a
CREATE (a:Person {name:'Vishnu', born: 'Dec 12, 1994', linkedin:'@codemaker2015'}) RETURN a
 Execute the following statements to create 2 game nodes.

CREATE (a:Sports {name:'Chess'}) RETURN a


CREATE (a:Sports {name:'Cricket'}) RETURN a
 Execute the following statement to create a company node.

CREATE (a:Company {name:'ABC Company', location: "Ernakulam"}) RETURN a


 Execute the following statement to create an institution node.


CREATE (a:Institution {name:'XYZ College', location: "Kochi"}) RETURN a

Let’s list the node details using the MATCH clause.


MATCH (n) RETURN n LIMIT 25
 Get the node details of the person node, ‘Vishnu’, by clicking on
the node.


MATCH (a {name: 'Vishnu'}) RETURN a

Now, I can create relationship between the nodes as shown in the figure
(fig: person management).

 Execute the following statement to create a PLAYS relationship


between person nodes and game nodes.

MATCH (a:Person), (b:Sports) WHERE (a.name = 'Vishnu' OR a.name = 'Ajith') AND


b.name = 'Cricket' CREATE (a)-[r:PLAYS]->(b)
MATCH (a:Person), (b:Sports) WHERE (a.name = 'Vishnu' OR a.name = 'Gowri' OR a.name
= 'Ishani') AND b.name = 'Chess' CREATE (a)-[r:PLAYS]->(b)
 Create a relationship between institution and person nodes using
the following statement.

MATCH (a:Person), (b:Institution) WHERE (a.name = 'Vishnu' OR a.name = 'Ishani') AND


b.name = 'XYZ College' CREATE (a)-[r:ACADEMICS]->(b)

 Create a relationship between company and person nodes using


the following statement.

MATCH (a:Person), (b:Company) WHERE (a.name = 'Vishnu' OR a.name = 'Gowri') AND
b.name = 'ABC Company' CREATE (a)-[r:WORKS]->(b)

 View the nodes and relationships using the following statement.

MATCH (n) RETURN n

 Delete the PLAYS relationship between Ajith and Cricket using


the following command.


MATCH (n {name: 'Ajith'})-[r:PLAYS]->() DELETE r
 Delete a node with property name as Ajith.

MATCH (n {name: 'Ajith'}) DELETE n


Aplicatie 2

Creating and switching the current database

To create a new database, you should use the following command:

1 create database <database name>

To switch to the current database, you should use the following command:

1 :use <database name>

To switch to the system database, you should use:

1 :use system

Creating Nodes and Edges

The next step is to add Nodes (entities). If we look at the code shown in the second slide. We
can see that there are two types of commands:

1. Creating Nodes
2. Creating Edges

The following lines of code are an example of creating nodes:

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real


1
World'})
2
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
3
CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})

In the previous code lines, the first line is to add a movie node, and the others are for persons.
From those lines, we can see that the node creation command syntax is:

CREATE (<Node name>:<Node type> {<Property 1 name>:<property 1 value>, <Property 2


1
name>:<Property 2 value>, …})
The following lines of codes are an example of creating edges (relations):

1 CREATE

2 (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),

3 (Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),

4 (Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),

5 (Hugo)-[:ACTED_IN {roles:['Agent Smith']}]->(TheMatrix),

6 (LillyW)-[:DIRECTED]->(TheMatrix),

7 (LanaW)-[:DIRECTED]->(TheMatrix),

8 (JoelS)-[:PRODUCED]->(TheMatrix)

From those lines, we can see that the node creation command syntax is:

CREATE (<Node 1 label>)-[:<Edge label> {<Edge property 1>:<Edge property 1 value>, …,


1
<Edge property n>:<Edge property n value>}]->(<Node 2 label)

After running this command we the created graph will be visualized as shown below:
Querying graphs

The rest of the Movie database tutorial contains different examples of data retrieval operations
using GraphQL. Note that the MATCH command is used to query data. As an example, to get
all movies that Tom Hanks acted in:

1 MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies)

2 RETURN tom,tomHanksMovies

That the result can be visualized as a graph, text, table, or code:

You might also like