1. 程式人生 > >Neo4j圖資料庫簡介和底層原理

Neo4j圖資料庫簡介和底層原理

http://www.cnblogs.com/bonelee/p/6211290.html

現實中很多資料都是用圖來表達的,比如社交網路中人與人的關係、地圖資料、或是基因資訊等等。RDBMS並不適合表達這類資料,而且由於海量資料的存在,讓其顯得捉襟見肘。NoSQL資料庫的興起,很好地解決了海量資料的存放問題,圖資料庫也是NoSQL的一個分支,相比於NoSQL中的其他分支,它很適合用來原生表達圖結構的資料。

下面一張圖說明,相比於其他NoSQL,圖資料庫存放的資料規模有所下降,但是更能夠表達複雜的資料。

通常來說,一個圖資料庫儲存的結構就如同資料結構中的圖,由頂點和邊組成。

Neo4j是圖資料庫中一個主要代表,其開源,且用Java實現。經過幾年的發展,已經可以用於生產環境。其有兩種執行方式,一種是服務的方式,對外提供REST介面;另外一種是嵌入式模式,資料以檔案的形式存放在本地,可以直接對本地檔案進行操作。

Neo4j簡介

Neo4j是一個高效能的,NOSQL圖形資料庫,它將結構化資料儲存在網路上而不是表中。Neo4j也可以被看作是一個高效能的圖引擎,該引擎具有成熟資料庫的所有特性。程式設計師工作在一個面向物件的、靈活的網路結構下而不是嚴格、靜態的表中——但是他們可以享受到具備完全的事務特性、企業級的資料庫的所有好處。

Neo4j因其嵌入式、高效能、輕量級等優勢,越來越受到關注。

圖形資料結構

在一個圖中包含兩種基本的資料型別:Nodes(節點) 和 Relationships(關係)。Nodes 和 Relationships 包含key/value形式的屬性。Nodes通過Relationships所定義的關係相連起來,形成關係型網路結構。

