Neo4j学习
1、预配置
由于Neo4j是基于Java的图形数据库,运行Neo4j需要启动JVM进程,因此必须安装JAVA SE的JDK。配置JDK环境,为以后能适应Springboot,请选择最低JDK1.8的环境。
2、版本选择
个人开发者可以选择Neo4j的server版本或者desktop版本即可,但是切记,这两种版本不要同时安装,否则可能会产生一些错误。
desktop版本相当于server+界面展示(取代了浏览器打开),个人比较喜欢Desktop版本,desktop版本在创建、切换数据库时很形象易懂。
3、下载与安装
在Neo4j的官网下载对应版本即可,最好下载最新版,下载和安装过程按照官网引导即可。
4、软件使用
创建project,在project里面创建数据库。
关于如何在数据库中导入已有数据的方法:
- 打开neo4j project的管理->Open Folder
- 点击进入data文件夹
- 打开已存在的数据库的文件夹->进入data文件夹
- 将databases和transactions两个文件夹复制粘贴进之前打开的neo4j project的data文件夹里。
- 启动数据库,就可以看到已经生成的Node和Relationship啦。
数据库运行时,在开启Neo4j Browser时出现“服务器验证失败”(The client is unauthorized due to authentication failure.)的解决方案:
- 如果是在浏览器上登录不同的neo4j数据库,很可能是由于缓存没有清理掉导致的。可以试试无痕浏览来访问neo4j的web页面。
- 停止neo4j服务,并且删除data/dbms/auth,重新启动。
- 修改conf/neo4j.conf配置文件,取消验证机制,修改如下
(修改方法:vi neo4j.conf;输入i;开始编辑,添加上述语句;esc;输入:wq)dbms.security.auth_enabled=false
Neo4j中数据库语句
-
查询
find a person named
Tom Hanks
MATCH (a:Person {name:'Tom Hanks'}) RETURN a
find all
MATCH (a) RETURN a
-
添加
Create a person named
Brie Larson
and Brith1989
CREATE (a:Person {name:'Brie Larson', born:1989}) RETURN a
Create a movie titled
Capatin Marvel
, Released2019
,TaglineEverything begins with a (her)o.
CREATE (a:Movie {title:'Captain Marvel',released:2019,tagline:'Everything begins with a (her)o.'}) RETURN a
-
删除
Delete all persons named
Brie Larson
MATCH (a:Person {name:'Brie Larson'}) DETACH DELETE a
Delete all movies named
Captain Marvel
MATCH (a:Movie {title:'Captain Marvel'}) DETACH DELETE a
-
创建/更新
Update a person named
Brie Larson
,change his Brith as1989
and increase his star(+1)MERGE (a:Person {name:'Brie Larson'}) ON CREATE SET a.born = 1989 ON MATCH SET a.stars = COALESCE(a.stars, 0) + 1 RETURN a
Use the ON CREATE branch to set values for newly created nodes.
Use the ON MATCH branch to update values for existing nodes. -
创建关系
Person named
Brie Larson
acted asCarol Danvers
in movie titledCaptain Marvel
MATCH (a:Person {name:'Brie Larson'}), (b:Movie {title:'Captain Marvel'}) MERGE (a)-[r:ACTED_IN]->(b) SET r.roles = ['Carol Danvers'] RETURN a,r,b
-
高级查询-where
find Persons with names that start with
Tom
MATCH (a:Person) WHERE a.name STARTS WITH 'Tom' RETURN a
find Movies released after
1990
but before2000
MATCH (a:Movie) WHERE a.released > 1990 AND a.released < 2000 RETURN a
-
高级查询-基于关系
List movies with actor
Tom Hanks
MATCH (a:Person {name:'Tom Hanks'})-[:ACTED_IN]->(m:Movie) RETURN a,m
find Who directed the movie
Cloud Atlas
MATCH (m:Movie {title:'Cloud Atlas'})<-[:DIRECTED]-(d:Person) RETURN d
-
高级查询-基于关系
find Co-actors of actor
Tom Hanks
MATCH (a:Person {name:'Tom Hanks'})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(c) RETURN c.name
How people are related to movie
Cloud Atlas
MATCH (people:Person)-[relatedTo]-(:Movie {title:'Cloud Atlas'}) RETURN people.name, type(relatedTo), relatedTo
-
高级查询-计算联通度/路径
Movies and actors up to
4 "hops"
away fromKevin Bacon
MATCH (bacon:Person {name:"Kevin Bacon"})-[*1..4]-(hollywood) RETURN DISTINCT hollywood
The shortest path following any relationship from
Kevin Bacon
toAl Pacino
MATCH p=shortestPath((bacon:Person {name:"Kevin Bacon"})-[*]-(a:Person {name:'Al Pacino'})) RETURN p
-
推荐
Extend co-actors, to find co-co-actors who haven't worked with
Tom Hanks
MATCH (a:Person {name:'Tom Hanks'})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),(coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cocoActors) WHERE NOT (a)-[:ACTED_IN]->()<-[:ACTED_IN]-(cocoActors) AND a <> cocoActors RETURN cocoActors.name AS Recommended, count(*) AS Strength ORDER BY Strength DESC
Find someone to introduce
Tom Hanks
toTom Cruise
MATCH (a:Person {name:'Tom Hanks'})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),(coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(other:Person {name:'Tom Cruise'}) RETURN a, m, coActors, m2, other