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.
A. Preparation
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.
installing APOC: create a new database, and install the APOC plugin before starting the database.

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.
To check if APOC is functioning correctly, execute the following code:
CALL apoc.import.csv([{fileName: 'file:/people.csv', labels: ['Person']}], [], {})
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.
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:
MATCH (n) DETACH DELETE n
Make use of APOC dynamic label and dynamic relationship loading to import the three csv files and build a graph database.
C. Advanced Cypher and APOC
C.1. Aggregation and Data Profiling in Cypher
C.2. Path Expansion
For more information about APOC path finding, please refer to this APOC documentation:
C.3. List and Pattern Comprehension
The queries below allow you test out integrating pattern comprehension with other clauses (e.g. UNWIND
) and functions (e.g. collect()
). What is UNWIND:
YOUR TURN: Combine the two above into one block of queries to reduce redundant code.
YOUR TURN: Modify the third code chunk and try to list the people with the number of occupation types related to them in desending order. e.g. (if the circle_occupation of Alice is ["Mechanic", "Actor"], the result should be Alice 2)
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.
MATCH (p:Person)-[:LIVES_IN]->(c:City{name:"Perth"})
WITH collect(p.name) AS residents, c, count(p) as count
CALL apoc.create.vNode(residents,{name:residents}) yield node as a
CALL apoc.create.vRelationship(a,"LIVES_IN",{count:count},c) yield rel
RETURN a, c, rel
YOUR TURN, adapt the above code to create such virtual nodes for all cities.
For more information on Virtual Node and Virtual Relationships, please read the APOC documentation:
Last updated