Cypher CREATE
为每条数据写一个CREATE(或MERGE)
CREATE
(`0`:Student{name:"JM68"})-[:`friend` {from:'2013'}]->(`1`:Student{name:"XTT"})
return `0`, `1`
上例为创建两个节点,节点间关系为friend, 节点皆有name属性
Cypher LOAD CSV
将数据转为CSV导入Neo4j数据库
在Neo4j的安装目录的import目录下,
如:新建test.csv,写入
id,name,age
1,JM,26
2,JM68,27
3,HH,18
4,yy,20
执行
LOAD CSV WITH HEADERS FROM "file:///test.csv" AS line
CREATE (p:person{id:line.id,name:line.name,age:line.age})
返回
Added 4 labels, created 4 nodes, completed after 113 ms.
大量数据时加入USING PERIODIC COMMIT
USING periodic commit 1000
LOAD CSV from "file:///test.csv" as line
CREATE (p:person{id:line.id,name:line.name,age:line.age})
使用了语句USING PERIODIC COMMIT 1000,使得每1000行作为一次Transaction提交,提升性能
Neo4j import
使用注意:必须停止neo4j;只能重新生成新的数据库;导入格式为CSV
- into:数据库名称
- nodes:插入节点
- relationships:插入关系
- bad-tolerance:能容忍的错误数据条数(即超过指定条数程序直接挂掉),默认1000
- multiline-fields:是否允许多行插入(即有些换行的数据也可读取)
举例:
三个文件放在neo4j-import命令文件同一目录下,或必须指定完整路径
students.csv
classId:ID,name,:LABEL
c1,"MHT班",Class
c2,"MY班",Class
c3,"LYH班",Class
c4,"RZF班",Class
classes.csv
studentId:ID,name,age:int,:LABEL
s1,"JM68",27,Student
s2,"JM6",22,Student
s3,"JM8",18,Student
s4,"JM",2,Student
relations.csv
:START_ID,:END_ID,:TYPE
s1,c4,FROM
s2,c2,FROM
s3,c3,FROM
s4,c1,FROM
执行
bin/neo4j-import --into newgraph.db --nodes /Users/students.csv --nodes /Users/classes.csv --relationships /Users/relations.csv
结果返回
IMPORT DONE in 1s 86ms.
Imported:
8 nodes
4 relationships
20 properties