我的Neo4j时最新的V4.1.3,界面跟官网的教程可能不太一样。
本文参考Neo4j英文开发者文档链接如下:
https://neo4j.com/developer/desktop-csv-import/
数据来源:Youtube视频-Neo4j Batch Insertion from CSV
// neo4j-flight-csv-raw.csv
flight,airline,depart,arrive,capacity
23,Delta,ATL,DTW,160
24,Delta,DTW,ATL,160
27,Delta,DTW,BOS,160
28,Delta,BOS,DTW,160
35,Delta,DTW,PIT,128
36,Delta,PIT,DTW,128
37,Delta,ATL,BOS,160
38,Delta,BOS,ATL,160
43,Delta,ATL,PIT,160
44,Delta,PIT,ATL,160
45,Delta,BOS,PIT,160
46,Delta,PIT,BOS,160
101,Southwest,DTW,BOS,136
102,Southwest,BOS,DTW,136
103,Southwest,DTW,PIT,136
104,Southwest,PIT,DTW,136
1231,American,DTW,BOS,160
1232,American,DTW,PIT,160
1257,American,DTW,ATL,128
1258,American,BOS,ATL,160
1277,American,BOS,DTW,160
1278,American,PIT,DTW,160
1292,American,ATL,DTW,128
1292,American,ATL,BOS,160
// neo4j-airport-csv-raw.csv
label,city,state
DTW,Detroit,Michigan
ATL,Altanta,Georgia
PIT,Pittsburgh,Pennsylvania
BOS,Bostan,Massachussetts
声明:以下代码,即使你没装java的jdk也能跑起来,新手不要在环境上浪费过多时间。neo4j只认识utf-8编码的csv,不要直接把excel改成csv来用!
step1:
打开你的Neo4j桌面端,新建一个项目,并为此项目添加一个数据库实例(记住自己设置的用户名、密码),然后开启(start)本项目,之后会有登录操作,用户名默认是neo4j。
step2:
这时点数据库-设置,会出现本地访问的端口号界面,点击open folder-import打开该项目对应的本地import文件夹,把上述两个csv文件拖至此处。(只有执行了此操作,引入文件时才可以用file:///
字样)
step3:
打开localhost:7474
,会自动跳转页面至Neo4j网页版,然后就可以开始敲下面的代码了,依次敲完就好。
load csv with headers from "file:///neo4j-airport-csv-raw.csv" as airports return airports
load csv with headers from "file:///neo4j-flight-csv-raw.csv" as flights return flights
这两行代码,会读取文件得到对应的结构化信息。
create constraint on (f:Flight) assert f.flight is unique
create constraint on (a:Airport) assert a.label is unique
这两步是为Flight对象和Airport对象的label加上约束——唯一性。这样使得访问变得更快。
load csv with headers from "file:///neo4j-airport-csv-raw.csv" as airports
merge (a:Airport{label:airports.label})
set a.city = airports.city, a.state = airports.state
return count(a)
生成各个机场节点,返回节点数目
match (a:Airport) return (a)
显示机场节点
LOAD CSV WITH HEADERS FROM 'file:///neo4j-flight-csv-raw.csv' AS flights
MERGE (f:Flight {number: flights.flight})
SET f.airline = flights.airline, f.capacity = flights.capacity
RETURN count(f)
MATCH (f:Flight) RETURN f
同上
LOAD CSV WITH HEADERS FROM 'file:///neo4j-flight-csv-raw.csv' AS flights
MATCH (f:Flight {number:flights.flight})
MATCH (a:Airport {label:flights.arrive})
MERGE (f)-[r:arrives]->(a)
LOAD CSV WITH HEADERS FROM 'file:///neo4j-flight-csv-raw.csv' AS flights
MATCH (f:Flight {number:flights.flight})
MATCH (a:Airport {label:flights.depart})
MERGE (f)-[r:Depart]->(a)
以上两步是连接节点,形成关系