圖數據庫是專門存儲和檢索大量信息網絡的存儲引擎。它能夠有效地將數據存儲爲節點和關係,並容許高性能檢索和查詢這些結構。屬性能夠添加到節點和關係。節點能夠用零個或多個標籤標註,關係老是定向和命名。java
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/併發
https://neo4j.com/docs/developer-manual/3.2/cypher/clauses/app
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964}) CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
前面的Keanu實際沒有做用,性能
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix), (Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix)
:['Neo']指的是關係上的屬性。url
刪除全部節點和關係 :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
MATCH (n) RETURN n
返回獨立節點:
MATCH (tom {name: "Tom Hanks"}) RETURN tom MATCH (people:Person) RETURN people.name LIMIT 10
MATCH (movie:Movie) RETURN movie.title
MATCH (director { name: 'Nora Ephron' })--(movie) RETURN movie.title
返回全部'Nora Ephron'導演的電影。符號‘--’指的是不考慮聯繫的類型和方向。
MATCH (:Person { name: 'Nora Ephron' })-->(movie) RETURN movie.title
這個只知道有聯繫,可是不清楚聯繫的類型。
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
MATCH (movie { title: 'The Matrix Reloaded' })<-[:ACTED_IN|:DIRECTED]-(person) RETURN person.name
返回和'The Matrix Reloaded'是演員或者導演關係的人
MATCH (c:CLASS{ name: 'PreFetchServiceImpl' })--(class)RETURN c,class
CREATE (n:CLASS {id : {id}, name: {name}, type: {type}, appCode : {appCode}, pack : {pack}, urls : {urls}})
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();