Neo4j Makes Graphs Easy

Post on 14-Jul-2015

772 views 0 download

transcript

Neo4j makes Graphs Easy

Overview

Step 1: Data and Relationships

#1

Step 2: Loading your Data into Neo4j

#2

Step 3: Querying your Data

#3a

#3b

Data and RelationshipsFind all the code at https://github.com/kvangundy/GraphDays

NorthWind

What if we used SQL?

Let’s make a Graph

Building Relationships in Graphs

-name

-employeeID

-orderNum

-shippedOn

SOLD

PRODUCT

PART_OF

SUPPLIES

Modeling it as a Graph

What’s Next?

Step 2: Loading your Data into Neo4j

#2

#3

Querying your Data

Cypher Query Language

Basic Query: Who do people report to?

Basic Query: Who do people report to?

MATCH

(e:Employee)<-[:REPORTS_TO]-(sub:Employee)

RETURN

e.employeeID AS managerID,

e.firstName AS managerName,

sub.employeeID AS employeeID,

sub.firstName AS employeeName;

Basic Query: Who do people report to?

Querying your Data

Query Browser

To The Browser!Holy nodes

Batman!

Querying your Data

REST Drivers

REST Drivers

Java

.NET

JavaScript

Python

Ruby

PHP

ANYWHERE…

Python Code

Loading your Data

C S V

CSV Files for Northwind Data

CSV Files for Northwind Data

Step-by-step Creating the Graph

1.Create the core data

2.Create indexes on the data for performance

3.Create the relationships

LOADing the Data

//Create customers

USING PERIODIC COMMIT 1000

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/customers.csv" AS row

CREATE (:Customer {companyName: row.CompanyName, customerID:

row.CustomerID, fax: row.Fax, phone: row.Phone});

//Create products

USING PERIODIC COMMIT 1000

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/products.csv" AS row

CREATE (:Product {productName: row.ProductName, productID: row.ProductID,

unitPrice: toFloat(row.UnitPrice)});

LOADing the Data

// Create suppliers

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/suppliers.csv" AS row

CREATE (:Supplier {companyName: row.CompanyName, supplierID:

row.SupplierID});

// Create employees

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/employees.csv" AS row

CREATE (:Employee {employeeID:row.EmployeeID, firstName: row.FirstName,

lastName: row.LastName, title: row.Title});

LOADing the Data

// Create categories

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/categories.csv" AS row

CREATE (:Category {categoryID: row.CategoryID, categoryName:

row.CategoryName, description: row.Description});

// Create orders

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/orders.csv" AS row

MERGE (order:Order {orderID: row.OrderID}) ON CREATE SET order.shipName =

row.ShipName;

Creating the Indexes

CREATE INDEX ON :Product(productID);

CREATE INDEX ON :Product(productName);

CREATE INDEX ON :Category(categoryID);

CREATE INDEX ON :Employee(employeeID);

CREATE INDEX ON :Supplier(supplierID);

CREATE INDEX ON :Customer(customerID);

CREATE INDEX ON :Customer(customerName);

Creating the Relationships

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/orders.csv" AS row

MATCH (order:Order {orderID: row.OrderID})

MATCH (customer:Customer {customerID: row.CustomerID})

MERGE (customer)-[:PURCHASED]->(order);

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/products.csv" AS row

MATCH (product:Product {productID: row.ProductID})

MATCH (supplier:Supplier {supplierID: row.SupplierID})

MERGE (supplier)-[:SUPPLIES]->(product);

Creating the Relationships

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/products.csv" AS row

MATCH (product:Product {productID: row.ProductID})

MATCH (category:Category {categoryID: row.CategoryID})

MERGE (product)-[:PART_OF]->(category);

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/employees.csv" AS row

MATCH (employee:Employee {employeeID: row.EmployeeID})

MATCH (manager:Employee {employeeID: row.ReportsTo})

MERGE (employee)-[:REPORTS_TO]->(manager);

Creating the Relationships

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/orders.csv" AS row

MATCH (order:Order {orderID: row.OrderID})

MATCH (product:Product {productID: row.ProductID})

MERGE (order)-[relation:PRODUCT]->(product)

ON CREATE SET relation.unitPrice = toFloat(row.UnitPrice), relation.quantity

= toFloat(row.Quantity);

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "https://raw.githubusercontent.com/neo4j-

contrib/developer-resources/gh-pages/data/northwind/orders.csv" AS row

MATCH (order:Order {orderID: row.OrderID})

MATCH (employee:Employee {employeeID: row.EmployeeID})

MERGE (employee)-[:SOLD]->(order);

Recap

What we talked about Today

#1 #2

#3a

#3b

Questions?

Go Forth and Be Graphy.