代做COMP 315: Cloud Computing for E-Commerce Lab 3: Neo4j and the Cypher Query Language 2024代写Java程序
- 首页 >> Matlab编程Lab 3: Neo4j and the Cypher Query Language
COMP 315: Cloud Computing for E-Commerce
March 4, 2024
Introduction
Note: Do not attempt to use sudo on the university servers.
This lab is focused onthe Neo4j graph database and the Cypher query language. You’ll learn how to set up Neo4j, create a database, and perform basic operations using Cypher. The lab is best done on your own computer rather than the university servers. There are 3 ways to access Neo4j:
● Using the Neo4j Desktop application
● Using the Neo4j Docker container
● Using the free tier of the Neo4j Aura cloud service..
This tutorial will be following the introductory example ”Try Neo4j with live data” which is provided in the the desktop and Docker container:
1 Movie Graph Guide
The Movie Graph is a mini graph application, containing actors and directors that are related through the movies they have collaborated on.
In this tutorial, you’ll learn how to:
● Load: Insert movie data into the graph.
● Constrain: Create unique node property constraints.
● Index: Index nodes based on their labels.
● Find: Retrieve individual movies and actors.
● Query: Discover related actors and directors.
● Solve: The Bacon Path.
2 Create the Movie Graph
NOTE: This guide assumes that you use an empty graph. If it contains data, see page 9 on how to clean it up.
1. Copy the cypher from lab 3 cypher .txt into the input area of the Neo4j Browser.
2. Run the Cypher code by clicking the Run button.
3. Wait for the operation to finish.
4. Take a look at lab 3 cypher .txt and familiarise yourself with the Cypher code in it.
3 Create Unique Node Property Constraints
Create unique node property constraints to ensure that property values are unique for all nodes with a specific label. Adding the unique constraint, implicitly adds an index on that property.
CREATE CONSTRAINT FOR (n:Movie) REQUIRE (n.title) IS UNIQUE
CREATE CONSTRAINT FOR (n:Person) REQUIRE (n.name) IS UNIQUE You can get help as follows:
● :help help
● :help cypher
● :help create-constraint
4 Index Nodes
Create indexes on one or more properties for all nodes that have a given label. Indexes are used to increase search performance.
CREATE INDEX FOR (m:Movie) ON (m.released)
5 Find Individual Nodes
Run the following query examples. Notice the syntax pattern. Try looking for other movies or actors.
● Find the actor named ”Tom Hanks”:
MATCH (tom:Person {name: "Tom Hanks"}) RETURN tom
● Find the movie with title ”Cloud Atlas”:
MATCH (cloudAtlas:Movie {title: "Cloud Atlas"}) RETURN cloudAtlas
● Find 10 people and return their names:
MATCH (people:Person) RETURN people .name LIMIT 10
● Find movies released in the 1990s and return their titles:
MATCH (nineties:Movie)
WHERE nineties .released >= 1990 AND nineties .released < 2000
RETURN nineties .title
6 Query Patterns
Use the type of the relationship to find patterns within the graph, for example, ACTED IN or DIRECTED. What other relationships exist?
● What movies did Tom Hanks act in?
MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies)
RETURN tom,tomHanksMovies
● Who directed ”Cloud Atlas”?
MATCH (cloudAtlas:Movie {title: "Cloud Atlas"})<-[:DIRECTED]-(directors)
RETURN directors .name
● Who were Tom Hanks’ co-actors?
MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors)
RETURN DISTINCT coActors .name
● How people are related to ”Cloud Atlas”?
MATCH (people:Person)-[relatedTo]-(:Movie {title: "Cloud Atlas"})
RETURN people .name, Type(relatedTo), relatedTo .roles
7 Solve: Six Degrees of Kevin Bacon
You might have heard of the classic ”Six Degrees of Kevin Bacon” . That is simply the shortest path between two nodes, called the ”Bacon Path” .
● Use variable length patterns to find movies and actors up to 4 “hops” away from Kevin Bacon:
MATCH (bacon:Person {name:"Kevin Bacon"})-[*1 . .4]-(hollywood)
RETURN DISTINCT hollywood
● Use the built-in shortestPath() algorithm to find the “Bacon Path” to Meg Ryan:
MATCH p=shortestPath(
(bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"})
)
RETURN p
8 Recommend: New Co-actors for Tom Hanks
Let’s recommend new co-actors for Tom Hanks. A basic recommendation approach is to find connections past an immediate neighborhood that are themselves well connected.
● Extend Tom Hanks co-actors to find co-co-actors who have not worked with Tom Hanks:
MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors), (coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cocoActors)
WHERE NOT (tom)-[:ACTED_IN]->()<-[:ACTED_IN]-(cocoActors) AND tom <> cocoActors
RETURN cocoActors .name AS Recommended, count(*) AS Strength ORDER BY Strength DESC
● Find someone who can introduce Tom Hanks to his potential co-actor, in this case Tom Cruise:
MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),
(coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cruise:Person {name:"Tom Cruise"})
RETURN tom, m, coActors, m2, cruise
9 Clean Up: Remove the Movie Data Set
When you are done experimenting, you can clean up your graph.
NOTE: Nodes cannot be deleted if they have relationships, so you need to detach the nodes to delete them.
● Delete all Movie and Person nodes, and their relationships:
MATCH (n) DETACH DELETE n
● Verify that the Movie Graph has been removed:
MATCH (n) RETURN n