轉自:https://cloud.tencent.com/developer/article/1336299javascript
Neo4j是一個高性能的,NOSQL圖形數據庫,它將結構化數據存儲在網絡上而不是表中。它是一個嵌入式的、基於磁盤的、具有徹底的事務特性的Java持久化引擎,可是它將結構化數據存儲在網絡(從數學角度叫作圖)上而不是表中。Neo4j也能夠被看做是一個高性能的圖引擎,該引擎具備成熟數據庫的全部特性。程序員工做在一個面向對象的、靈活的網絡結構下而不是嚴格、靜態的表中——可是他們能夠享受到具有徹底的事務特性、企業級的數據庫的全部好處。php
Centos 7.4html
neo4j-community-3.4.1.tar.gzjava
下載地址 https://neo4j.com/download/other-releases/node
下載git
wget https://neo4j.com/artifact.php?name=neo4j-community-3.4.1-unix.tar.gz
解壓程序員
tar -zxvf neo4j-community-3.4.1.tar.gz
1、對於3.0之前的版本github
在安裝目錄的 $NEO4J_HOME/conf/neo4j.conf 文件內,找到下面一行,將註釋#號去掉就能夠了 #dbms.connector.https.address=localhost:7473
改成 dbms.connector.https.address=0.0.0.0:7473
這樣,遠程其餘電腦能夠用本機的IP或者域名後面跟上7474 端口就能打開web界面了 如: https://:7473web
固然,你的操做系統的防火牆也要確保開放了7474端口才行,防火牆怎樣開放請自行鍼對本身的操做系統查找文檔spring
2、對於3.1及之後的版本
在安裝目錄的 $NEO4J_HOME/conf/neo4j.conf 文件內,找到下面一行,將註釋#號去掉就能夠了 dbms.connectors.default_listen_address=0.0.0.0
在bin目錄下,執行命令:./neo4j start啓動,其餘命令 { console | start | stop | restart | status }
訪問http://IP地址:7474/
, 出現下圖即表明安裝成功,頂部的$輸入框用來執行下面的CQL語句。
CQL表明Cypher查詢語言。 像Oracle數據庫具備查詢語言SQL,Neo4j具備CQL做爲查詢語言。
Neo4j CQL -
如Oracle SQL -
經常使用的Neo4j CQL命令/條款以下:
S.No. |
CQL命令/條 |
用法 |
---|---|---|
1。 |
CREATE 建立 |
建立節點,關係和屬性 |
2。 |
MATCH 匹配 |
檢索有關節點,關係和屬性數據 |
3。 |
RETURN 返回 |
返回查詢結果 |
4。 |
WHERE 哪裏 |
提供條件過濾檢索數據 |
5。 |
DELETE 刪除 |
刪除節點和關係 |
6。 |
REMOVE 移除 |
刪除節點和關係的屬性 |
7。 |
ORDER BY以…排序 |
排序檢索數據 |
8。 |
SET 組 |
添加或更新標籤 |
如下是經常使用的Neo4j CQL函數:
S.No. |
定製列表功能 |
用法 |
---|---|---|
1。 |
String 字符串 |
它們用於使用String字面量。 |
2。 |
Aggregation 聚合 |
它們用於對CQL查詢結果執行一些聚合操做。 |
3。 |
Relationship 關係 |
他們用於獲取關係的細節,如startnode,endnode等。 |
咱們將在後面的章節中詳細討論全部Neo4j CQL命令,子句和函數語法,用法和示例。
這些數據類型與Java語言相似。 它們用於定義節點或關係的屬性
Neo4j CQL支持如下數據類型:
S.No. |
CQL數據類型 |
用法 |
---|---|---|
1. |
boolean |
用於表示布爾文字:true,false。 |
2. |
byte |
用於表示8位整數。 |
3. |
short |
用於表示16位整數。 |
4. |
int |
用於表示32位整數。 |
5. |
long |
用於表示64位整數。 |
6. |
float |
I用於表示32位浮點數。 |
7. |
double |
用於表示64位浮點數。 |
8. |
char |
用於表示16位字符。 |
9. |
String |
用於表示字符串。 |
Neo4j CQL建立一個沒有屬性的節點
CREATE (<node-name>:<label-name>)
語法說明
規範說法是節點標籤名稱,其實至關於Mysql數據庫中的表名,而是節點名稱,其實代指建立的此行數據。
示例
CREATE (emp:Employee)
或者
CREATE (:Employee)
Neo4j CQL建立具備屬性的節點
Neo4j CQL「CREATE」命令用於建立帶有屬性的節點。 它建立一個具備一些屬性(鍵值對)的節點來存儲數據。
CREATE ( <node-name>:<label-name> { <key>:<Value> ........ <n-key>:<n-Value> } )
示例
CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })
建立多個標籤到節點
語法:
CREATE (<node-name>:<label-name1>:<label-name2>.....:<label-namen>)
示例
CREATE (m:Movie:Cinema:Film:Picture)
Neo4j CQL MATCH命令用於
MATCH命令語法:
MATCH ( <node-name>:<label-name> )
示例
MATCH (dept:Dept)
可是執行後會報錯:
Neo.ClientError.Statement.SyntaxError: Query cannot conclude with MATCH (must be RETURN or an update clause) (line 1, column 1 (offset: 0))
若是你觀察到錯誤消息,它告訴咱們,咱們可使用MATCH命令與RETURN子句或UPDATA子句。
Neo4j CQL RETURN子句用於 -
RETURN命令語法:
RETURN <node-name>.<property1-name>, ........ <node-name>.<propertyn-name>
示例
MATCH (e:Employee) RETURN e
或
MATCH (dept: Dept) RETURN dept.deptno,dept.dname,dept.location
Neo4j圖數據庫遵循屬性圖模型來存儲和管理其數據。
根據屬性圖模型,關係應該是定向的。 不然,Neo4j將拋出一個錯誤消息。
基於方向性,Neo4j關係被分爲兩種主要類型。
使用新節點建立關係
示例
CREATE (e:Employee)-[r:DemoRelation]->(c:Employee)
這句會建立節點e,節點c,以及e -> c的關係r,這裏須要注意方向,好比雙向是
CREATE (e:Employee)<-[r:DemoRelation]->(c:Employee)
使用已知節點建立帶屬性的關係:
MATCH (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) CREATE (<node1-label-name>)-[<relationship-label-name>:<relationship-name> {<define-properties-list>}]->(<node2-label-name>) RETURN <relationship-label-name>
仍是一系列鍵值對
示例
MATCH (cust:Customer),(cc:CreditCard) CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) RETURN r
檢索關係節點的詳細信息:
MATCH (<node1-label-name>)-[<relationship-label-name>:<relationship-name>]->(<node2-label-name>) RETURN <relationship-label-name>
示例
MATCH (cust)-[r:DO_SHOPPING_WITH]->(cc) RETURN cust,cc
像SQL同樣,Neo4j CQL在CQL MATCH命令中提供了WHERE子句來過濾MATCH查詢的結果。
簡單WHERE子句語法
WHERE <property-name> <comparison-operator> <value>
語法說明:
S.No. |
語法元素 |
描述 |
---|---|---|
1 |
WHERE |
它是一個Neo4j CQL關鍵字。 |
2 |
<屬性名稱> |
它是節點或關係的屬性名稱。 |
3 |
<比較運算符> |
它是Neo4j CQL比較運算符之一。 |
4 |
<值> |
它是一個字面值,如數字文字,字符串文字等。 |
Neo4j CQL中的比較運算符
Neo4j 支持如下的比較運算符,在 Neo4j CQL WHERE 子句中使用來支持條件
S.No. |
布爾運算符 |
描述 |
---|---|---|
1. |
= |
它是Neo4j CQL「等於」運算符。 |
2. |
<> |
它是一個Neo4j CQL「不等於」運算符。 |
3. |
< |
它是一個Neo4j CQL「小於」運算符。 |
4. |
> |
它是一個Neo4j CQL「大於」運算符。 |
5. |
<= |
它是一個Neo4j CQL「小於或等於」運算符。 |
6. |
= |
它是一個Neo4j CQL「大於或等於」運算符。 |
咱們可使用布爾運算符在同一命令上放置多個條件。
Neo4j CQL中的布爾運算符
Neo4j支持如下布爾運算符在Neo4j CQL WHERE子句中使用以支持多個條件。
S.No. |
布爾運算符 |
描述 |
---|---|---|
1 |
AND |
它是一個支持AND操做的Neo4j CQL關鍵字。 |
2 |
OR |
它是一個Neo4j CQL關鍵字來支持OR操做。 |
3 |
NOT |
它是一個Neo4j CQL關鍵字支持NOT操做。 |
4 |
XOR |
它是一個支持XOR操做的Neo4j CQL關鍵字。 |
示例
MATCH (emp:Employee) WHERE emp.name = 'Abc' OR emp.name = 'Xyz' RETURN emp
利用WHERE
建立指定關係節點:
MATCH (cust:Customer),(cc:CreditCard) WHERE cust.id = "1001" AND cc.id= "5001" CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) RETURN r
有必要補充一下,能夠不使用WHERE達到WHERE
的一些效果,好比
MATCH p=(m:Bot{id:123})<-[:BotRelation]->(:Bot) RETURN p
Neo4j使用CQL DELETE子句
DELETE節點子句語法
DELETE <node-name-list>
示例
MATCH (e: Employee) DELETE e
DELETE節點和關係子句語法
DELETE <node1-name>,<node2-name>,<relationship-name>
示例
MATCH (cc: CreditCard)-[rel]-(c:Customer) DELETE cc,c,rel
有時基於咱們的客戶端要求,咱們須要向現有節點或關係添加或刪除屬性。
咱們使用Neo4j CQL SET子句向現有節點或關係添加新屬性。
咱們使用Neo4j CQL REMOVE子句來刪除節點或關係的現有屬性。
Neo4j CQL REMOVE命令用於
Neo4j CQL DELETE和REMOVE命令之間的主要區別 -
Neo4j CQL DELETE和REMOVE命令之間的類似性 -
1.REMOVE屬性子句語法
REMOVE <node-name>.<property1-name>,<node-name>.<property2-name>
語法說明:
S.No. |
語法元素 |
描述 |
---|---|---|
1。 |
|
它是節點的名稱。 |
2。 |
|
它是節點的屬性名稱。 |
示例
這裏咱們能夠觀察到DebitCard節點包含6個屬性。
在數據瀏覽器上鍵入如下命令刪除cvv
屬性
MATCH (dc:DebitCard) REMOVE dc.cvv RETURN dc
2.REMOVE一個Label子句語法:
REMOVE <label-name-list>
S.No. |
語法元素 |
描述 |
---|---|---|
1. |
REMOVE |
它是一個Neo4j CQL關鍵字。 |
2. |
|
它是一個標籤列表,用於永久性地從節點或關係中刪除它。 |
語法
<node-name>:<label2-name>, .... <node-name>:<labeln-name>
示例
1.咱們建立一個含有兩個標籤的節點:
CREATE (m:Movie:Pic)
3.刪除標籤
MATCH (m:Movie) REMOVE m:Pic
4.再次查詢
有時,根據咱們的客戶端要求,咱們須要向現有節點或關係添加新屬性。
要作到這一點,Neo4j CQL提供了一個SET子句。
Neo4j CQL已提供SET子句來執行如下操做。
SET子句語法
SET <node-label-name>.<property1-name>,...<node-laben-name>.<propertyn-name>
語法說明:
S.No. |
語法元素 |
描述 |
---|---|---|
1 |
<節點標籤名稱> |
這是一個節點的標籤名稱。 |
2 |
<屬性名稱> |
它是一個節點的屬性名。 |
示例
MATCH (dc:DebitCard) SET dc.atm_pin = 3456 RETURN dc
Neo4j CQL ORDER BY子句
Neo4j CQL在MATCH命令中提供了「ORDER BY」子句,對MATCH查詢返回的結果進行排序。
咱們能夠按升序或降序對行進行排序。
默認狀況下,它按升序對行進行排序。 若是咱們要按降序對它們進行排序,咱們須要使用DESC子句。
ORDER BY子句語法
ORDER BY <property-name-list> [DESC]
語法:
<node-label-name>.<property1-name>, <node-label-name>.<property2-name>, .... <node-label-name>.<propertyn-name>
S.No. |
語法元素 |
描述 |
---|---|---|
1。 |
|
它是節點的標籤名稱。 |
2。 |
|
它是節點的屬性名稱。 |
示例
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno ORDER BY emp.name
與SQL同樣,Neo4j CQL有兩個子句,將兩個不一樣的結果合併成一組結果
UNION子句
它將兩組結果中的公共行組合並返回到一組結果中。 它不從兩個節點返回重複的行。
限制:
結果列類型和來自兩組結果的名稱必須匹配,這意味着列名稱應該相同,列的數據類型應該相同。
UNION子句語法
<MATCH Command1> UNION <MATCH Command2>
S.No. |
語法元素 |
描述 |
---|---|---|
1。 |
|
它是CQL MATCH命令,由UNION子句使用。 |
2。 |
|
它是CQL MATCH命令兩個由UNION子句使用。 |
3。 |
UNION |
它是UNION子句的Neo4j CQL關鍵字。 |
注意 -
若是這兩個查詢不返回相同的列名和數據類型,那麼它拋出一個錯誤。
示例
MATCH (cc:CreditCard) RETURN cc.id,cc.number UNION MATCH (dc:DebitCard) RETURN dc.id,dc.number
UNION ALL子句
它結合並返回兩個結果集的全部行成一個單一的結果集。它還返回由兩個節點重複行。
限制
結果列類型,並從兩個結果集的名字必須匹配,這意味着列名稱應該是相同的,列的數據類型應該是相同的。
UNION ALL子句語法
<MATCH Command1> UNION ALL <MATCH Command2>
示例
MATCH (cc:CreditCard) RETURN cc.id,cc.number UNION ALL MATCH (dc:DebitCard) RETURN dc.id,dc.number
Neo4j CQL已提供LIMIT
子句和SKIP
來過濾或限制查詢返回的行數。
簡單來講:LIMIT
返回前幾行,SKIP
返回後幾行。
LIMIT
示例
MATCH (emp:Employee) RETURN emp LIMIT 2
它只返回Top的兩個結果,由於咱們定義了limit = 2。這意味着前兩行。
SKIP
示例
MATCH (emp:Employee) RETURN emp SKIP 2
它只返回來自Bottom的兩個結果,由於咱們定義了skip = 2。這意味着最後兩行。
Neo4j使用CQL MERGE命令 -
MERGE命令是CREATE命令和MATCH命令的組合。
MERGE = CREATE + MATCH
Neo4j CQL MERGE命令在圖中搜索給定模式,若是存在,則返回結果
若是它不存在於圖中,則它建立新的節點/關係並返回結果。
Neo4j CQL MERGE語法
MERGE (<node-name>:<label-name> { <key>:<1-Value> ..... <n-key>:<n-Value> })
注意 -
Neo4j CQL MERGE命令語法與CQL CREATE命令相似。
咱們將使用這兩個命令執行如下操做 -
咱們將使用CREATE命令執行這些操做:
MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})
MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})
MATCH (gp1:GoogleProfile1) RETURN gp1.Id,gp1.Name
若是咱們觀察上面的查詢結果,它只顯示一行,由於CQL MERGE命令檢查該節點在數據庫中是否可用。 若是它不存在,它建立新節點。 不然,它不建立新的。
經過觀察這些結果,咱們能夠說,CQL MERGE命令將新的節點添加到數據庫,只有當它不存在。
Neo4j CQL將空值視爲對節點或關係的屬性的缺失值或未定義值。
當咱們建立一個具備現有節點標籤名稱但未指定其屬性值的節點時,它將建立一個具備NULL屬性值的新節點。
讓咱們用一個例子來看這個。
MATCH (e:Employee) WHERE e.id IS NOT NULL RETURN e.id,e.name,e.sal,e.deptno
提供了一個WHERE子句來過濾該行,即Id屬性不該該包含NULL值。
MATCH (e:Employee) WHERE e.id IS NULL RETURN e.id,e.name,e.sal,e.deptno
這裏咱們使用IS操做符來僅返回NULL行。
與SQL同樣,Neo4j CQL提供了一個IN運算符,以便爲CQL命令提供值的集合。
IN操做符語法
IN[<Collection-of-values>]
它是由逗號運算符分隔的值的集合。
示例
MATCH (e:Employee) WHERE e.id IN [123,124] RETURN e.id,e.name,e.sal,e.deptno
Neo4j SQL支持節點或關係屬性上的索引,以提升應用程序的性能。
咱們能夠爲具備相同標籤名稱的全部節點的屬性建立索引。
咱們能夠在MATCH或WHERE或IN運算符上使用這些索引列來改進CQL Command的執行。
Neo4J索引操做
咱們將在本章中用示例來討論這些操做。
建立索引的語法:
CREATE INDEX ON :<label_name> (<property_name>)
注意:-
冒號(:)運算符用於引用節點或關係標籤名稱。
上述語法描述它在節點或關係的的上建立一個新索引。
示例
CREATE INDEX ON :Customer (name)
刪除索引的語法:
DROP INDEX ON :<label_name> (<property_name>)
示例
DROP INDEX ON :Customer (name)
在Neo4j數據庫中,CQL CREATE命令始終建立新的節點或關係,這意味着即便您使用相同的值,它也會插入一個新行。 根據咱們對某些節點或關係的應用需求,咱們必須避免這種重複。 而後咱們不能直接獲得這個。 咱們應該使用一些數據庫約束來建立節點或關係的一個或多個屬性的規則。
像SQL同樣,Neo4j數據庫也支持對NODE或Relationship的屬性的UNIQUE約束
UNIQUE約束的優勢
建立惟一約束語法
CREATE CONSTRAINT ON (<label_name>) ASSERT <property_name> IS UNIQUE
語法說明:
S.No. |
語法元素 |
描述 |
---|---|---|
1。 |
CREATE CONSTRAINT ON |
它是一個Neo4j CQL關鍵字。 |
2。 |
|
它是節點或關係的標籤名稱。 |
3。 |
ASSERT |
它是一個Neo4j CQL關鍵字。 |
4。 |
|
它是節點或關係的屬性名稱。 |
5。 |
IS UNIQUE |
它是一個Neo4j CQL關鍵字,通知Neo4j數據庫服務器建立一個惟一約束。 |
注意:-
上述語法描述了只須要 節點或關係就能夠創造一個獨特的約束。
示例
注意
若是建立約束時節點屬性有重複值,Neo4j DB服務器將會拋出一個錯誤,表示沒法建立。
刪除UNIQUE約束語法:
示例
這個函數的用法就像SQL中的distinct關鍵字,返回的是全部不一樣值。
示例
返回的是
關於Neo4j提供的圖形瀏覽器,咱們能夠從其中查看節點的屬性,或者改變其中的節點的大小顏色。
在Neo4j中,「Id」是節點和關係的默認內部屬性。 這意味着,當咱們建立一個新的節點或關係時,Neo4j數據庫服務器將爲內部使用分配一個數字。 它會自動遞增。
咱們從一個例子去看:
新增一個節點
查看該節點
所謂的Caption標題,就是更改Neo4j瀏覽器的節點顯示的文字(圓圈內部)。好比
咱們點擊下圖所示:
圓圈內部變爲了id值。
與SQL同樣,Neo4J CQL提供了一組String函數,用於在CQL查詢中獲取所需的結果。
這裏咱們將討論一些重要的和常用的功能。
字符串函數列表
S.No. |
功能 |
描述 |
---|---|---|
1。 |
UPPER |
它用於將全部字母更改成大寫字母。 |
2。 |
LOWER |
它用於將全部字母改成小寫字母。 |
3。 |
SUBSTRING |
它用於獲取給定String的子字符串。 |
4。 |
REPLACE |
它用於替換一個字符串的子字符串。 |
注意:全部CQL函數應使用「()」括號。
如今咱們將經過示例詳細討論每一個Neo4J CQL字符串函數
1.UPPER
它須要一個字符串做爲輸入並轉換爲大寫字母。 全部CQL函數應使用「()」括號。
函數語法
UPPER (<input-string>)
注意:-
能夠是來自Neo4J數據庫的節點或關係的屬性名稱。
示例
MATCH (e:Employee) RETURN e.id,UPPER(e.name),e.sal,e.deptno
2.LOWER
它須要一個字符串做爲輸入並轉換爲小寫字母。 全部CQL函數應使用「()」括號。
函數語法
LOWER (<input-string>)
注意:-
能夠是來自Neo4J數據庫的節點或關係的屬性名稱
MATCH (e:Employee) RETURN e.id,LOWER(e.name),e.sal,e.deptno
3.SUBSTRING
它接受一個字符串做爲輸入和兩個索引:一個是索引的開始,另外一個是索引的結束,並返回從StartInded到EndIndex-1的子字符串。 全部CQL函數應使用「()」括號。
函數的語法
SUBSTRING(<input-string>,<startIndex> ,<endIndex>)
注意:-
在Neo4J CQL中,若是一個字符串包含n個字母,則它的長度爲n,索引從0開始,到n-1結束。
是SUBSTRING函數的索引值。
是可選的。 若是咱們省略它,那麼它返回給定字符串的子串從startIndex到字符串的結尾。
示例
MATCH (e:Employee) RETURN e.id,SUBSTRING(e.name,0,2),e.sal,e.deptno
和SQL同樣,Neo4j CQL提供了一些在RETURN子句中使用的聚合函數。 它相似於SQL中的GROUP BY子句。
咱們可使用MATCH命令中的RETURN +聚合函數來處理一組節點並返回一些聚合值。
聚合函數列表
S.No. |
彙集功能 |
描述 |
---|---|---|
1。 |
COUNT |
它返回由MATCH命令返回的行數。 |
2。 |
MAX |
它從MATCH命令返回的一組行返回最大值。 |
3。 |
MIN |
它返回由MATCH命令返回的一組行的最小值。 |
4。 |
SUM |
它返回由MATCH命令返回的全部行的求和值。 |
5。 |
AVG |
它返回由MATCH命令返回的全部行的平均值。 |
如今咱們將經過示例詳細討論每一個Neo4j CQL AGGREGATION函數
計數
它從MATCH子句獲取結果,並計算結果中出現的行數,並返回該計數值。 全部CQL函數應使用「()」括號。
函數語法
COUNT(<value>)
注意 -
能夠是*,節點或關係標籤名稱或屬性名稱。
示例
MATCH (e:Employee) RETURN COUNT(*)
MAX
它採用一組行和節點或關係的做爲輸入,並從給定行的give 列中查找最小值。
函數語法
MAX(<property-name> )
MIN
它採用一組行和節點或關係的做爲輸入,並從給定行的give 列中查找最小值。
函數語法
MIN(<property-name> )
注意 -
應該是節點或關係的名稱。
讓咱們用一個例子看看MAX和MIN的功能。
示例
MATCH (e:Employee) RETURN MAX(e.sal),MIN(e.sal)
AVG
它採用一組行和節點或關係的做爲輸入,並從給定行的give 列中查找平均值。
函數的語法
AVG(<property-name> )
SUM
它採用一組行和節點或關係的做爲輸入,並從給定行的give 列中查找求和值。
函數的語法
SUM(<property-name> )
讓咱們用一個例子來檢查SUM和AVG函數。
MATCH (e:Employee) RETURN SUM(e.sal),AVG(e.sal)
此命令從數據庫中可用的全部Employee節點查找總和平均值.
Neo4j CQL提供了一組關係函數,以在獲取開始節點,結束節點等細節時知道關係的細節。
關係函數列表
S.No. |
功能 |
描述 |
---|---|---|
1。 |
STARTNODE |
它用於知道關係的開始節點。 |
2。 |
ENDNODE |
它用於知道關係的結束節點。 |
3。 |
ID |
它用於知道關係的ID。 |
4。 |
TYPE |
它用於知道字符串表示中的一個關係的TYPE。 |
如今咱們將經過示例詳細討論每一個Neo4j CQL關係函數
STARTNODE
它須要一個字符串做爲輸入並轉換爲大寫字母。 全部CQL函數應使用「()」括號。
函數語法
STARTNODE (<relationship-label-name>)
注意:-
能夠是來自Neo4j數據庫的節點或關係的屬性名稱。
示例
MATCH (a)-[movie:ACTION_MOVIES]->(b) RETURN STARTNODE(movie)
ENDNODE
MATCH (a)-[movie:ACTION_MOVIES]->(b) RETURN ENDNODE(movie)
ID TYPE
MATCH (a)-[movie:ACTION_MOVIES]->(b) RETURN ID(movie),TYPE(movie)
在對Neo4j數據進行備份、還原、遷移的操做時,首先要關閉neo4j;
cd %NEO4J_HOME%/bin
./neo4j stop
數據備份到文件
./neo4j-admin dump --database=graph.db --to=/home/2018.dump
以後,進行數據還原,將生成的存儲文件拷貝到另外一個相同版本的環境中。
還原、遷移以前 ,關閉neo4j服務。操做同上;
數據導入:
./neo4j-admin load --from=/home/2016-10-02.dump --database=graph.db --force
重啓服務:
./neo4j start
Neo4j提供JAVA API以編程方式執行全部數據庫操做。
具體Neo4j如何在原生Java程序編程,以及與Spring的集成,本章暫不討論。
Spring數據模塊的優勢:
Spring數據模塊功能:
Spring DATA Neo4j模塊具備與上述相同的優勢和特色。
接下來,咱們將基於Spring Boot在IDEA上開發Neo4j應用程序,須要注意的是Springboot的版本
咱們選擇web和Neo4j兩個依賴便可,這裏有必要說一下,若是你是使用Spring boot2.0以上,在你建立項目完成後,啓動程序會報錯:
Caused by: java.lang.ClassNotFoundException: org.neo4j.ogm.drivers.http.driver.HttpDriver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_111] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_111] at java.lang.Class.forName0(Native Method) ~[na:1.8.0_111] at java.lang.Class.forName(Class.java:264) ~[na:1.8.0_111] at org.neo4j.ogm.session.SessionFactory.newDriverInstance(SessionFactory.java:92) ~[neo4j-ogm-core-3.1.0.jar:3.1.0] ... 45 common frames omitted
緣由是缺乏依賴,解決方法是導入缺乏的依賴:
<dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-http-driver</artifactId> </dependency>
若是你的Spring boot版本爲1.5.x,那麼你只須要spring-data-neo4j便可:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency>
新建節點類,id
的屬性爲Long
而不能爲long
,還須要注意的是在Spring boot1.5中修飾id
屬性的註釋爲@GraphId
,org.neo4j.ogm.annotation.Id
不存在,效果同樣,都是Neo4j數據庫自動建立的ID值。
@NodeEntity(label = "Bot")
public class BotNode {
@Id
@GeneratedValue
private Long id; //id
@Property(name = "name")
private String name;//名
@Property(name = "kind")
private String kind;//類
@Property(name = "weight")
private long weight;//權重
<span class="token keyword">public</span> <span class="token function">BotNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">BotNode</span><span class="token punctuation">(</span>Long id<span class="token punctuation">,</span> String name<span class="token punctuation">,</span> String kind<span class="token punctuation">,</span> long weight<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>kind <span class="token operator">=</span> kind<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>weight <span class="token operator">=</span> weight<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Long <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span>Long id<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getKind</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> kind<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setKind</span><span class="token punctuation">(</span>String kind<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>kind <span class="token operator">=</span> kind<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> long <span class="token function">getWeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> weight<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setWeight</span><span class="token punctuation">(</span>long weight<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>weight <span class="token operator">=</span> weight<span class="token punctuation">;</span> <span class="token punctuation">}</span> @Override <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"BotNode{"</span> <span class="token operator">+</span> <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span> <span class="token string">", name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">", kind='"</span> <span class="token operator">+</span> kind <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">", weight="</span> <span class="token operator">+</span> weight <span class="token operator">+</span> <span class="token string">'}'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token function">BotNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">BotNode</span><span class="token punctuation">(</span>Long id<span class="token punctuation">,</span> String name<span class="token punctuation">,</span> String kind<span class="token punctuation">,</span> long weight<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>kind <span class="token operator">=</span> kind<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>weight <span class="token operator">=</span> weight<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Long <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span>Long id<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getKind</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> kind<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setKind</span><span class="token punctuation">(</span>String kind<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>kind <span class="token operator">=</span> kind<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> long <span class="token function">getWeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> weight<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setWeight</span><span class="token punctuation">(</span>long weight<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>weight <span class="token operator">=</span> weight<span class="token punctuation">;</span> <span class="token punctuation">}</span> @Override <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"BotNode{"</span> <span class="token operator">+</span> <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span> <span class="token string">", name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">", kind='"</span> <span class="token operator">+</span> kind <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">", weight="</span> <span class="token operator">+</span> weight <span class="token operator">+</span> <span class="token string">'}'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
新建節點關係類
有必要說明一下, @StartNode
和@EndNode
註釋的類能夠不是同一個類。
@RelationshipEntity(type = 「BotRelation」)
public class BotRelation {
@Id
@GeneratedValue
private Long id;
@StartNode
private BotNode startNode;
@EndNode
private BotNode endNode;
@Property
private String relation;<span class="token keyword">public</span> <span class="token function">BotRelation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">BotRelation</span><span class="token punctuation">(</span>Long id<span class="token punctuation">,</span> BotNode startNode<span class="token punctuation">,</span> BotNode endNode<span class="token punctuation">,</span> String relation<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>startNode <span class="token operator">=</span> startNode<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>endNode <span class="token operator">=</span> endNode<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>relation <span class="token operator">=</span> relation<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getRelation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> relation<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setRelation</span><span class="token punctuation">(</span>String relation<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>relation <span class="token operator">=</span> relation<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Long <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span>Long id<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> BotNode <span class="token function">getStartNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> startNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setStartNode</span><span class="token punctuation">(</span>BotNode startNode<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>startNode <span class="token operator">=</span> startNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> BotNode <span class="token function">getEndNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> endNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEndNode</span><span class="token punctuation">(</span>BotNode endNode<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>endNode <span class="token operator">=</span> endNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> @Override <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"BotRelation{"</span> <span class="token operator">+</span> <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span> <span class="token string">", startNode="</span> <span class="token operator">+</span> startNode <span class="token operator">+</span> <span class="token string">", endNode="</span> <span class="token operator">+</span> endNode <span class="token operator">+</span> <span class="token string">", relation='"</span> <span class="token operator">+</span> relation <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">'}'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token function">BotRelation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">BotRelation</span><span class="token punctuation">(</span>Long id<span class="token punctuation">,</span> BotNode startNode<span class="token punctuation">,</span> BotNode endNode<span class="token punctuation">,</span> String relation<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>startNode <span class="token operator">=</span> startNode<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>endNode <span class="token operator">=</span> endNode<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>relation <span class="token operator">=</span> relation<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getRelation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> relation<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setRelation</span><span class="token punctuation">(</span>String relation<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>relation <span class="token operator">=</span> relation<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Long <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span>Long id<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> BotNode <span class="token function">getStartNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> startNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setStartNode</span><span class="token punctuation">(</span>BotNode startNode<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>startNode <span class="token operator">=</span> startNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> BotNode <span class="token function">getEndNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> endNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEndNode</span><span class="token punctuation">(</span>BotNode endNode<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>endNode <span class="token operator">=</span> endNode<span class="token punctuation">;</span> <span class="token punctuation">}</span> @Override <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"BotRelation{"</span> <span class="token operator">+</span> <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span> <span class="token string">", startNode="</span> <span class="token operator">+</span> startNode <span class="token operator">+</span> <span class="token string">", endNode="</span> <span class="token operator">+</span> endNode <span class="token operator">+</span> <span class="token string">", relation='"</span> <span class="token operator">+</span> relation <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">'}'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
咱們只須要使接口繼承Neo4jRepository就可使用該接口提供的一些基礎的增刪改查方法。
@Repository
public interface BotRepository extends Neo4jRepository<BotNode,Long> {
BotNode findAllByName(String name);
}
對於複雜的查詢咱們能夠參照上面講到的CQL語句執行。
@Repository
public interface BotRelationRepository extends Neo4jRepository<BotRelation,Long> {
//返回節點n以及n指向的全部節點與關係
@Query(「MATCH p=(n:Bot)-[r:BotRelation]->(m:Bot) WHERE id(n)={0} RETURN p」)
List<BotRelation> findAllByBotNode(BotNode botNode);<span class="token comment">//返回節點n以及n指向或指向n的全部節點與關係</span> @<span class="token function">Query</span><span class="token punctuation">(</span><span class="token string">"MATCH p=(n:Bot)<-[r:BotRelation]->(m:Bot) WHERE m.name={name} RETURN p"</span><span class="token punctuation">)</span> List<span class="token operator"><</span>BotRelation<span class="token operator">></span> <span class="token function">findAllBySymptom</span><span class="token punctuation">(</span>@<span class="token function">Param</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> String name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回節點n以及n指向或指向n的全部節點以及這些節點間的全部關係</span> @<span class="token function">Query</span><span class="token punctuation">(</span><span class="token string">"MATCH p=(n:Bot)<-[r:BotRelation]->(m:Bot)<-[:BotRelation]->(:Bot)<-[:BotRelation]->(n:Bot) WHERE n.name={name} RETURN p"</span><span class="token punctuation">)</span> List<span class="token operator"><</span>BotRelation<span class="token operator">></span> <span class="token function">findAllByStartNode</span><span class="token punctuation">(</span>@<span class="token function">Param</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> String name<span class="token punctuation">)</span><span class="token punctuation">;</span>
< <span class="token comment">//返回節點n以及n指向或指向n的全部節點與關係</span> @<span class="token function">Query</span><span class="token punctuation">(</span><span class="token string">"MATCH p=(n:Bot)<-[r:BotRelation]->(m:Bot) WHERE m.name={name} RETURN p"</span><span class="token punctuation">)</span> List<span class="token operator"><</span>BotRelation<span class="token operator">></span> <span class="token function">findAllBySymptom</span><span class="token punctuation">(</span>@<span class="token function">Param</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> String name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回節點n以及n指向或指向n的全部節點以及這些節點間的全部關係</span> @<span class="token function">Query</span><span class="token punctuation">(</span><span class="token string">"MATCH p=(n:Bot)<-[r:BotRelation]->(m:Bot)<-[:BotRelation]->(:Bot)<-[:BotRelation]->(n:Bot) WHERE n.name={name} RETURN p"</span><span class="token punctuation">)</span> List<span class="token operator"><</span>BotRelation<span class="token operator">></span> <span class="token function">findAllByStartNode</span><span class="token punctuation">(</span>@<span class="token function">Param</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> String name<span class="token punctuation">)</span><span class="token punctuation">;</span>
}
保存
@RunWith(SpringRunner.class)
@SpringBootTest
public class Neo4jApplicationTests {
@Autowired
MovieRepository movieRepository;@Test <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">contextLoads</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> movieRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Movie</span><span class="token punctuation">(</span><span class="token string">"《奧特曼》"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>movieRepository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
@Test <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">contextLoads</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> movieRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Movie</span><span class="token punctuation">(</span><span class="token string">"《奧特曼》"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>movieRepository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
查看打印:
[Movie{id=8183, name=’《奧特曼》’}]
保存成功!
補充
若是想保存關係的話
MedicalNode node = new MedicalNode(-1l,「節點」,「測試」);
medicalNodeRepository.save(node);
MedicalNode node1 = new MedicalNode(-1l,「節點」,「測試」);
medicalNodeRepository.save(node1);
medicalRelationRepository.save(new MedicalRelation(-1l,node,node1,「關係」));
更新
接下來咱們測試更新數據:
@Test
public void updata(){
Movie movie = movieRepository.findAllById(8183l);
movie.setName("《迪迦》");
movieRepository.save(movie);
System.out.println(movieRepository.findAll());
}
執行程序,報錯:
java.lang.NullPointerException
咱們看到程序執行的CQL語句爲:
MATCH (n:Movie
) WHERE n.id
= {id_0
} WITH n RETURN n, ID(n) Movieidid_0
而後咱們在Neo4j瀏覽器控制檯執行查詢語句:
這是爲何呢?在Neo4j中,根據Id查詢節點的語句爲:
MATCH (n:Movie) where id(n)=8183 RETURN n
咱們修改Repository層的查詢方法:
@Repository
public interface MovieRepository extends Neo4jRepository<Movie, Long> {
@Query(「MATCH (n:Movie) where id(n)={id} RETURN n」)
Movie findAllById(@Param(「id」) Long id);
}
再次執行更新程序,結果爲:
[Movie{id=8183, name=’《迪迦》’}]
更新成功!
換句話說,只要掌握了CQL語句,就基本啥都會了~!
還有,本書並不表明所有的Neo4j知識。
For more details and a complete list of settings, please see https://neo4j.com/docs/operations-manual/current/reference/configuration-settings/
#dbms.active_database=graph.db
#安裝Neo4j數據庫的各個配置路徑,默認使用$NEO4J_HOME下的路徑
#Paths of directories in the installation.
#dbms.directories.data=data
#dbms.directories.plugins=plugins
#dbms.directories.certificates=certificates 證書路徑
#dbms.directories.logs=logs 日誌路徑
#dbms.directories.lib=lib jar包路徑
#dbms.directories.run=run 運行路徑
#默認狀況下想load csv文件,只能把csv文件放到${NEO4J_HOME}/import目錄下,把下面的#刪除後,能夠在load csv時使用絕對路徑,這樣可能不安全
#This setting constrains all LOAD CSV
import files to be under the import
directory. Remove or comment it out to allow files to be loaded from anywhere in the filesystem; this introduces possible security problems. See the LOAD CSV
section of the manual for details.
#此設置將全部「LOAD CSV」導入文件限制在import
目錄下。刪除註釋容許從文件系統的任何地方加載文件;這引入了可能的安全問題。
dbms.directories.import=import
#把下面這行的#刪掉後,鏈接neo4j數據庫時就不用輸密碼了
#Whether requests to Neo4j are authenticated. 是否對Neo4j的請求進行了身份驗證。
#To disable authentication, uncomment this line 要禁用身份驗證,請取消註釋此行。
#dbms.security.auth_enabled=false
#Enable this to be able to upgrade a store from an older version. 是否兼容之前版本的數據
dbms.allow_format_migration=true
#Java Heap Size: by default the Java heap size is dynamically calculated based on available system resources. Java堆大小:默認狀況下,Java堆大小是動態地根據可用的系統資源計算。
#Uncomment these lines to set specific initial and maximum heap size. 取消註釋這些行以設置特定的初始值和最大值
#dbms.memory.heap.initial_size=512m
#dbms.memory.heap.max_size=512m
#The amount of memory to use for mapping the store files, in bytes (or kilobytes with the ‘k’ suffix, megabytes with ‘m’ and gigabytes with ‘g’). 用於映射存儲文件的內存量(以字節爲單位)千字節帶有’k’後綴,兆字節帶有’m’,千兆字節帶有’g’)。
#If Neo4j is running on a dedicated server, then it is generally recommended to leave about 2-4 gigabytes for the operating system, give the JVM enough heap to hold all your transaction state and query context, and then leave the rest for the page cache. 若是Neo4j在專用服務器上運行,那麼一般建議爲操做系統保留大約2-4千兆字節,爲JVM提供足夠的堆來保存全部的事務狀態和查詢上下文,而後保留其他的頁面緩存 。
#The default page cache memory assumes the machine is dedicated to running Neo4j, and is heuristically set to 50% of RAM minus the max Java heap size. 默認頁面緩存存儲器假定機器專用於運行Neo4j,而且試探性地設置爲RAM的50%減去最大Java堆大小。
#dbms.memory.pagecache.size=10g
#With default configuration Neo4j only accepts local connections. Neo4j默認只接受本地鏈接(localhost)
#To accept non-local connections, uncomment this line: 要接受非本地鏈接,請取消註釋此行
dbms.connectors.default_listen_address=0.0.0.0 (這是刪除#後的配置,能夠經過ip訪問)
#You can also choose a specific network interface, and configure a non-default port for each connector, by setting their individual listen_address. 還能夠選擇特定的網絡接口,並配置非默認值端口,設置它們各自的listen_address
#The address at which this server can be reached by its clients. This may be the server’s IP address or DNS name, or it may be the address of a reverse proxy which sits in front of the server. This setting may be overridden for individual connectors below. 客戶端能夠訪問此服務器的地址。這能夠是服務器的IP地址或DNS名稱,或者能夠是位於服務器前面的反向代理的地址。此設置可能會覆蓋如下各個鏈接器。
#dbms.connectors.default_advertised_address=localhost
#You can also choose a specific advertised hostname or IP address, and configure an advertised port for each connector, by setting their individual advertised_address. 您還能夠選擇特定廣播主機名或IP地址,
爲每一個鏈接器配置通告的端口,經過設置它們獨特的advertised_address。
#Bolt connector 使用Bolt協議
dbms.connector.bolt.enabled=true
dbms.connector.bolt.tls_level=OPTIONAL
dbms.connector.bolt.listen_address=:7687
#HTTP Connector. There must be exactly one HTTP connector. 使用http協議
dbms.connector.http.enabled=true
dbms.connector.http.listen_address=:7474
#HTTPS Connector. There can be zero or one HTTPS connectors. 使用https協議
dbms.connector.https.enabled=true
dbms.connector.https.listen_address=:7473
#Number of Neo4j worker threads. Neo4j線程數
#dbms.threads.worker_count=
#Logging configuration 日誌配置
#To enable HTTP logging, uncomment this line 要啓用HTTP日誌記錄,請取消註釋此行
dbms.logs.http.enabled=true
#Number of HTTP logs to keep. 要保留的HTTP日誌數
#dbms.logs.http.rotation.keep_number=5
#Size of each HTTP log that is kept. 每一個HTTP日誌文件的大小
dbms.logs.http.rotation.size=20m
#To enable GC Logging, uncomment this line 要啓用GC日誌記錄,請取消註釋此行
#dbms.logs.gc.enabled=true
#GC Logging Options see http://docs.oracle.com/cd/E19957-01/819-0084-10/pt_tuningjava.html#wp57013 for more information. GC日誌記錄選項 有關詳細信息,請參見http://docs.oracle.com/cd/E19957-01/819-0084-10/pt_tuningjava.html#wp57013
#dbms.logs.gc.options=-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintPromotionFailure -XX:+PrintTenuringDistribution
#Number of GC logs to keep. 要保留的GC日誌數
#dbms.logs.gc.rotation.keep_number=5
#Size of each GC log that is kept. 保留的每一個GC日誌文件的大小
#dbms.logs.gc.rotation.size=20m
#Size threshold for rotation of the debug log. If set to zero then no rotation will occur. Accepts a binary suffix 「k」, 「m」 or 「g」. 調試日誌旋轉的大小閾值。若是設置爲零,則不會發生滾動(達到指定大小後切割日誌文件)。接受二進制後綴「k」,「m」或「g」。
#dbms.logs.debug.rotation.size=20m
#Maximum number of history files for the internal log. 最多保存幾個日誌文件
#dbms.logs.debug.rotation.keep_number=7
#Enable this to specify a parser other than the default one. 啓用此選項可指定除默認解析器以外的解析器
#cypher.default_language_version=3.0
#Determines if Cypher will allow using file URLs when loading data using LOAD CSV
. Setting this value to false
will cause Neo4j to fail LOAD CSV
clauses that load data from the file system. 肯定當使用加載數據時,Cypher是否容許使用文件URL LOAD CSV
。將此值設置爲false
將致使Neo4j不能經過互聯網上的URL導入數據,LOAD CSV
會從文件系統加載數據。
dbms.security.allow_csv_import_from_file_urls=true
#Retention policy for transaction logs needed to perform recovery and backups. 執行恢復和備份所需的事務日誌的保留策略
#dbms.tx_log.rotation.retention_policy=7 days
#Enable a remote shell server which Neo4j Shell clients can log in to. 啓用Neo4j Shell客戶端能夠登陸的遠程shell服務器
dbms.shell.enabled=true
#The network interface IP the shell will listen on (use 0.0.0.0 for all interfaces).
dbms.shell.host=127.0.0.1
#The port the shell will listen on, default is 1337.
dbms.shell.port=1337
#Only allow read operations from this Neo4j instance. This mode still requires write access to the directory for lock purposes. 只容許從Neo4j實例讀取操做。此模式仍然須要對目錄的寫訪問以用於鎖定目的。
#dbms.read_only=false
#Comma separated list of JAX-RS packages containing JAX-RS resources, one package name for each mountpoint. The listed package names will be loaded under the mountpoints specified. Uncomment this line to mount the org.neo4j.examples.server.unmanaged.HelloWorldResource.java from neo4j-server-examples under /examples/unmanaged, resulting in a final URL of http://localhost:7474/examples/unmanaged/helloworld/{nodeId} 包含JAX-RS資源的JAX-RS軟件包的逗號分隔列表,每一個安裝點一個軟件包名稱。所列出的軟件包名稱將在指定的安裝點下加載。取消註釋此行以裝載org.neo4j.examples.server.unmanaged.HelloWorldResource.java neo4j-server-examples下/ examples / unmanaged,最終的URL爲http//localhost7474/examples/unmanaged/helloworld/{nodeId}
#dbms.unmanaged_extension_classes=org.neo4j.examples.server.unmanaged=/examples/unmanaged
#JVM Parameters JVM參數
#G1GC generally strikes a good balance between throughput and tail latency, without too much tuning. G1GC一般在吞吐量和尾部延遲之間達到很好的平衡,而沒有太多的調整。
dbms.jvm.additional=-XX:+UseG1GC
#Have common exceptions keep producing stack traces, so they can be debugged regardless of how often logs are rotated. 有共同的異常保持生成堆棧跟蹤,因此他們能夠被調試,不管日誌被旋轉的頻率
dbms.jvm.additional=-XX:-OmitStackTraceInFastThrow
#Make sure that initmemory
is not only allocated, but committed to the process, before starting the database. This reduces memory fragmentation, increasing the effectiveness of transparent huge pages. It also reduces the possibility of seeing performance drop due to heap-growing GC events, where a decrease in available page cache leads to an increase in mean IO response time. Try reducing the heap memory, if this flag degrades performance. 確保在啓動數據庫以前,「initmemory」不只被分配,並且被提交到進程。這減小了內存碎片,增長了透明大頁面的有效性。它還減小了因爲堆增加的GC事件而致使性能降低的可能性,其中可用頁面緩存的減小致使平均IO響應時間的增長。若是此標誌下降性能,請減小堆內存。
dbms.jvm.additional=-XX:+AlwaysPreTouch
#Trust that non-static final fields are really final. This allows more optimizations and improves overall performance. NOTE: Disable this if you use embedded mode, or have extensions or dependencies that may use reflection or serialization to change the value of final fields! 信任非靜態final字段真的是final。這容許更多的優化和提升總體性能。注意:若是使用嵌入模式,或者有可能使用反射或序列化更改最終字段的值的擴展或依賴關係,請禁用此選項!
dbms.jvm.additional=-XX:+UnlockExperimentalVMOptions
dbms.jvm.additional=-XX:+TrustFinalNonStaticFields
#Disable explicit garbage collection, which is occasionally invoked by the JDK itself. 禁用顯式垃圾回收,這是偶爾由JDK自己調用。
dbms.jvm.additional=-XX:+DisableExplicitGC
#Remote JMX monitoring, uncomment and adjust the following lines as needed. Absolute paths to jmx.access and jmx.password files are required. 遠程JMX監視,取消註釋並根據須要調整如下行。須要jmx.access和jmx.password文件的絕對路徑。
#Also make sure to update the jmx.access and jmx.password files with appropriate permission roles and passwords, the shipped configuration contains only a read only role called ‘monitor’ with password ‘Neo4j’. 還要確保使用適當的權限角色和密碼更新jmx.access和jmx.password文件,所配置的配置只包含名爲「monitor」的只讀角色,密碼爲「Neo4j」。
#For more details, see: http://download.oracle.com/javase/8/docs/technotes/guides/management/agent.html On Unix based systems the jmx.password file needs to be owned by the user that will run the server, and have permissions set to 0600. Unix系統,有關詳情,請參閱:http://download.oracle.com/javase/8/docs/technotes/guides/management/agent.html,jmx.password文件須要由運行服務器的用戶擁有,而且權限設置爲0600。
#For details on setting these file permissions on Windows see: http://docs.oracle.com/javase/8/docs/technotes/guides/management/security-windows.html Windows系統 有關在設置這些文件權限的詳細信息,請參閱:http://docs.oracle.com/javase/8/docs/technotes/guides/management/security-windows.html
#dbms.jvm.additional=-Dcom.sun.management.jmxremote.port=3637
#dbms.jvm.additional=-Dcom.sun.management.jmxremote.authenticate=true
#dbms.jvm.additional=-Dcom.sun.management.jmxremote.ssl=false
#dbms.jvm.additional=-Dcom.sun.management.jmxremote.password.file=/absolute/path/to/conf/jmx.password
#dbms.jvm.additional=-Dcom.sun.management.jmxremote.access.file=/absolute/path/to/conf/jmx.access
#Some systems cannot discover host name automatically, and need this line configured: 某些系統沒法自動發現主機名,須要配置如下行:
#dbms.jvm.additional=-Djava.rmi.server.hostname=$THE_NEO4J_SERVER_HOSTNAME
#Expand Diffie Hellman (DH) key size from default 1024 to 2048 for DH-RSA cipher suites used in server TLS handshakes. 對於服務器TLS握手中使用的DH-RSA密碼套件,將Diffie Hellman(DH)密鑰大小從默認1024展開到2048。
#This is to protect the server from any potential passive eavesdropping. 這是爲了保護服務器免受任何潛在的被動竊聽。
dbms.jvm.additional=-Djdk.tls.ephemeralDHKeySize=2048
#WARNING - Do not modify any of these properties when an application using this configuration file has been installed as a service. WARNING - 當使用此配置文件的應用程序已做爲服務安裝時,不要修改任何這些屬性。
#Please uninstall the service before modifying this section. The service can then be reinstalled. 請在修改此部分以前卸載服務。 而後能夠從新安裝該服務。
#Name of the service 服務的名稱
dbms.windows_service_name=neo4j