-
Notifications
You must be signed in to change notification settings - Fork 216
/
setupCypherCommands.txt
152 lines (112 loc) · 5.5 KB
/
setupCypherCommands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Craig Miller
// 01/10/2017
// File : UploadAllCsvFiles.cql
// Project: neo4j-movies-template
// Model : 3 nodes: Person, Movie, Genre
// 6 relations
// Note : 1. use CONSTRAINT to handle missing (also creates index)
// 2. small files so no need to chunk (USING PERIODIC COMMIT 1000)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Neo4j 3.1.0 Enterprise server is restricted, by default, to import
// from the %NEO4J_HOME%/import directory, so copy all your csv file
// to that directory before running this script
// ==================================================================
// clear the existing database (a better approach would be to delete
// the %NEO4J_HOME%/data/database/graph.db file)
MATCH (n)
WITH n LIMIT 10000
OPTIONAL MATCH (n)-[r]->()
DELETE n,r;
// ==================================================================
// test
LOAD CSV WITH HEADERS FROM "file:///person_node.csv" AS r FIELDTERMINATOR ';'
WITH r LIMIT 10 WHERE r.`id:ID(Person)` IS NOT NULL
RETURN r.`id:ID(Person)`, r.name, r.`born:int`, r.poster_image
// test property array
LOAD CSV WITH HEADERS FROM "file:///acted_in_rels.csv" AS r FIELDTERMINATOR ';'
WITH r LIMIT 10
RETURN r.`:START_ID(Person)`, r.`:END_ID(Movie)`, SPLIT(r.role, '/')
// ==================================================================
// Upload nodes
// ------------------------------------------------------
CREATE CONSTRAINT ON (g:Genre) ASSERT g.id IS UNIQUE;
// Added 1 constraint, statement completed in 127 ms
LOAD CSV WITH HEADERS FROM "file:///genre_node.csv" AS r FIELDTERMINATOR ';'
CREATE (g:Genre {
id: toInteger(r.`id:ID(Genre)`),
name: r.name
});
// Added 19 labels, created 19 nodes, set 38 properties, statement completed in 202 ms.
// ------------------------------------------------------
CREATE CONSTRAINT ON (k:Keyword) ASSERT k.id IS UNIQUE;
// Added 1 constraint, statement completed in 35 ms.
LOAD CSV WITH HEADERS FROM "file:///keyword_node.csv" AS r FIELDTERMINATOR ';'
CREATE (k:Keyword {
id: toInteger(r.`id:ID(Keyword)`),
name: r.name
});
// Added 3253 labels, created 3253 nodes, set 6506 properties, statement completed in 356 ms.
// ------------------------------------------------------
CREATE CONSTRAINT ON (m:Movie) ASSERT m.id IS UNIQUE;
// Added 1 constraint, statement completed in 44 ms.
LOAD CSV WITH HEADERS FROM "file:///movie_node.csv" AS r FIELDTERMINATOR ';'
CREATE (m:Movie {
id: toInteger(r.`id:ID(Movie)`),
title: r.title,
tagline: r.tagline,
summary: r.summary,
poster_image: r.poster_image,
duration: toInteger(r.`duration:int`),
rated: r.rated
});
// Added 54 labels, created 54 nodes, set 378 properties, statement completed in 55 ms.
// ------------------------------------------------------
CREATE CONSTRAINT ON (p:Person) ASSERT p.id IS UNIQUE;
// Added 1 constraint, statement completed in 38 ms.
LOAD CSV WITH HEADERS FROM "file:///person_node.csv" AS r FIELDTERMINATOR ';'
CREATE (p:Person {
id: toInteger(r.`id:ID(Person)`),
name: r.name,
born: toInteger(r.`born:int`),
poster_image: r.poster_image
});
// Added 665 labels, created 665 nodes, set 2660 properties, statement completed in 54 ms.
// ------------------------------------------------------
// Movie-[:HAS_GENRE]->Genre
LOAD CSV WITH HEADERS FROM "file:///has_genre_rels.csv" AS r FIELDTERMINATOR ';'
MATCH (m:Movie {id: toInteger(r.`:START_ID(Movie)`)}), (g:Genre {id: toInteger(r.`:END_ID(Genre)`)})
CREATE (m)-[:HAS_GENRE]->(g);
// Created 152 relationships, statement completed in 198 ms.
// ------------------------------------------------------
// Movie-[HAS_KEYWORD]->Keyword
LOAD CSV WITH HEADERS FROM "file:///has_keyword_rels.csv" AS r FIELDTERMINATOR ';'
MATCH (m:Movie {id: toInteger(r.`:START_ID(Movie)`)}), (k:Keyword {id: toInteger(r.`:END_ID(Keyword)`)})
CREATE (m)-[:HAS_KEYWORD]->(k);
// Created 5118 relationships, statement completed in 607 ms.
// ------------------------------------------------------
// Person-[WRITER_OF]->Movie
LOAD CSV WITH HEADERS FROM "file:///writer_of_rels.csv" AS r FIELDTERMINATOR ';'
MATCH (p:Person {id: toInteger(r.`:START_ID(Person)`)}), (m:Movie {id: toInteger(r.`:END_ID(Movie)`)})
CREATE (p)-[:WRITER_OF]->(m);
// Created 107 relationships, statement completed in 18 ms.
// ------------------------------------------------------
// Person-[PRODUCED]->Movie
LOAD CSV WITH HEADERS FROM "file:///produced_rels.csv" AS r FIELDTERMINATOR ';'
MATCH (p:Person {id: toInteger(r.`:START_ID(Person)`)}), (m:Movie {id: toInteger(r.`:END_ID(Movie)`)})
CREATE (p)-[:PRODUCED]->(m);
// Created 60 relationships, statement completed in 13 ms.
// ------------------------------------------------------
// Person-[DIRECTED]->Movie
LOAD CSV WITH HEADERS FROM "file:///directed_rels.csv" AS r FIELDTERMINATOR ';'
MATCH (p:Person {id: toInteger(r.`:START_ID(Person)`)}), (m:Movie {id: toInteger(r.`:END_ID(Movie)`)})
CREATE (p)-[:DIRECTED]->(m);
// Created 65 relationships, statement completed in 111 ms.
// ------------------------------------------------------
// Person-[ACTED_IN]->Movie
// Note: relationship property 'role' is an array
LOAD CSV WITH HEADERS FROM "file:///acted_in_rels.csv" AS r FIELDTERMINATOR ';'
MATCH (p:Person {id: toInteger(r.`:START_ID(Person)`)}), (m:Movie {id: toInteger(r.`:END_ID(Movie)`)})
CREATE (p)-[:ACTED_IN{role:SPLIT(r.role, '/')}]->(m);
// Set 665 properties, created 665 relationships, statement completed in 187 ms.
// ------------------------------------------------------