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里面创建数据库。

关于如何在数据库中导入已有数据的方法:

  1. 打开neo4j project的管理->Open Folder
  2. 点击进入data文件夹
  3. 打开已存在的数据库的文件夹->进入data文件夹
  4. 将databases和transactions两个文件夹复制粘贴进之前打开的neo4j project的data文件夹里。
  5. 启动数据库,就可以看到已经生成的Node和Relationship啦。

数据库运行时,在开启Neo4j Browser时出现“服务器验证失败”(The client is unauthorized due to authentication failure.)的解决方案:

  1. 如果是在浏览器上登录不同的neo4j数据库,很可能是由于缓存没有清理掉导致的。可以试试无痕浏览来访问neo4j的web页面。
  2. 停止neo4j服务,并且删除data/dbms/auth,重新启动。
  3. 修改conf/neo4j.conf配置文件,取消验证机制,修改如下
    dbms.security.auth_enabled=false
    
    (修改方法:vi neo4j.conf;输入i;开始编辑,添加上述语句;esc;输入:wq)

Neo4j中数据库语句

  1. 查询

    find a person named
    Tom Hanks

    MATCH (a:Person {name:'Tom Hanks'}) RETURN a
    

    find all

    MATCH (a) RETURN a
    
  2. 添加

    Create a person named Brie Larson and Brith 1989

    CREATE (a:Person {name:'Brie Larson', born:1989}) RETURN a
    

    Create a movie titled Capatin Marvel, Released 2019,Tagline Everything begins with a (her)o.

    CREATE (a:Movie {title:'Captain Marvel',released:2019,tagline:'Everything begins with a (her)o.'}) RETURN a
    
  3. 删除

    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
    
  4. 创建/更新

    Update a person named Brie Larson ,change his Brith as 1989 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.

  5. 创建关系

    Person named Brie Larson acted as Carol Danvers in movie titled Captain 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
    
  6. 高级查询-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 before 2000

    MATCH (a:Movie) WHERE a.released > 1990 AND a.released < 2000 RETURN a
    
  7. 高级查询-基于关系

    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
    
  8. 高级查询-基于关系

    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
    
  9. 高级查询-计算联通度/路径

    Movies and actors up to 4 "hops" away from Kevin Bacon

    MATCH (bacon:Person {name:"Kevin Bacon"})-[*1..4]-(hollywood) RETURN DISTINCT hollywood
    

    The shortest path following any relationship from Kevin Bacon to Al Pacino

    MATCH p=shortestPath((bacon:Person {name:"Kevin Bacon"})-[*]-(a:Person {name:'Al Pacino'})) RETURN p
    
  10. 推荐

    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 to Tom 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
    
posted @ 2021-03-17 21:00  墨华_shr  阅读(279)  评论(0编辑  收藏  举报