Querying Neo4j With Sql

Yes, that is really true. If you are not that good in neo4j because it is in whole different level than any other languages. Although it has not been specified and keep this as an secret by neo4j developers. It can be written  along side with other languages like Sql. I now that is really ridiculous, but yes it’s true.

In my side, we are on python parsing along with neo4j query. In short, we are doing neo4j output through python query by py2neo or maybe rest api client. But that is on whole different level.

To ensure that your project my project is doing write, my way is to check it first using the language of Sql to create neo4j nodes and relationship. Because it is very hard to take measures if you will undergo python parsing to create neo4j nodes and relationship and then you found out that you are doing it wrong. It is very critical to start again with new query because it is not that easy. Like I said, it is on different level.

One example is in this repository. https://github.com/Doveps/sample_neo4j

All tables are on csv file. It has been made on libre calc on linux  or excel on windows. And then write a code in sql form.

If you wanted to run it, here is the sample:

For Creating Customer:

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/customers.csv” AS row

CREATE (:Customer {companyName: row.CompanyName, customerID: row.CustomerID, fax: row.Fax, phone: row.Phone});

For Creating Product:

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/products.csv” AS row

CREATE (:Product {productName: row.ProductName, productID: row.ProductID, unitPrice: toFloat(row.UnitPrice)});

For Creating Suppliers:

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/suppliers.csv” AS row

CREATE (:Supplier {companyName: row.CompanyName, supplierID: row.SupplierID});

For Creating Employees:

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/employees.csv” AS row

CREATE (:Employee {employeeID:row.EmployeeID,  firstName: row.FirstName, lastName: row.LastName, title: row.Title});

For Creating Categories:

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/categories.csv” AS row

CREATE (:Category {categoryID: row.CategoryID, categoryName: row.CategoryName, description: row.Description});

For Creating Orders:

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/orders.csv” AS row

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

For Creating 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);

 

For Creating a Relationships:

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/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://github.com/Doveps/sample_neo4j/blob/master/products.csv” AS row

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

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

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

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/orders.csv” AS row

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

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

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

ON CREATE SET pu.unitPrice = toFloat(row.UnitPrice), pu.quantity = toFloat(row.Quantity);

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/orders.csv” AS row

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

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

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

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM “https://github.com/Doveps/sample_neo4j/blob/master/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://github.com/Doveps/sample_neo4j/blob/master/employees.csv” AS row

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

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

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

 

Note: If there is any error that occur. Please have a comment on issues in https://github.com/Doveps/sample_neo4j. Thanks!

 

Leave a Reply