Top Banner
Neo4j Makes Graphs Easy Nicole White @_nicolemargaret
25

Neo4j Makes Graphs Easy: Nicole White

Jul 16, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Neo4j Makes Graphs Easy: Nicole White

Neo4j Makes Graphs Easy

Nicole White@_nicolemargaret

Page 2: Neo4j Makes Graphs Easy: Nicole White

Agenda

Model Import QueryBuild an

App!

Page 3: Neo4j Makes Graphs Easy: Nicole White

Model

NorthWind

Page 4: Neo4j Makes Graphs Easy: Nicole White

What if we used SQL?

Page 5: Neo4j Makes Graphs Easy: Nicole White

Categories

CategoryID

CategoryName

Suppliers

SupplierID

CompanyName

Products

ProductID

ProductName

CategoryID

SupplierID

Customers

CustomerID

CompanyName

Order Details

OrderID

ProductID

Orders

OrderID

CustomerID

ShipVia

EmployeeID

Employees

EmployeeID

FirstName

LastName

ReportsTo

Shippers

ShipperID

CompanyName

EmployeeTerritories

EmployeeID

TerritoryID

Territories

TerritoryID

TerritoryDescription

RegionID

Regions

RegionID

RegionDescription

Page 6: Neo4j Makes Graphs Easy: Nicole White

What if we used Neo4j?

Page 7: Neo4j Makes Graphs Easy: Nicole White

Order

Product

Employee

Category

Supplier

Customer

Shipper

Territory

Region

Employee

REPORTS TO

Product

Employee

REPORTS TO

Page 8: Neo4j Makes Graphs Easy: Nicole White

Import

LOAD CSV

Page 9: Neo4j Makes Graphs Easy: Nicole White

Export from SQL

SELECT * FROM Orders

INTO OUTFILE '/tmp/orders.csv'

FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY ''

LINES TERMINATED BY '\r\n';

...

SELECT * FROM Products

INTO OUTFILE '/tmp/products.csv'

FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY ''

LINES TERMINATED BY '\r\n';

Page 10: Neo4j Makes Graphs Easy: Nicole White

To The Browser!Holy nodes

Batman!

Import into Neo4j

Page 11: Neo4j Makes Graphs Easy: Nicole White

Query

Cypher Query Language

Page 12: Neo4j Makes Graphs Easy: Nicole White

What are Steven Buchanan’s top 5 selling products?

Page 13: Neo4j Makes Graphs Easy: Nicole White

SQL

SELECT Products.ProductName AS Product,ROUND(SUM((`Order Details`.UnitPrice*Quantity*(1-Discount)/100)*100)) AS SalesFROM Employees

JOIN Orders ON Employees.EmployeeID = Orders.EmployeeIDJOIN `Order Details` ON Orders.OrderID = `Order Details`.OrderIDJOIN Products ON `Order Details`.ProductID = Products.ProductID

WHERE Employees.FirstName = "Steven" AND Employees.LastName = "Buchanan"GROUP BY Products.ProductNameORDER BY Sales DESC LIMIT 5;

Page 14: Neo4j Makes Graphs Easy: Nicole White

Output

+------------------------+-------+

° Product ° Sales °

+------------------------+-------+

° Cte de Blaye ° 8432 °

° Raclette Courdavault ° 7920 °

° Mozzarella di Giovanni ° 5829 °

° Alice Mutton ° 5538 °

° Perth Pasties ° 5084 °

+------------------------+-------+

Page 15: Neo4j Makes Graphs Easy: Nicole White

Cypher

MATCH (e:Employee)-[:SOLD]->(:Order)-[r:INCLUDED]->(p:Product)

WHERE e.first_name = 'Steven' AND e.last_name = 'Buchanan'

RETURN p.name AS Product,

ROUND(SUM(r.unit_price*r.quantity*(1-r.discount)/100)*100) AS Sales

ORDER BY Sales DESC LIMIT 5;

Page 16: Neo4j Makes Graphs Easy: Nicole White

Output

+-----------------------------------+

| Product | Sales |

+-----------------------------------+

| "Cte de Blaye" | 8432 |

| "Raclette Courdavault" | 7920 |

| "Mozzarella di Giovanni" | 5829 |

| "Alice Mutton" | 5538 |

| "Perth Pasties" | 5084 |

+-----------------------------------+

Page 17: Neo4j Makes Graphs Easy: Nicole White

What is the reporting structure two levels deep?

Page 18: Neo4j Makes Graphs Easy: Nicole White

SQL

SELECT CONCAT(Employees.FirstName, ' ', Employees.LastName) AS Name,CONCAT(e2.FirstName, ' ', e2.LastName) AS Manager,CONCAT(e3.FirstName, ' ', e3.LastName) AS `Manager's Manager`

FROM EmployeesJOIN Employees AS e2 ON Employees.ReportsTo = e2.EmployeeIDJOIN Employees AS e3 ON e2.ReportsTo = e3.EmployeeID;

Page 19: Neo4j Makes Graphs Easy: Nicole White

Output

+----------------+-----------------+-------------------+

° Name ° Manager ° Manager's Manager °

+----------------+-----------------+-------------------+

° Michael Suyama ° Steven Buchanan ° Andrew Fuller °

° Robert King ° Steven Buchanan ° Andrew Fuller °

° Anne Dodsworth ° Steven Buchanan ° Andrew Fuller °

+----------------+-----------------+-------------------+

Page 20: Neo4j Makes Graphs Easy: Nicole White

Cypher

MATCH p = (:Employee)-[:REPORTS_TO*2]->(:Employee)RETURN EXTRACT(n IN NODES(p) |

n.first_name + ' ' + n.last_name) AS reporting;

Page 21: Neo4j Makes Graphs Easy: Nicole White

Output

+------------------------------------------------------+

| reporting |

+------------------------------------------------------+

| ["Michael Suyama","Steven Buchanan","Andrew Fuller"] |

| ["Robert King","Steven Buchanan","Andrew Fuller"] |

| ["Anne Dodsworth","Steven Buchanan","Andrew Fuller"] |

+------------------------------------------------------+

Page 22: Neo4j Makes Graphs Easy: Nicole White

Build an App!

REST Drivers

Page 23: Neo4j Makes Graphs Easy: Nicole White

REST Drivers

Java

.NET

JavaScript

Python

Ruby

PHP

Page 24: Neo4j Makes Graphs Easy: Nicole White

Python App

Page 25: Neo4j Makes Graphs Easy: Nicole White

Questions?