Piece
Composer
Type
Key
User
(:Composer)-[:COMPOSED {role:"some role"}]->(:Movie)
(:Piece)-[:HAS_TYPE]->(:TYPE)
(:Piece)-[:HAS_KEY]->(:Key)
(:User)-[:RATED {rating:"1-5"}]->(:Piece)
This is the recommended way and should work similarly on Windows.
- Run the Neo4j Docker Container
docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/import:/var/lib/neo4j/import \
--name neo4j \
neo4j
- Copy the csv files into the import directory:
cp csv/*.csv $HOME/neo4j/import
- Initialize the database:
docker exec -i neo4j bin/cypher-shell -u neo4j -p neo < ./setup.cql
As an alternative to Docker you can also install Neo4j locally
- Download Neo4j Community Edition: .tar Version
- Set your
NEO4J_HOME
variable:export NEO4J_HOME=/path/to/neo4j-community
- From this project's root directory, run the import script:
docker exec -i my-neo4j bin/neo4j-admin import \
--nodes:Piece /var/lib/neo4j/import/piece_node.csv \
--nodes:User /var/lib/neo4j/import/users_node.csv \
--nodes:Type /var/lib/neo4j/import/type_node.csv \
--relationships:TYPE "/var/lib/neo4j/import/type_rels.csv" \
--delimiter ";" --array-delimiter "|" --id-type INTEGER
$NEO4J_HOME/bin/neo4j-import --into $NEO4J_HOME/data/databases/graph.db --nodes:Person csv/person_node.csv --nodes:Movie csv/movie_node.csv --nodes:Genre csv/genre_node.csv --nodes:Keyword csv/keyword_node.csv --relationships:ACTED_IN csv/acted_in_rels.csv --relationships:DIRECTED csv/directed_rels.csv --relationships:HAS_GENRE csv/has_genre_rels.csv --relationships:HAS_KEYWORD csv/has_keyword_rels.csv --relationships:PRODUCED csv/produced_rels.csv --relationships:WRITER_OF csv/writer_of_rels.csv --delimiter ";" --array-delimiter "|" --id-type INTEGER
If you see Input error: Directory 'neo4j-community-3.0.3/data/databases/graph.db' already contains a database
, delete the graph.db
directory and try again.
- Add constraints to your database:
$NEO4J_HOME/bin/neo4j-shell < setup.cql -path $NEO4J_HOME/databases/graph.db
- Start the database:
$NEO4J_HOME/bin/neo4j start
Let's start with taking a look at the current state of the database. Therefore open the Neo4j browser.
Take a look at all currently existing Pieces in the database, by entering this command:
MATCH (n:Piece) RETURN n;
We can also find out which Pieces are Quartets:
MATCH (n:Piece)-[:HAS_TYPE]->(:Type {name: 'Quartet'}) RETURN n;
Moving on we can also create a new node ourselves. Let's add Mozart
as a Composer
to our database:
CREATE (n:Composer {name:'Mozart'}) RETURN n;
As all the Pieces in the database where composed by Mozart, connect them with a COMPOSED
relationship:
MATCH (m:Composer {name:"Mozart"}), (p:Piece)
CREATE (m)-[:COMPOSED]->(p)
As described one of the strength of Graph Databases is the traversal over its paths. This can be used for recommendation systems, for example.
To set this up let's create a few users:
LOAD CSV WITH HEADERS FROM "file:///users_node.csv" AS r FIELDTERMINATOR ';'
CREATE (p:User {
id: toInteger(r.`id:ID(User)`),
name: r.name
});
Also create a few ratings:
MATCH (u:User), (p:Piece)
WITH u, p
WHERE rand() < 0.1
CREATE (u)-[:RATED]->(p)
and assign rating scores:
MATCH (:User)-[r:RATED]->(:Piece)
SET r.rating = floor(rand()*6)
Your task is now to develop an approach to recommend Pieces to a user, based on all information currently stored in the database.