neo4j 添加属性_Neo4j:动态添加属性/设置动态属性
neo4j 添加屬性
我一直在研究一個具有英國國家鐵路時刻表的數據集,它們以文本格式為您提供每列火車的出發和到達時間。
例如,可以這樣創建代表停止的節點:
CREATE (stop:Stop {arrival: "0802", departure: "0803H"})該時間格式不是特別適合查詢,因此我想添加另一個屬性,該屬性指示自一天開始以來的秒數。
因此,我們想向節點添加“ arrivalSecondsSinceStartOfDay”和“ departureSecondsSinceStartOfDay”屬性。 我編寫了以下查詢來計算這些屬性的值。
MATCH (stop:Stop) UNWIND ["arrival", "departure"] AS keyWITH key,toInteger(substring(stop[key], 0, 2)) AS hours, toInteger(substring(stop[key], 2, 2)) AS minutes,CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS secondsWITH key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDayRETURN key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay╒═══════════════════════════════╤══════════════════════╕ │newKey │secondsSinceStartOfDay│ ╞═══════════════════════════════╪══════════════════════╡ │arrivalSecondsSinceStartOfDay │28920 │ ├───────────────────────────────┼──────────────────────┤ │departureSecondsSinceStartOfDay│29010 │ └───────────────────────────────┴──────────────────────┘現在我們準備在“停止”節點上設置這些屬性。
MATCH (stop:Stop2) UNWIND ["arrival", "departure"] AS keyWITH stop,key,toInteger(substring(stop[key], 0, 2)) AS hours, toInteger(substring(stop[key], 2, 2)) AS minutes,CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS secondsWITH stop, key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay WITH stop, key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay SET stop[newKey] = secondsSinceStartOfDayInvalid input '[': expected an identifier character, whitespace, '{', node labels, a property map, a relationship pattern, '.', '(', '=' or "+=" (line 12, column 9 (offset: 447)) "SET stop[newKey] = secondsSinceStartOfDay"^嗯,沒有按預期工作! 看起來我們還不能使用Cypher設置動態屬性。
幸運的是,我的同事Michael Hunger和Neo4j社區一直在管理APOC程序庫,并且該程序正是可以幫助我們的程序。
您需要下載適用于您的Neo4j版本的jar ,然后將其放在plugins目錄中。 我正在使用Neo4j 3.1 Beta1,因此對我來說是這樣的:
$ tree neo4j-enterprise-3.1.0-BETA1/plugins/neo4j-enterprise-3.1.0-BETA1/plugins/ └── apoc-3.1.0.1-all.jar0 directories, 1 file完成之后,您將需要重新啟動Neo4j,以便它可以采用我們添加的新過程。 完成后,執行以下查詢以檢查它們是否正確安裝:
call dbms.procedures() YIELD name WITH name WHERE name STARTS WITH "apoc" RETURN COUNT(*)╒════════╕ │COUNT(*)│ ╞════════╡ │183 │ └────────┘現在,我們準備在圖中動態設置屬性。 我們將使用的過程是apoc.create.setProperty ,很容易更新查詢以使用它:
MATCH (stop:Stop) UNWIND ["arrival", "departure"] AS keyWITH stop,key,toInteger(substring(stop[key], 0, 2)) AS hours, toInteger(substring(stop[key], 2, 2)) AS minutes,CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS secondsWITH stop, key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay WITH stop, key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay CALL apoc.create.setProperty(stop, newKey, secondsSinceStartOfDay)Query cannot conclude with CALL (must be RETURN or an update clause) (line 12, column 1 (offset: 439)) "CALL apoc.create.setProperty(stop, newKey, secondsSinceStartOfDay)"^糟糕,我講得太早了! 我們需要產生過程的return列并返回它,或者只是返回一個計數來解決此問題:
MATCH (stop:Stop) UNWIND ["arrival", "departure"] AS keyWITH stop,key,toInteger(substring(stop[key], 0, 2)) AS hours, toInteger(substring(stop[key], 2, 2)) AS minutes,CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS secondsWITH stop, key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay WITH stop, key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay CALL apoc.create.setProperty(stop, newKey, secondsSinceStartOfDay) YIELD node RETURN COUNT(*)╒════════╕ │COUNT(*)│ ╞════════╡ │2 │ └────────┘就是這樣,我們現在可以在查詢中動態設置屬性。
翻譯自: https://www.javacodegeeks.com/2016/10/neo4j-dynamically-add-propertyset-dynamic-property.html
neo4j 添加屬性
總結
以上是生活随笔為你收集整理的neo4j 添加属性_Neo4j:动态添加属性/设置动态属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华日冰箱怎么样_华日冰箱特点介绍
- 下一篇: jax-rs jax-ws_信守承诺:针