從這幾個方面來說,Neo4j是一個合適的選擇。Neo4j……

  • 自帶一套易於學習的查詢語言(名為Cypher
  • 不使用schema,因此可以滿足你的任何形式的需求
  • 與關係型資料庫相比,對於高度關聯的資料(圖形資料)的查詢快速要快上許多
  • 它的實體與關係結構非常自然地切合人類的直觀感受
  • 支援相容ACID的事務操作
  • 提供了一個高可用性模型,以支援大規模資料量的查詢,支援備份、資料區域性性以及冗餘
  • 提供了一個視覺化的查詢控制檯,你不會對它感到厭倦的

什麼時候不應使用Neo4j?

作為一個圖形NoSQL資料庫,Neo4j提供了大量的功能,但沒有什麼解決方案是完美的。在以下這些用例中,Neo4j就不是非常適合的選擇:

  • 記錄大量基於事件的資料(例如日誌條目或感測器資料)
  • 對大規模分散式資料進行處理,類似於Hadoop
  • 二進位制資料儲存
  • 適合於儲存在關係型資料庫中的結構化資料

neo4j儲存模型

The node records contain only a pointer to their first property and their first relationship (in what is oftentermed the _relationship chain). From here, we can follow the (doubly) linked-list of relationships until we find the one we’re interested in, the LIKES relationship from Node 1 to Node 2 in this case. Once we’ve found the relationship record of interest, we can simply read its properties if there are any via the same singly-linked list structure as node properties, or we can examine the node records that it relates via its start node and end node IDs. These IDs, multiplied by the node record size, of course give the immediate offset of both nodes in the node store file.

上面的英文摘自<Graph Databases>(作者:IanRobinson) 一書,描述了 neo4j 的儲存模型。Node和Relationship 的 Property 是用一個 Key-Value 的雙向列表來儲存的; Node 的 Relatsionship 是用一個雙向列表來儲存的,通過關係,可以方便的找到關係的 from-to Node. Node 節點儲存第1個屬性和第1個關係ID。

通過上述儲存模型,從一個Node-A開始,可以方便的遍歷以該Node-A為起點的圖。下面給個示例,來幫助理解上面的儲存模型,儲存檔案的具體格式在第2章詳細描述。

示例1


在這個例子中,A~E表示Node 的編號,R1~R7 表示 Relationship 編號,P1~P10 表示Property 的編號。

  • Node 的儲存示例圖如下,每個Node 儲存了第1個Property 和 第1個Relationship
  • 關係的儲存示意圖如下:

    從示意圖可以看出,從 Node-B 開始,可以通過關係的 next 指標,遍歷Node-B 的所有關係,然後可以到達與其有關係的第1層Nodes,在通過遍歷第1層Nodes的關係,可以達到第2層Nodes,…

neo4j graph db的儲存檔案介紹

當我們下載neo4j-community-2.1.0-M01 並安裝,然後拿 neo4j embedded-example 的EmbeddedNeo4j 例子跑一下,可以看到在target/neo4j-hello-db下會生成如下neo4j graph db 的儲存檔案。

-rw-rr–     11 04-11 13:28 active_tx_log

drwxr-xr-x   4096 04-11 13:28 index

-rw-rr–  23740 04-11 13:28 messages.log

-rw-rr–     78 04-11 13:28 neostore

-rw-rr–      9 04-11 13:28 neostore.id

-rw-rr–     22 04-11 13:28 neostore.labeltokenstore.db

-rw-rr–      9 04-11 13:28 neostore.labeltokenstore.db.id

-rw-rr–     64 04-11 13:28 neostore.labeltokenstore.db.names

-rw-rr–      9 04-11 13:28 neostore.labeltokenstore.db.names.id

-rw-rr–     61 04-11 13:28 neostore.nodestore.db

-rw-rr–      9 04-11 13:28 neostore.nodestore.db.id

-rw-rr–     93 04-11 13:28 neostore.nodestore.db.labels

-rw-rr–      9 04-11 13:28 neostore.nodestore.db.labels.id

-rw-rr–    307 04-11 13:28 neostore.propertystore.db

-rw-rr–    153 04-11 13:28 neostore.propertystore.db.arrays

-rw-rr–      9 04-11 13:28 neostore.propertystore.db.arrays.id

-rw-rr–      9 04-11 13:28 neostore.propertystore.db.id

-rw-rr–     61 04-11 13:28 neostore.propertystore.db.index

-rw-rr–      9 04-11 13:28 neostore.propertystore.db.index.id

-rw-rr–    216 04-11 13:28 neostore.propertystore.db.index.keys

-rw-rr–      9 04-11 13:28 neostore.propertystore.db.index.keys.id

-rw-rr–    410 04-11 13:28 neostore.propertystore.db.strings

-rw-rr–      9 04-11 13:28 neostore.propertystore.db.strings.id

-rw-rr–     69 04-11 13:28 neostore.relationshipgroupstore.db

-rw-rr–      9 04-11 13:28 neostore.relationshipgroupstore.db.id

-rw-rr–     92 04-11 13:28 neostore.relationshipstore.db

-rw-rr–      9 04-11 13:28 neostore.relationshipstore.db.id

-rw-rr–     38 04-11 13:28 neostore.relationshiptypestore.db

-rw-rr–      9 04-11 13:28 neostore.relationshiptypestore.db.id

-rw-rr–    140 04-11 13:28 neostore.relationshiptypestore.db.names

-rw-rr–      9 04-11 13:28 neostore.relationshiptypestore.db.names.id

-rw-rr–     82 04-11 13:28 neostore.schemastore.db

-rw-rr–      9 04-11 13:28 neostore.schemastore.db.id

-rw-rr–      4 04-11 13:28 nioneo_logical.log.active

-rw-rr–   2249 04-11 13:28 nioneo_logical.log.v0

drwxr-xr-x   4096 04-11 13:28 schema

-rw-rr–      0 04-11 13:28 store_lock

-rw-rr–    800 04-11 13:28 tm_tx_log.1