neo4j 添加属性
我一直在研究一个具有英国国家铁路时刻表的数据集,它们以文本格式为您提供每列火车的出发和到达时间。
例如,可以这样创建代表停止的节点:
CREATE (stop:Stop {arrival: "0802", departure: "0803H"})
该时间格式不是特别适合查询,因此我想添加另一个属性,该属性指示自一天开始以来的秒数。
因此,我们想向节点添加“ arrivalSecondsSinceStartOfDay”和“ departureSecondsSinceStartOfDay”属性。 我编写了以下查询来计算这些属性的值。
- MATCH (stop:Stop)
- UNWIND ["arrival", "departure"] AS key
-
- WITH 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 seconds
-
- WITH key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay
-
- RETURN key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay
- ╒═══════════════════════════════╤══════════════════════╕
- │newKey │secondsSinceStartOfDay│
- ╞═══════════════════════════════╪══════════════════════╡
- │arrivalSecondsSinceStartOfDay │28920 │
- ├───────────────────────────────┼──────────────────────┤
- │departureSecondsSinceStartOfDay│29010 │
- └───────────────────────────────┴──────────────────────┘
现在我们准备在“停止”节点上设置这些属性。
- MATCH (stop:Stop2)
- UNWIND ["arrival", "departure"] AS key
-
- WITH 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 seconds
-
- WITH stop, key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay
- WITH stop, key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay
- SET stop[newKey] = secondsSinceStartOfDay
- Invalid 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.jar
-
- 0 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 key
-
- WITH 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 seconds
-
- WITH 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 key
-
- WITH 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 seconds
-
- WITH 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 添加属性