Neo4j圖數據庫

01. 圖數據庫

圖數據庫是專門存儲和檢索大量信息網絡的存儲引擎。它能夠有效地將數據存儲爲節點和關係,並容許高性能檢索和查詢這些結構。屬性能夠添加到節點和關係。節點能夠用零個或多個標籤標註,關係老是定向和命名。java

1. Neo4j

Neo4j是一個開源的NOSQL圖形數據庫。它將數據轉換成化爲節點、關係和屬性。使用圖查詢語言Cypher,支持集羣,支持ACID事務,併發控制、事務恢復、高可用性。數據庫

介紹(第一章):
https://neo4j.com/docs/developer-manual/3.2/introduction/api

基礎入門:https://neo4j.com/developer/java/網絡

Java開發手冊:http://neo4j.com/docs/java-reference/current/session

Java API: https://neo4j.com/docs/java-reference/current/javadocs/併發

2. 圖查詢語言Cypher

1. 查詢語言文檔(第三章)

https://neo4j.com/docs/developer-manual/3.2/cypher/clauses/app

2. 建立節點

CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})

前面的Keanu實際沒有做用,性能

3. 創建聯繫

CREATE
(Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix)

:['Neo']指的是關係上的屬性。url

4. 刪除

刪除全部節點和關係 :code

MATCH (n) DETACH DELETE n

刪除CLASS標籤全部節點和關係:

MATCH (n:CLASS) DETACH DELETE n

MATCH (n:CLASS) where n.title='A Few Good Men' DETACH DELETE n

5. match(查找)

1. 返回數據庫中全部類型的節點
MATCH (n) RETURN n

返回獨立節點:

MATCH (tom {name: "Tom Hanks"}) RETURN tom
MATCH (people:Person) RETURN people.name LIMIT 10
2. 標籤類型的全部節點
MATCH (movie:Movie) RETURN movie.title
3. 不帶聯繫指向的match
MATCH (director { name: 'Nora Ephron' })--(movie)
RETURN movie.title

返回全部'Nora Ephron'導演的電影。符號‘--’指的是不考慮聯繫的類型和方向。

4. 帶聯繫指向的match
MATCH (:Person { name: 'Nora Ephron' })-->(movie)
RETURN movie.title

這個只知道有聯繫,可是不清楚聯繫的類型。

5. 帶聯繫指向和類型的match
MATCH (wallstreet:Movie { title: 'The Matrix Reloaded' })<-[:ACTED_IN]-(actor)
RETURN actor.name

返回全部acted in 'The Matrix Reloaded'演員。
帶了聯繫指向和類型。

MATCH (director:Person)-[:DIRECTED]->(movie)
WHERE director.name = "Vincent Ward"
RETURN movie.title

箭頭的指向實際上就是關係的指向。
等價於:

MATCH (movie)<-[:DIRECTED]-(director)
WHERE director.name = "Vincent Ward"
RETURN movie.title
6. 多類型匹配
MATCH (movie { title: 'The Matrix Reloaded' })<-[:ACTED_IN|:DIRECTED]-(person)
RETURN person.name

返回和'The Matrix Reloaded'是演員或者導演關係的人

7. 查詢這個節點類和其相關調用
MATCH (c:CLASS{ name: 'PreFetchServiceImpl' })--(class)RETURN c,class

3. Java API

1. 建立節點

CREATE (n:CLASS {id : {id}, name: {name}, type: {type}, appCode : {appCode}, pack : {pack}, urls : {urls}})

2. 創建聯繫

MATCH (a:CLASS),(b:CLASS) WHERE a.id = {aId} AND b.id = {bId} CREATE (a)-[r:IMPLEMENT]->(b)

a指向b,聯繫的名稱是「IMPLEMENT」

Demo:

Driver driver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "1234"));
    Session session = driver.session();

    session.run("CREATE (a:Person {name: {name}, title: {title}})",
            parameters("name", "Arthur", "title", "King"));

    StatementResult result = session.run("MATCH (a:Person) WHERE a.name = {name} " +
                    "RETURN a.name AS name, a.title AS title",
            parameters("name", "Arthur"));
    while (result.hasNext()) {
        Record record = result.next();
        System.out.println(record.get("title").asString() + " " + record.get("name").asString());
    }

    session.close();
    driver.close();
相關文章
相關標籤/搜索