5分鍾了解圖數據庫Neo4j的使用


1.圖數據庫安裝與配置

1.1安裝與配置

配置path = %NEO4J_HOME%\bin
 
啟動命令:neo4j console
 
web訪問: http://localhost:7474

1.2權限管理

:server change-password 修改密碼
 
:server user list 可視化界面管理用戶權限
 
:server disconnect 退出當前用戶

2.從csv導入數據

(1)停掉服務
 
(2)刪除 graph.db 目錄
 
(3)報錯,解決辦法:將bin/neo4j-import.ps1文件的相對路徑改為絕對路徑
 
(4)准備CSV文件。舉例如下,記錄為電影,明星 以及其中存在的一個扮演角色關系。CSV格式為:
 
movies.csv
movieId:ID,title,year:int,:LABEL
tt0133093,"The Matrix",1999,Movie
tt0234215,"The Matrix Reloaded",2003,Movie;Sequel
tt0242653,"The Matrix Revolutions",2003,Movie;Sequel
 
actors.csv
personId:ID,name,:LABEL
keanu,"Keanu Reeves",Actor
laurence,"Laurence Fishburne",Actor
carrieanne,"Carrie-Anne Moss",Actor
 
roles.csv
:START_ID,role,:END_ID,:TYPE
keanu,"Neo",tt0133093,ACTED_IN
keanu,"Neo",tt0234215,ACTED_IN
keanu,"Neo",tt0242653,ACTED_IN
laurence,"Morpheus",tt0133093,ACTED_IN
laurence,"Morpheus",tt0234215,ACTED_IN
laurence,"Morpheus",tt0242653,ACTED_IN
carrieanne,"Trinity",tt0133093,ACTED_IN
carrieanne,"Trinity",tt0234215,ACTED_IN
carrieanne,"Trinity",tt0242653,ACTED_IN
 
(5) 導入命令:neo4j-import --into graph.db --nodes <節點1.csv> --nodes <節點2.csv> --relationships <關系.csv>
 

3.常見的CQL命令

以Movie、Actors、Roles 為例,圖形如下:
 
 

3.1查詢

  • 查詢整個圖形
match(n) return n
 
  • 查詢year小於2000的電影
match (n)
where n.year < 2000
return n
 
  • 查詢帶有movie標簽的節點
match(n:Movie)
return n
 
  • 查詢名字叫Keanu Reeves的演員
match (n{name:'Keanu Reeves'})
return n
 
  • 查詢與帶Movie標簽的節點相關的所有節點
match(n) -- (m:Movie)
return n
 
  • 查詢“Keanu Reeves”所有參演過的電影
match (n) -[r:ACTED_IN]-> (m:Movie)
where n.name = 'Keanu Reeves'
return m
 
match (n{name:'Keanu Reeves'}) -[r:ACTED_IN]-> (m:Movie)
return m
 
  • 查詢與“Keanu Reeves”同演過的人
match (a) -[:ACTED_IN]->(m)<-[:ACTED_IN]- (b)
return distinct b
 

3.2.創建

  • 增加拍攝於2010年名叫“super man”的電影
create (n:Movie{title:'super man',year:2010})
return n
 
  • 增加名叫“Jone”的演員
create (n:Actor{name:'Jone'})
return n
 
  • 增加“Jone”和“super man”之間類型為ACTED_IN的關系
match (a{name:'Jone'}),(b{name:'super man'})
create (a) -[r:ACTED_IN]->(b)
return r

3.3更新

  • 給“Jone”增加屬性age = 40
match(n{name:'Jone'})
set n.age = 40
return n
 
  • 給“super man”增加description = “Hot”
match(n{name:'super man'})
set n.description = 'Hot'
return n
 
  • 給“Jone”和“super man”之間的關系增加description=“first”
match (a{name:'Jone'})-[r]->(b{name:'super man'})
set r.description = 'first'
return r

3.4刪除

  • 刪除id不同,名字相同的重復的演員實體
match (a:Actor),(b:Actor)
where id(a) <> id(b) and a.name = b.name
delete b
return b

3.5函數

  • 查詢name=“Jone”的節點的ID
match (n{name:'Jone'})
return id(n)
 
  • 查詢“Jone”和“super man”之間關系類型
match (a{name:'Jone'})-[r]->(b{name:'super man'})
return type(r)
 
  • 查詢name=“Jone”的節點的所有屬性名
match (n{name:'Jone'})
return keys(n)
 
  • 查詢name=“Jone”的節點的所有屬性名及值
match (n{name:'Jone'})
return properties(n)
 
  • 統計帶標簽“Movie”的節點數量
match (n:Movie)
with count(*) as f
return f
 
  • 給所有節點增加時間戳
match (n)
set n.timestamp = timestamp()
 

3.6路徑

  • 查詢與“Keanu Reeves”距離1-3度的節點
match (n{name:'Keanu Reeves'}) -[*1..3]- (m)
return m
 
  • 查詢“Laurence Fishburne”和“Keanu Reeves”的最短路徑
match p = shortestPath ((a{name:'Laurence Fishburne'})-[*]-(b{name:'Keanu Reeves'}))
return p

4.Python實現neo4j的訪問

from py2neo import Database, Graph, Node, Relationship

# 建立連接
db = Database("http://127.0.0.1:7474")
graph = Graph("bolt://127.0.0.1:7687", username="neo4j", password="123456")

try:
    for node in graph.nodes:
        print(node)
except:
    print("key error!")
    
# 匹配
n = graph.nodes.match("Keanu Reeves")
for i in n:
    print(i)
try:
    for r in graph.relationships:
        print(r)
except:
    print("key error!")
    
# 提交任務
tx = graph.begin()
a = Node("Actor", name="張鶴倫")
tx.create(a)
b = Node("Actor", name="楊九郎")
ab = Relationship(a, "師兄弟", b)
tx.create(ab)
tx.commit()

# 判斷是否存在
isExists = graph.exists(ab)
print("is Exists=" + str(isExists))

# 執行CQL命令
graph.run('create(p:Actor{name:"周九良"})')
ans = graph.run('match(p:Actor) return p.name,p.born').to_ndarray()
print(ans)

 

參考資料:
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM