SlideShare a Scribd company logo
Neo4J
Coursework (CMM534)
Sammy Hegab (9907223)
Tasks 1: Preparing, Creating Nodes and Relationships
Creating and dropping constraints
CREATE CONSTRAINT ON (a:Actor) ASSERT a.actorID IS UNIQUE;
CREATE CONSTRAINT ON (a:Actor) ASSERT a.actorName IS UNIQUE;
DROP CONSTRAINT ON (a:Actor) ASSERT a.actorName IS UNIQUE;
CREATE CONSTRAINT ON (d:Director) ASSERT d.directorID IS UNIQUE;
CREATE CONSTRAINT ON (d:Director) ASSERT d.directorName IS UNIQUE;
CREATE CONSTRAINT ON (m:Movie) ASSERT m.movieID IS UNIQUE;
CREATE CONSTRAINT ON (m:Movie) ASSERT m.title IS UNIQUE;
CREATE CONSTRAINT ON (m:Movie) ASSERT m.imdbID IS UNIQUE;
1. Statement to create the actor nodes.
//View Actors ID
LOAD CSV WITH HEADERS FROM
"file:///home/sam/Desktop/csv/actors.csv" AS line WITH line
,SPLIT(line.actorID,'-') AS actorID
LIMIT 1
RETURN actorID[1]
// Create Actors
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/actors.csv" AS line WITH
line
,SPLIT(line.actorID,'-') AS aID
OPTIONAL MATCH(actors:Actors{actorID: aID[1]})
WHERE actors IS NULL
CREATE (actor:Actors {actorID: aID[1], actorName: line.actorName})
// Create MovieActor (actorID,ranking,movieID)
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movie_actors.csv" AS row
CREATE (movieActor:MovieActor {actorID: row.actorID, ranking: toInt(row.ranking), movieID:
toInt(row.movieID)});
2. Statement to create the movie
// Create movies
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movies.csv" AS row
CREATE (:Movie {movieID: toInt(row.movieID), title: row.title, imdbID: toint(row.imdbID),
imdbPictureURL: row.imdbPictureURL, year: toInt(row.year), rtAllCriticsRating:
row.rtAllCriticsRating});
3. Statement to create the director
// Create MovieDirector (movieID,directorID)
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movie_directors.csv" AS
row
CREATE (movieDirector:MovieDirector {movieID: toInt(row.movieID), directorID:
row.directorID});
//View Director ID
LOAD CSV WITH HEADERS FROM
"file:///home/sam/Desktop/csv/directors.csv" AS line WITH line
,SPLIT(line.directorID,'-') AS directorID
LIMIT 1
RETURN directorID[1]
// Create Directors
LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/directors.csv" AS line WITH
line
,SPLIT(line.directorID,'-') AS dID
OPTIONAL MATCH(director:Director{directorID: dID[1]})
WHERE director IS NULL
CREATE (:Director {directorID: dID[1], directorName: line.directorName})
4. Statement to create the genres nodes
// Create Genres
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/genres.csv" AS row
CREATE (:Genres {genreName: row.genreName});
// Create MovieGenre
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movie_genres.csv" AS row
MERGE (:MovieGenre {movieID: toInt(row.movieID), genreName: row.genreName});
5. Statement to create the ‘PLAYS_IN’ relationship between actors and movies to represent
the movies in which each actor plays a role.
//adding actor name from actor table to movie actor node
MATCH (actor:Actor),(movieActor: MovieActor)
WHERE movieActor.actorID IS NOT NULL AND movieActor.actorID CONTAINS actor.ActorID
SET movieActor += { actorName: actor.actorName }
return movieActor.actorID LIMIT 5;
// Create the 'PLAYS_IN' relationship between actors and movies
// to represent the movies in which each actor plays a role.
MATCH (actor: Actor),(movie: Movie)
WHERE actor.movieID = movie.movieID
CREATE (actor)-[r:PLAYS_IN]->(movie)
RETURN r
6. Statement to create the ‘DIRECTS’ relationship between directors and movies to represent
the movies directed by each director.
MATCH (movieDirector: MovieDirector),(director: Director)
WHERE movieDirector.directorID CONTAINS director.directorID
SET movieDirector += { directorName: director.directorName }
return movieDirector.directorID, movieDirector.directorName;
MATCH (movieDirector: MovieDirector),(movie: Movie)
WHERE
movie.movieID = movieDirector.movieID and movieDirector.directorName IS NOT NULL
RETURN movieDirector.directorID,movieDirector.directorName, movie.title
LIMIT 25;
MATCH (movieDirector: MovieDirector),(movie: Movie)
WHERE
movie.movieID = movieDirector.movieID and movieDirector.directorName IS NOT NULL
CREATE (movieDirector)-[r:DIRECTS]->(movie)
RETURN movieDirector.directorID,movieDirector.directorName, movie.title
LIMIT 25;
7. Statement to create the ‘CATEGORY’ relationship between genres and movies to represent
the genre of each movie.
// Create the 'CATEGORY' relationship between movie and genre
MATCH (movie: Movie),(movieGenre: MovieGenre)
WHERE movieGenre.movieID = movie.movieID
CREATE (movie)-[r:CATEGORY]->(movieGenre)
RETURN movieGenre.movieID,movie.title,movieGenre.genreName
LIMIT 25;
Task 2: Querying the Graph
8. All actors limited to 20 nodes:
MATCH (actor:Actors) RETURN actor LIMIT 20
9. The top 10 actors sorted by name.
MATCH (actor:Actors) RETURN actor order by actor.actorName LIMIT 10
10. Show only the actors whose names end with “c”. Provide the full list in grid
mode.
MATCH (actor:Actors)
WHERE actor.actorName =~ '.*c'
RETURN actor
Task 3: Discovering details
Example of relationship
$MATCH ()-[r:PLAYS_IN]->() RETURN r LIMIT 25
11. The name of the actor that has participated in the largest number of
movies, according to the database, and list those movies.
This query gives us a match between movieActor table and movie table
MATCH (actor:Actors),(movieActor:MovieActor)
WHERE actor.actorID CONTAINS movieActor.actorID
RETURN actor.actorID, actor.actorName LIMIT 25;
This query should list the actors participated in movies movies
MATCH (actor:Actors),(movieActor:MovieActor),(movie:Movie)
WHERE actor.actorID CONTAINS movieActor.actorID AND
movieActor.MovieID CONTAINS movie.movieID
MATCH ()-[:PLAYS_IN]->()
RETURN actor.actorID, actor.actorName, count(*)
ORDER BY count(*) DESC LIMIT 100;
Second attempt query
MATCH (a:Actors),(ma:MovieActor)
WHERE a.actorID CONTAINS ma.actorID
MATCH (actor)-[:PLAYS_IN]->(movie)
RETURN actor.actorID, a.actorName,movie.title, count(*)
ORDER BY count(*) DESC LIMIT 100;
Third attempt without actor names
MATCH (actor)-[:PLAYS_IN]->(movie)
RETURN actor.actorID, actor.actorName,movie.title, count(*)
ORDER BY count(*) DESC LIMIT 100;
12. The name of the director that has directed the largest number of
movies, according to the database, and list those movies.
MATCH (director)-[:DIRECTS]->(movie)
RETURN director.directorID, director.directorName,movie.title, count(*)
ORDER BY count(*) DESC LIMIT 100;

