参考:
https://neo4j.com/developer/desktop-csv-import/
bin\\cypher-shell
# 使用双斜杠,不然会报错找不到路径
neo4j
# 然后输入对应的用户名和密码即可
另外可以看到,命令行的前缀是:neo4j@mysql
,用户名是neo4j
,默认的图数据库是mysql。所以如果想换一个数据库导入的话,可以另外创建一个
这个命令行其实和browser那个可视化界面差不多,只不过没有文字,但是命令都是一模一样的.所以完全没必要用这个,直接用browser好了。
一般导入数据之前,要先看一下数据大概多少行,确认一下有没有表头
//count data rows in products.csv (no headers)
LOAD CSV FROM 'file:///vetex_family.csv' AS row
RETURN count(row);
LOAD CSV FROM 'file:///vetex_family.csv' AS row
RETURN row
LIMIT 3;
可以大致看一下这部分的描述:Graph data model
关于约束的文档,参考:1. Syntax for creating constraints
# 确定好节点类别的名称(建议首字母大写)
CREATE CONSTRAINT UniqueRole ON (p:Role) ASSERT p.id IS UNIQUE;
CREATE CONSTRAINT UniqueFamily ON (o:Family) ASSERT o.id IS UNIQUE;
可以使用show index
或者show UNIQUE constraint
来查看建立好的约束
看起来都差不多
如果不小心写错约束的名称,需要删了重新建立。。。
DROP CONSTRAINT UniqueProduct IF EXISTS
DROP CONSTRAINT UniqueOrder IF EXISTS
关于这里的merge和set语法,可以参考:3.1. Merge with ON CREATE
LOAD CSV FROM 'file:///vetex_role.csv' AS row
WITH row[0] AS roleId, row[1] AS roleName, row[2] AS roleAlias
MERGE (p:Role {roleId: roleId})
SET p.roleName = roleName, p.roleAlias = roleAlias
RETURN count(p);
LOAD CSV FROM 'file:///vetex_family.csv' AS row
WITH row[1] AS familyId, row[0] AS familyName
MERGE (p:Family {familyId: familyId})
SET p.familyName = familyName
RETURN count(p);
导入关系csv
:auto USING PERIODIC COMMIT 500
# 如果数据量很大,使用上面语句每次提交500条数据
LOAD CSV WITH HEADERS FROM 'file:///edge_role_famlily.csv' AS row
# 如果有表头 就使用 WITH HEADERS
LOAD CSV FROM 'file:///edge_role_famlily.csv' AS row
WITH row[0] AS roleId, row[1] AS familyId
MATCH (p:Role {roleId: roleId})
MATCH (o:Family {familyId: familyId})
MERGE (p)-[rel:BelongToFamily]->(o)
RETURN count(rel);
也可以查看一下这个关系:
MATCH (r:Role)-[rel:BelongToFamily]->(f:Family)
RETURN r, rel, f LIMIT 50;
类似的,如果关系也有属性,可以使用以下方式导入
LOAD CSV FROM 'file:///edge_role_role.csv' AS row
WITH row[0] AS roleId, row[1] AS roleId2,row[2] as type
MATCH (p:Role {roleId: roleId})
MATCH (o:Role {roleId: roleId2})
MERGE (p)-[rel:KindShip {type:type}]->(o)
RETURN count(rel);
插入后再查看关系
MATCH (r:Role)-[rel:KindShip]->(r2:Role)
RETURN r, rel, r2 LIMIT 50;