Recently, I was studying the application of graph database Neo4j in network security, and I briefly searched and said that csv is a good way to enter the database, so I followed the tutorial on the official website to realize the data storage step by step.
AILX10
Excellent answerer in cybersecurity
Master's in Cybersecurity
Go to consult
The tutorial on the official website [1] is particularly detailed, but in English, and I have reproduced it here, which is very simple.
If the CSV file does not have a header file, import it as follows:
LOAD CSV FROM 'file:///products.csv' AS row
WITH toInteger(row[0]) AS productId, row[1] AS productName, toFloat(row[2]) AS unitCost
MERGE (p:Product {productId: productId})
SET p.productName = productName, p.unitCost = unitCost
RETURN count(p);
If the CSV file has header files, the difference is that the column names must be the same, and the import method is as follows:
LOAD CSV WITH HEADERS FROM 'file:///orders.csv' AS row
WITH toInteger(row.orderID) AS orderId, datetime(replace(row.orderDate,' ','T')) AS orderDate, row.shipCountry AS country
MERGE (o:Order {orderId: orderId})
SET o.orderDateTime = orderDate, o.shipCountry = country
RETURN count(o);
In addition, indexing some IDs can speed up inbound performance:
CREATE CONSTRAINT UniqueProduct ON (p:Product) ASSERT p.id IS UNIQUE;
CREATE CONSTRAINT UniqueOrder ON (o:Order) ASSERT o.id IS UNIQUE;
The above is the storage method of points, let's take a look at the storage method of the edge:
:auto USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM 'file:///order-details.csv' AS row
WITH toInteger(row.productID) AS productId, toInteger(row.orderID) AS orderId, toInteger(row.quantity) AS quantityOrdered
MATCH (p:Product {productId: productId})
MATCH (o:Order {orderId: orderId})
MERGE (o)-[rel:CONTAINS {quantityOrdered: quantityOrdered}]->(p)
RETURN count(rel);
At this time, it's done, and you can enjoy the renderings~
MATCH (o:Order)-[rel:CONTAINS]->(p:Product)
RETURN p, rel, o LIMIT 50;
reference
- ^NEO4J warehousing case https://neo4j.com/developer/desktop-csv-import/