More Related Content

Similar to Neo4J Coursework - Sammy Hegab 22-04-2016

Imdb import presentation
Imdb import presentationImdb import presentation
Imdb import presentation
Joshua Bae
 
Formai sec
Formai secFormai sec
Formai sec
ttasi86
 
Introduction to Cypher
Introduction to Cypher Introduction to Cypher
Introduction to Cypher
Neo4j
 
Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...
Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...
Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...
Chris Grabosky
 
Introduction to Cypher.pptx
Introduction to Cypher.pptxIntroduction to Cypher.pptx
Introduction to Cypher.pptx
tuanpham21012003
 
EdgeQL — A primer
EdgeQL — A primerEdgeQL — A primer
EdgeQL — A primer
EdgeDB
 
SQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer CentreSQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer Centre
jatin batra
 
FP in JS-Land
FP in JS-LandFP in JS-Land
FP in JS-Land
Robert Pearce
 
Modify Assignment 5 toReplace the formatted output method (toStri.docx
Modify Assignment 5 toReplace the formatted output method (toStri.docxModify Assignment 5 toReplace the formatted output method (toStri.docx
Modify Assignment 5 toReplace the formatted output method (toStri.docx
adelaidefarmer322
 
struct Movie stdstring title Movie.pdf
struct Movie      stdstring title               Movie.pdfstruct Movie      stdstring title               Movie.pdf
struct Movie stdstring title Movie.pdf
kiraan007
 
Neo4j
Neo4jNeo4j
RSpec best practice - avoid using before and let
RSpec best practice - avoid using before and letRSpec best practice - avoid using before and let
RSpec best practice - avoid using before and let
Bruce Li
 
ActiveResource & REST
ActiveResource & RESTActiveResource & REST
ActiveResource & REST
Robbert
 
20101217 mtg
20101217 mtg20101217 mtg
20101217 mtg
riamaehb
 
Graph Database Query Languages
Graph Database Query LanguagesGraph Database Query Languages
Graph Database Query Languages
Jay Coskey
 
How to Import JSON Using Cypher and APOC
How to Import JSON Using Cypher and APOCHow to Import JSON Using Cypher and APOC
How to Import JSON Using Cypher and APOC
Neo4j
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 

Similar to Neo4J Coursework - Sammy Hegab 22-04-2016 (17)

Imdb import presentation
Imdb import presentationImdb import presentation
Imdb import presentation
 
Formai sec
Formai secFormai sec
Formai sec
 
Introduction to Cypher
Introduction to Cypher Introduction to Cypher
Introduction to Cypher
 
Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...
Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...
Microsoft cloud workshop - automated cloud service for MongoDB on Microsoft A...
 
Introduction to Cypher.pptx
Introduction to Cypher.pptxIntroduction to Cypher.pptx
Introduction to Cypher.pptx
 
EdgeQL — A primer
EdgeQL — A primerEdgeQL — A primer
EdgeQL — A primer
 
SQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer CentreSQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer Centre
 
FP in JS-Land
FP in JS-LandFP in JS-Land
FP in JS-Land
 
Modify Assignment 5 toReplace the formatted output method (toStri.docx
Modify Assignment 5 toReplace the formatted output method (toStri.docxModify Assignment 5 toReplace the formatted output method (toStri.docx
Modify Assignment 5 toReplace the formatted output method (toStri.docx
 
struct Movie stdstring title Movie.pdf
struct Movie      stdstring title               Movie.pdfstruct Movie      stdstring title               Movie.pdf
struct Movie stdstring title Movie.pdf
 
Neo4j
Neo4jNeo4j
Neo4j
 
RSpec best practice - avoid using before and let
RSpec best practice - avoid using before and letRSpec best practice - avoid using before and let
RSpec best practice - avoid using before and let
 
ActiveResource & REST
ActiveResource & RESTActiveResource & REST
ActiveResource & REST
 
20101217 mtg
20101217 mtg20101217 mtg
20101217 mtg
 
Graph Database Query Languages
Graph Database Query LanguagesGraph Database Query Languages
Graph Database Query Languages
 
How to Import JSON Using Cypher and APOC
How to Import JSON Using Cypher and APOCHow to Import JSON Using Cypher and APOC
How to Import JSON Using Cypher and APOC
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
 

Neo4J Coursework - Sammy Hegab 22-04-2016

  • 1. Neo4J Coursework (CMM534) Sammy Hegab (9907223) Tasks 1: Preparing, Creating Nodes and Relationships Creating and dropping constraints CREATE CONSTRAINT ON (a:Actor) ASSERT a.actorID IS UNIQUE; CREATE CONSTRAINT ON (a:Actor) ASSERT a.actorName IS UNIQUE; DROP CONSTRAINT ON (a:Actor) ASSERT a.actorName IS UNIQUE; CREATE CONSTRAINT ON (d:Director) ASSERT d.directorID IS UNIQUE; CREATE CONSTRAINT ON (d:Director) ASSERT d.directorName IS UNIQUE; CREATE CONSTRAINT ON (m:Movie) ASSERT m.movieID IS UNIQUE; CREATE CONSTRAINT ON (m:Movie) ASSERT m.title IS UNIQUE; CREATE CONSTRAINT ON (m:Movie) ASSERT m.imdbID IS UNIQUE; 1. Statement to create the actor nodes. //View Actors ID LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/actors.csv" AS line WITH line ,SPLIT(line.actorID,'-') AS actorID LIMIT 1 RETURN actorID[1] // Create Actors USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/actors.csv" AS line WITH line ,SPLIT(line.actorID,'-') AS aID OPTIONAL MATCH(actors:Actors{actorID: aID[1]}) WHERE actors IS NULL CREATE (actor:Actors {actorID: aID[1], actorName: line.actorName}) // Create MovieActor (actorID,ranking,movieID) USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movie_actors.csv" AS row CREATE (movieActor:MovieActor {actorID: row.actorID, ranking: toInt(row.ranking), movieID: toInt(row.movieID)}); 2. Statement to create the movie // Create movies USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movies.csv" AS row CREATE (:Movie {movieID: toInt(row.movieID), title: row.title, imdbID: toint(row.imdbID), imdbPictureURL: row.imdbPictureURL, year: toInt(row.year), rtAllCriticsRating: row.rtAllCriticsRating});
  • 2. 3. Statement to create the director // Create MovieDirector (movieID,directorID) USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movie_directors.csv" AS row CREATE (movieDirector:MovieDirector {movieID: toInt(row.movieID), directorID: row.directorID}); //View Director ID LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/directors.csv" AS line WITH line ,SPLIT(line.directorID,'-') AS directorID LIMIT 1 RETURN directorID[1] // Create Directors LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/directors.csv" AS line WITH line ,SPLIT(line.directorID,'-') AS dID OPTIONAL MATCH(director:Director{directorID: dID[1]}) WHERE director IS NULL CREATE (:Director {directorID: dID[1], directorName: line.directorName}) 4. Statement to create the genres nodes // Create Genres USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/genres.csv" AS row CREATE (:Genres {genreName: row.genreName}); // Create MovieGenre USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///home/sam/Desktop/csv/movie_genres.csv" AS row MERGE (:MovieGenre {movieID: toInt(row.movieID), genreName: row.genreName}); 5. Statement to create the ‘PLAYS_IN’ relationship between actors and movies to represent the movies in which each actor plays a role. //adding actor name from actor table to movie actor node MATCH (actor:Actor),(movieActor: MovieActor) WHERE movieActor.actorID IS NOT NULL AND movieActor.actorID CONTAINS actor.ActorID SET movieActor += { actorName: actor.actorName } return movieActor.actorID LIMIT 5; // Create the 'PLAYS_IN' relationship between actors and movies // to represent the movies in which each actor plays a role. MATCH (actor: Actor),(movie: Movie) WHERE actor.movieID = movie.movieID CREATE (actor)-[r:PLAYS_IN]->(movie) RETURN r
  • 3. 6. Statement to create the ‘DIRECTS’ relationship between directors and movies to represent the movies directed by each director. MATCH (movieDirector: MovieDirector),(director: Director) WHERE movieDirector.directorID CONTAINS director.directorID SET movieDirector += { directorName: director.directorName } return movieDirector.directorID, movieDirector.directorName; MATCH (movieDirector: MovieDirector),(movie: Movie) WHERE movie.movieID = movieDirector.movieID and movieDirector.directorName IS NOT NULL RETURN movieDirector.directorID,movieDirector.directorName, movie.title LIMIT 25; MATCH (movieDirector: MovieDirector),(movie: Movie) WHERE movie.movieID = movieDirector.movieID and movieDirector.directorName IS NOT NULL CREATE (movieDirector)-[r:DIRECTS]->(movie) RETURN movieDirector.directorID,movieDirector.directorName, movie.title LIMIT 25; 7. Statement to create the ‘CATEGORY’ relationship between genres and movies to represent the genre of each movie. // Create the 'CATEGORY' relationship between movie and genre MATCH (movie: Movie),(movieGenre: MovieGenre) WHERE movieGenre.movieID = movie.movieID CREATE (movie)-[r:CATEGORY]->(movieGenre) RETURN movieGenre.movieID,movie.title,movieGenre.genreName LIMIT 25; Task 2: Querying the Graph 8. All actors limited to 20 nodes: MATCH (actor:Actors) RETURN actor LIMIT 20
  • 4. 9. The top 10 actors sorted by name. MATCH (actor:Actors) RETURN actor order by actor.actorName LIMIT 10 10. Show only the actors whose names end with “c”. Provide the full list in grid mode. MATCH (actor:Actors) WHERE actor.actorName =~ '.*c' RETURN actor
  • 5. Task 3: Discovering details Example of relationship $MATCH ()-[r:PLAYS_IN]->() RETURN r LIMIT 25 11. The name of the actor that has participated in the largest number of movies, according to the database, and list those movies. This query gives us a match between movieActor table and movie table MATCH (actor:Actors),(movieActor:MovieActor) WHERE actor.actorID CONTAINS movieActor.actorID RETURN actor.actorID, actor.actorName LIMIT 25;
  • 6. This query should list the actors participated in movies movies MATCH (actor:Actors),(movieActor:MovieActor),(movie:Movie) WHERE actor.actorID CONTAINS movieActor.actorID AND movieActor.MovieID CONTAINS movie.movieID MATCH ()-[:PLAYS_IN]->() RETURN actor.actorID, actor.actorName, count(*) ORDER BY count(*) DESC LIMIT 100; Second attempt query MATCH (a:Actors),(ma:MovieActor) WHERE a.actorID CONTAINS ma.actorID MATCH (actor)-[:PLAYS_IN]->(movie) RETURN actor.actorID, a.actorName,movie.title, count(*) ORDER BY count(*) DESC LIMIT 100; Third attempt without actor names MATCH (actor)-[:PLAYS_IN]->(movie) RETURN actor.actorID, actor.actorName,movie.title, count(*) ORDER BY count(*) DESC LIMIT 100;
  • 7. 12. The name of the director that has directed the largest number of movies, according to the database, and list those movies. MATCH (director)-[:DIRECTS]->(movie) RETURN director.directorID, director.directorName,movie.title, count(*) ORDER BY count(*) DESC LIMIT 100;