输入导入的链接都来自于neo4j 内部的数据源
一、导入实体数据节点
# 导入商品 相关 csv 相关数据
LOAD CSV WITH HEADERS FROM "http://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")
# 导入品类相关数据
LOAD CSV WITH HEADERS FROM "http://data.neo4j.com/northwind/categories.csv"
AS row
CREATE (n:Category)
SET n = row
# 加载供货方数据
LOAD CSV WITH HEADERS
FROM "http://data.neo4j.com/northwind/suppliers.csv" AS row
CREATE (n:Supplier)
SET n = row
# 创建商品的ID索引
CREATE INDEX ON :Product(productID)
# 创建分类的ID索引
CREATE INDEX ON :Category(categoryID)
# 供货商的ID索引
CREATE INDEX ON :Supplier(supplierID)
二、创建实体节点关系
# 商品与分类的关系处理
MATCH (p:Product),(c:Category)
WHERE p.categoryID = c.categoryID
CREATE (p)-[:PART_OF]->(c)
# 商品与供货商之间的关系
MATCH (p:Product),(s:Supplier)
WHERE p.supplierID = s.supplierID
CREATE (s)-[:SUPPLIES]->(p)
三、Querying Product Catalog Graph
# List the product categories provided by each supplier.
# 商品分类的供货商
MATCH (s:Supplier)-->(:Product)-->(c:Category)
RETURN s.companyName as Company,
collect(distinct c.categoryName) as Categories
# 品类的供货商 Find the produce suppliers.
MATCH (c:Category {categoryName:"Produce"})
<--(:Product)<--(s:Supplier)
RETURN DISTINCT s.companyName as ProduceSuppliers
四、客户 与 订单 Graph
# 加载客户信息
LOAD CSV WITH HEADERS
FROM "http://data.neo4j.com/northwind/customers.csv" AS row
CREATE (n:Customer)
SET n = row
# 加载订单信息
LOAD CSV WITH HEADERS
FROM "http://data.neo4j.com/northwind/orders.csv" AS row
CREATE (n:Order)
SET n = row
# 创建索引
CREATE INDEX ON :Customer(customerID)
CREATE INDEX ON :Order(orderID)
# 创建消费者与订单的关系
MATCH (c:Customer),(o:Order)
WHERE c.customerID = o.customerID
CREATE (c)-[:PURCHASED]->(o)
五、Customer Order Graph
# 加载订单详情 基于订单详情与商品、订单详情与订单 创建订单与商品之间的
# details:ORDERS 关系
LOAD CSV WITH HEADERS
FROM "http://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)
# 找到消费者与订单的关系
# 找到订单与商品的关系
# 找到商品分类为 categoryName:"Produce" 的分类
# 统计出 消费者购买 Produce 类别商品的总量
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