25
Neo4j Makes Graphs Easy Nicole White @_nicolemargaret

Neo4j Makes Graphs Easy: Nicole White

Embed Size (px)

Citation preview

Neo4j Makes Graphs Easy

Nicole White@_nicolemargaret

Agenda

Model Import QueryBuild an

App!

Model

NorthWind

What if we used SQL?

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

What if we used Neo4j?

Order

Product

Employee

Category

Supplier

Customer

Shipper

Territory

Region

Employee

REPORTS TO

Product

Employee

REPORTS TO

Import

LOAD CSV

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';

To The Browser!Holy nodes

Batman!

Import into Neo4j

Query

Cypher Query Language

What are Steven Buchanan’s top 5 selling products?

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;

Output

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

° Product ° Sales °

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

° Cte de Blaye ° 8432 °

° Raclette Courdavault ° 7920 °

° Mozzarella di Giovanni ° 5829 °

° Alice Mutton ° 5538 °

° Perth Pasties ° 5084 °

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

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;

Output

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

| Product | Sales |

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

| "Cte de Blaye" | 8432 |

| "Raclette Courdavault" | 7920 |

| "Mozzarella di Giovanni" | 5829 |

| "Alice Mutton" | 5538 |

| "Perth Pasties" | 5084 |

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

What is the reporting structure two levels deep?

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;

Output

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

° Name ° Manager ° Manager's Manager °

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

° Michael Suyama ° Steven Buchanan ° Andrew Fuller °

° Robert King ° Steven Buchanan ° Andrew Fuller °

° Anne Dodsworth ° Steven Buchanan ° Andrew Fuller °

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

Cypher

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

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

Output

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

| reporting |

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

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

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

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

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

Build an App!

REST Drivers

REST Drivers

Java

.NET

JavaScript

Python

Ruby

PHP

Python App

Questions?