-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnorthwind.neo4j-browser-guide
384 lines (333 loc) · 13.5 KB
/
northwind.neo4j-browser-guide
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<style type="text/css" media="screen">
/*
.nodes-image {
margin:-100;
}
*/
@import url("//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css");
.imageblock .content img, .image img {max-width: 100%;}
.deck h3, .deck h4 {display: block !important;margin-bottom:8px;margin-top:5px;}
.listingblock {margin:8px;}
.pull-bottom {position:relative;bottom:1em;}
.admonitionblock td.icon [class^="fa icon-"]{font-size:2.5em;text-shadow:1px 1px 2px rgba(0,0,0,.5);cursor:default}
.admonitionblock td.icon .icon-note:before{content:"\f05a";color:#19407c}
.admonitionblock td.icon .icon-tip:before{content:"\f0eb";text-shadow:1px 1px 2px rgba(155,155,0,.8);color:#111}
.admonitionblock td.icon .icon-warning:before{content:"\f071";color:#bf6900}
.admonitionblock td.icon .icon-caution:before{content:"\f06d";color:#bf3400}
.admonitionblock td.icon .icon-important:before{content:"\f06a";color:#bf0000}
.admonitionblock.note.speaker { display:none; }
</style>
<style type="text/css" media="screen">
/* #editor.maximize-editor .CodeMirror-code { font-size:24px; line-height:26px; } */
</style>
<article class="guide" ng-controller="AdLibDataController">
<carousel class="deck container-fluid">
<!--slide class="row-fluid">
<div class="col-sm-3">
<h3>Northwind Graph</h3>
<p class="lead">Information</p>
<!dl>
</dl>
</div>
<div class="col-sm-9">
<figure>
<img style="width:300px" src=""/>
</figure>
</div>
</slide-->
<h4>Northwind Graph</h4>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>From RDBMS to Graph, using a classic dataset</h3>
<br/>
<div>
<div class="paragraph">
<p>The<em>Northwind Graph</em> demonstrates how to migrate from a relational
database to Neo4j. The transformation is iterative and deliberate,
emphasizing the conceptual shift from relational tables to the nodes and
relationships of a graph.</p>
</div>
<div class="paragraph">
<p>This guide will show you how to:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Load: create data from external CSV files</p>
</li>
<li>
<p>Index: index nodes based on label</p>
</li>
<li>
<p>Relate: transform foreign key references into data relationships</p>
</li>
<li>
<p>Promote: transform join records into relationships</p>
</li>
</ol>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Product Catalog</h3>
<br/>
<div>
<div class="paragraph">
<p>Northwind sells food products in a few categories, provided by
suppliers. Let’s start by loading the product catalog tables.</p>
</div>
<div class="paragraph">
<p>The load statements to the right require public internet
access.<code>LOAD CSV</code> will retrieve a CSV file from a valid URL, applying a
Cypher statement to each row using a named map (here we’re using the
name <code>row</code>).</p>
</div>
<div class="paragraph">
<p><span class="image"><img src="https://dev.assets.neo4j.com/wp-content/uploads/20160211151109/product-category-supplier.png" alt="image"></span></p>
</div>
<h4>Create Constraints</h4>
<div class="listingblock">
<div class="title">Create constraints for the nodes</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->CREATE CONSTRAINT product_productID IF NOT EXISTS FOR (p:Product) REQUIRE (p.productID) IS UNIQUE;
CREATE CONSTRAINT category_categoryID IF NOT EXISTS FOR (c:Category) REQUIRE (c.categoryID) IS UNIQUE;
CREATE CONSTRAINT supplier_supplierID IF NOT EXISTS FOR (s:Supplier) REQUIRE (s.supplierID) IS UNIQUE;<!--/code--></pre>
</div>
</div>
<h4>Load records</h4>
<div class="listingblock">
<div class="title">Load products</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/products.csv" AS row
CREATE (n:Product)
SET n = row,
n.unitPrice = toFloat(row.unitPrice),
n.unitsInStock = toInteger(row.unitsInStock), n.unitsOnOrder = toInteger(row.unitsOnOrder),
n.reorderLevel = toInteger(row.reorderLevel), n.discontinued = (row.discontinued <> "0")<!--/code--></pre>
</div>
</div>
<div class="listingblock">
<div class="title">Load categories</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/categories.csv" AS row
CREATE (n:Category)
SET n = row<!--/code--></pre>
</div>
</div>
<div class="listingblock">
<div class="title">Load suppliers</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/suppliers.csv" AS row
CREATE (n:Supplier)
SET n = row<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Product Catalog Graph</h3>
<br/>
<div>
<div class="paragraph">
<p>The products, categories and suppliers are related through foreign key
references. Let’s promote those to data relationships to realize the
graph.</p>
</div>
<div class="paragraph">
<p><span class="image"><img src="https://dev.assets.neo4j.com/wp-content/uploads/20160211151108/product-graph.png" alt="image"></span></p>
</div>
<h4>Create data relationships</h4>
<div class="listingblock">
<div class="title">Connect products to categories</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (p:Product),(c:Category)
WHERE p.categoryID = c.categoryID
CREATE (p)-[:PART_OF]->(c)<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Note you only need to compare property values like this when first
creating relationships</p>
</div>
<div class="paragraph">
<p>Calculate join, materialize relationship.
(See <a href="https://neo4j.com/developer/guide-importing-data-and-etl">importing guide</a> for more details)</p>
</div>
<div class="listingblock">
<div class="title">Connect products to suppliers</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (p:Product),(s:Supplier)
WHERE p.supplierID = s.supplierID
CREATE (s)-[:SUPPLIES]->(p)<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Note you only need to compare property values like this when first
creating relationships</p>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Querying Product Catalog Graph</h3>
<br/>
<div>
<div class="paragraph">
<p>Lets try some queries using patterns.</p>
</div>
<div class="paragraph">
<p><span class="image"><img src="https://dev.assets.neo4j.com/wp-content/uploads/20160211151108/product-graph.png" alt="image"></span></p>
</div>
<h4>Query using patterns</h4>
<div class="listingblock">
<div class="title">List the product categories provided by each supplier</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (s:Supplier)-->(:Product)-->(c:Category)
RETURN s.companyName as Company, collect(distinct c.categoryName) as Categories<!--/code--></pre>
</div>
</div>
<div class="listingblock">
<div class="title">List suppliers for the "Produce" category</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (c:Category {categoryName:"Produce"})<--(:Product)<--(s:Supplier)
RETURN DISTINCT s.companyName as ProduceSuppliers<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Find the produce suppliers.</p>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Customer Orders</h3>
<br/>
<div>
<div class="paragraph">
<p>Northwind customers place orders which may detail multiple
products.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://dev.assets.neo4j.com/wp-content/uploads/20160211151108/customer-orders.png" alt="customer orders">
</div>
</div>
<h4>Load and index records</h4>
<div class="listingblock">
<div class="title">Create constraints for nodes</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->CREATE CONSTRAINT customer_customerID IF NOT EXISTS FOR (c:Customer) REQUIRE (c.customerID) IS UNIQUE;
CREATE CONSTRAINT order_orderID IF NOT EXISTS FOR (o:Order) REQUIRE (o.orderID) IS UNIQUE;<!--/code--></pre>
</div>
</div>
<div class="listingblock">
<div class="title">Load customers</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/customers.csv" AS row
CREATE (n:Customer)
SET n = row<!--/code--></pre>
</div>
</div>
<div class="listingblock">
<div class="title">Load orders</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/orders.csv" AS row
CREATE (n:Order)
SET n = row<!--/code--></pre>
</div>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Create data relationships</h3>
<br/>
<div>
<div class="listingblock">
<div class="title">Connect customers to their orders</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (c:Customer),(o:Order)
WHERE c.customerID = o.customerID
CREATE (c)-[:PURCHASED]->(o)<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Note you only need to compare property values like this when first
creating relationships</p>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Customer Order Graph</h3>
<br/>
<div>
<div class="paragraph">
<p>Notice that Order Details are always part of an Order and that
they<em>relate</em> the Order to a Product — they’re a join table. Join
tables are always a sign of a data relationship, indicating shared
information between two other records.</p>
</div>
<div class="paragraph">
<p>Here, we’ll directly promote each OrderDetail record into a relationship
in the graph. <span class="image"><img src="https://dev.assets.neo4j.com/wp-content/uploads/20160211151107/order-graph.png" alt="order graph"></span></p>
</div>
<h4>Load and index records</h4>
<div class="listingblock">
<div class="title">Load order details and use them to connect orders to products</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/order-details.csv" AS row
MATCH (p:Product), (o:Order)
WHERE p.productID = row.productID AND o.orderID = row.orderID
CREATE (o)-[details:ORDERS]->(p)
SET details = row,
details.quantity = toInteger(row.quantity)<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p>Note you only need to compare property values like this when first
creating relationships</p>
</div>
</div>
</div>
</slide>
<slide class="row-fluid">
<div class="col-sm-12">
<h3>Query using patterns</h3>
<br/>
<div>
<div class="listingblock">
<div class="title">Find total quantity per customer in the "Produce" category</div>
<div class="content">
<pre mode="cypher" class="highlight pre-scrollable programlisting cm-s-neo code runnable standalone-example ng-binding" data-lang="cypher" lang="cypher"><!--code class="cypher language-cypher"-->MATCH (cust:Customer)-[:PURCHASED]->(:Order)-[o:ORDERS]->(p:Product),
(p)-[:PART_OF]->(c:Category {categoryName:"Produce"})
RETURN DISTINCT cust.contactName as CustomerName, SUM(o.quantity) AS TotalProductsPurchased<!--/code--></pre>
</div>
</div>
<div class="paragraph">
<p><em>More Resources</em></p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://neo4j.com/developer/guide-importing-data-and-etl/">Full
Northwind import example</a></p>
</li>
<li>
<p><a href="https://neo4j.com/developer">Developer resources</a></p>
</li>
</ul>
</div>
</div>
</div>
</slide>
</carousel>
</article>