Week 11 - Advanced Cypher and APOC

This week's lab focuses on practicing advanced cipher features and using the APOC procedures and functions introduced in the lecture. APOC is not mandatory for Project 2 due to the lack of support for APOC in cloud Neo4j, and queries without APOC can still effectively address Project 2 questions. However, if you choose to incorporate APOC into your Project 2, you're more than welcome to do so.

In this lab, the solutions are deliberately hidden inside the expandable blocks. You are strongly encouraged to attempt the queries yourself first before expanding to check the sample answers.

A. Preparation

  1. downloading the dataset: The dataset is the people-city dataset we used in the lecture demo, about relationships between people and cities people live in.

47KB
Open
Week 11 Lab Dataset
  1. installing APOC: create a new database, and install the APOC plugin before starting the database.

Note: After installing APOC, it is necessary to restart your database.

  1. importing CSVs: place the CSV files in the import directory of the database. If you do not know how to do it, refer to Week 9 - Import data from a relational database into Neo4j for instructions.

  2. To check if APOC is functioning correctly, execute the following code:

If the code runs without errors, proceed to the next step. If you encounter the error mentions "apoc.conf" settings, there are TWO solutions.

Solution 1 (Note: This solution only works for a database with version 4.xx.xx)

a. go to settings:

b. use "Ctrl+F" to search for "apoc," then add "apoc.import.file.enabled=true" in the indicated location. Click "Apply" and then restart your database.

c. Execute the above code again; you should now be able to use APOC.

Solution 2 (Note: This solution works for a database with version 4.xx.xx AND 5.xx.xx)

a. Download the apoc.conf file.

29B
Open

b. Open the conf folder.

c. Move the downloaded apoc.conf file to the conf folder.

d. Restart the database.

B. Populating the Graph Database

Let's clear your database first:

Make use of APOC dynamic label and dynamic relationship loading to import the three csv files and build a graph database.

Step 1: Import people and set a static label Person as well as dynamic label according to Occupation (people.csv)
Step 2: Dynamically load relationship between people using APOC (relationships.csv)
Step 3: Create cities as nodes (promoting a column as nodes) and establish relationships with people (cities.csv)

C. Advanced Cypher and APOC

C.1. Aggregation and Data Profiling in Cypher

List the number of followers
List the name of followers
FOAF - Find Friend of A Friend for a person

C.2. Path Expansion

Path Expansion in Cypher - note the syntax that starts with a * in pattern matching
Path Expander in APOC
Find the Mechanic that "Kellsie" KNOWS or who KNOWS "Kellsie" (no specified direction) who are one or two hops away
Find people "Kellsie" FOLLOWS and KNOWS as well as those who KNOWS "Kellsie" that are 1 or 2 hops away.

For more information about APOC path finding, please refer to this APOC documentation:

C.3. List and Pattern Comprehension

Use pattern comprehension to find the average, minimum and maximum number of relationships of all nodes.

The queries below allow you test out integrating pattern comprehension with other clauses (e.g. UNWIND) and functions (e.g. collect()). What is UNWIND:

Modify the above query to add the set of occupations of one's relationship circles.

C.4. Virtual Relationships and Virtual Nodes (Graph Projections)

Virtual Nodes and Relationships do not exist in the graph, they are only returned to the UI/user for representing a graph projection. They can be visualised or processed. All virtual nodes and relationships have negative id’s.

For example, the following code creates a virtual node for all people living in Perth and created a virtual relationship between this virtual node with the city node Perth. Note how we can add the population count as a property when creating the virtual relationship.

For more information on Virtual Node and Virtual Relationships, please read the APOC documentation:

Last updated