1、下载neo4j并启动
docker run --rm --name testneo4j -p 7474:7474 -p 7687:7687 -d -v C:/Users/ASUS/Desktop/neo4j/data:/data -v C:/Users/ASUS/Desktop/neo4j/logs:/logs -v C:/Users/ASUS/Desktop/neo4j/import:/var/lib/neo4j/import -v C:/Users/ASUS/Desktop/neo4j/plugins:/plugins --env NEO4J_AUTH=neo4j/test neo4j:latest
- 桌面的neo4j文件夹会被自动创建
- 7474是neo4j默认http通信端口,使得可以用浏览器访问可视化界面
- 7687是neo4j默认用于Bolt协议的端口,Bolt协议是neo4j的应用层协议
- –env指定了NEO4J_AUTH环境变量,该环境变量用于设置登录账号和密码:分别为neo4j和test
2、浏览器访问
http://localhost:7474
3、创建一个node
CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })
ee只是变量,可以为其他名称
4、查看刚创建的node
MATCH (ee:Person)
WHERE ee.name = "Emil"
RETURN ee;
5、另外创建四个node,并定义关系
MATCH (ee:Person)
WHERE ee.name = "Emil"
CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }),
(ir:Person { name: "Ian", from: "England", title: "author" }),
(rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }),
(ally:Person { name: "Allison", from: "California", hobby: "surfing" }),(ee)-[:KNOWS {since: 2001}]->(js),
(ee)-[:KNOWS {rating: 5}]->(ir),
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
(rvb)-[:KNOWS]->(ally)
6、查看emil的朋友
MATCH (ee:Person)-[:KNOWS]-(friends)
WHERE ee.name = "Emil"
RETURN ee, friends
7、查看所有节点
MATCH (n) RETURN n
8、删除所有nodes和他们的关系
MATCH (n) DETACH DELETE n
9、最短路实战
创建图
CREATE (LocationA:Location { name: "Location A" })
CREATE (LocationB:Location { name: "Location B" })
CREATE (LocationC:Location { name: "Location C" })
CREATE (LocationD:Location { name: "Location D" })
CREATE (LocationE:Location { name: "Location E" })
CREATE (LocationF:Location { name: "Location F" })
CREATE (LocationG:Location { name: "Location G" })
CREATE (LocationH:Location { name: "Location H" })
CREATE (LocationI:Location { name: "Location I" })CREATE(LocationA)-[:CONNECTED_TO { distance: 5 }]->(LocationB),(LocationB)-[:CONNECTED_TO { distance: 6 }]->(LocationC),(LocationC)-[:CONNECTED_TO { distance: 4 }]->(LocationI),(LocationA)-[:CONNECTED_TO { distance: 3 }]->(LocationD),(LocationD)-[:CONNECTED_TO { distance: 4 }]->(LocationE),(LocationE)-[:CONNECTED_TO { distance: 5 }]->(LocationI),(LocationA)-[:CONNECTED_TO { distance: 2 }]->(LocationF),(LocationF)-[:CONNECTED_TO { distance: 3 }]->(LocationG),(LocationG)-[:CONNECTED_TO { distance: 2 }]->(LocationH),
(LocationH)-[:CONNECTED_TO { distance: 1 }]->(LocationI)
查看:MATCH (n) RETURN n
查找A到I的最短路
MATCH (from:Location { name:"Location A" }), (to:Location { name: "Location I"}), path = (from)-[:CONNECTED_TO*]->(to)
RETURN path AS shortestPath,reduce(distance = 0, r in relationships(path) | distance+r.distance) AS totalDistanceORDER BY totalDistance ASCLIMIT 1