当前位置:   article > 正文

neo4j 添加属性_Neo4j:动态添加属性/设置动态属性

neo4j 增加属性,以前的数据没有这个属性

neo4j 添加属性

我一直在研究一个具有英国国家铁路时刻表的数据集,它们以文本格式为您提供每列火车的出发和到达时间。

例如,可以这样创建代表停止的节点:

CREATE (stop:Stop {arrival: "0802", departure: "0803H"})

该时间格式不是特别适合查询,因此我想添加另一个属性,该属性指示自一天开始以来的秒数。

因此,我们想向节点添加“ arrivalSecondsSinceStartOfDay”和“ departureSecondsSinceStartOfDay”属性。 我编写了以下查询来计算这些属性的值。

  1. MATCH (stop:Stop)
  2. UNWIND ["arrival", "departure"] AS key
  3.  
  4. WITH key,
  5. toInteger(substring(stop[key], 0, 2)) AS hours,
  6. toInteger(substring(stop[key], 2, 2)) AS minutes,
  7. CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS seconds
  8.  
  9. WITH key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay
  10.  
  11. RETURN key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay
  1. ╒═══════════════════════════════╤══════════════════════╕
  2. │newKey │secondsSinceStartOfDay│
  3. ╞═══════════════════════════════╪══════════════════════╡
  4. │arrivalSecondsSinceStartOfDay │28920
  5. ├───────────────────────────────┼──────────────────────┤
  6. │departureSecondsSinceStartOfDay│29010
  7. └───────────────────────────────┴──────────────────────┘

现在我们准备在“停止”节点上设置这些属性。

  1. MATCH (stop:Stop2)
  2. UNWIND ["arrival", "departure"] AS key
  3.  
  4. WITH stop,
  5. key,
  6. toInteger(substring(stop[key], 0, 2)) AS hours,
  7. toInteger(substring(stop[key], 2, 2)) AS minutes,
  8. CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS seconds
  9.  
  10. WITH stop, key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay
  11. WITH stop, key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay
  12. SET stop[newKey] = secondsSinceStartOfDay
  1. Invalid input '[': expected an identifier character, whitespace, '{', node labels, a property map, a relationship pattern, '.', '(', '=' or "+=" (line 12, column 9 (offset: 447))
  2. "SET stop[newKey] = secondsSinceStartOfDay"
  3. ^

嗯,没有按预期工作! 看起来我们还不能使用Cypher设置动态属性。

幸运的是,我的同事Michael Hunger和Neo4j社区一直在管理APOC程序库,并且该程序正是可以帮助我们的程序。

您需要下载适用于您的Neo4j版本的jar ,然后将其放在plugins目录中。 我正在使用Neo4j 3.1 Beta1,因此对我来说是这样的:

  1. $ tree neo4j-enterprise-3.1.0-BETA1/plugins/
  2.  
  3. neo4j-enterprise-3.1.0-BETA1/plugins/
  4. └── apoc-3.1.0.1-all.jar
  5.  
  6. 0 directories, 1 file

完成之后,您将需要重新启动Neo4j,以便它可以采用我们添加的新过程。 完成后,执行以下查询以检查它们是否正确安装:

  1. call dbms.procedures()
  2. YIELD name
  3. WITH name
  4. WHERE name STARTS WITH "apoc"
  5. RETURN COUNT(*)
  1. ╒════════╕
  2. COUNT(*)│
  3. ╞════════╡
  4. 183
  5. └────────┘

现在,我们准备在图中动态设置属性。 我们将使用的过程是apoc.create.setProperty ,很容易更新查询以使用它:

  1. MATCH (stop:Stop)
  2. UNWIND ["arrival", "departure"] AS key
  3.  
  4. WITH stop,
  5. key,
  6. toInteger(substring(stop[key], 0, 2)) AS hours,
  7. toInteger(substring(stop[key], 2, 2)) AS minutes,
  8. CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS seconds
  9.  
  10. WITH stop, key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay
  11. WITH stop, key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay
  12. CALL apoc.create.setProperty(stop, newKey, secondsSinceStartOfDay)
  1. Query cannot conclude with CALL (must be RETURN or an update clause) (line 12, column 1 (offset: 439))
  2. "CALL apoc.create.setProperty(stop, newKey, secondsSinceStartOfDay)"
  3. ^

糟糕,我讲得太早了! 我们需要产生过程的return列并返回它,或者只是返回一个计数来解决此问题:

  1. MATCH (stop:Stop)
  2. UNWIND ["arrival", "departure"] AS key
  3.  
  4. WITH stop,
  5. key,
  6. toInteger(substring(stop[key], 0, 2)) AS hours,
  7. toInteger(substring(stop[key], 2, 2)) AS minutes,
  8. CASE WHEN substring(stop[key], 4,1) = "H" THEN 30 ELSE 0 END AS seconds
  9.  
  10. WITH stop, key, (hours * 60 * 60) + (minutes * 60) + seconds AS secondsSinceStartOfDay
  11. WITH stop, key + "SecondsSinceStartOfDay" AS newKey, secondsSinceStartOfDay
  12. CALL apoc.create.setProperty(stop, newKey, secondsSinceStartOfDay)
  13. YIELD node
  14. RETURN COUNT(*)
  1. ╒════════╕
  2. COUNT(*)│
  3. ╞════════╡
  4. 2
  5. └────────┘

就是这样,我们现在可以在查询中动态设置属性。

翻译自: https://www.javacodegeeks.com/2016/10/neo4j-dynamically-add-propertyset-dynamic-property.html

neo4j 添加属性

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1013849
推荐阅读
相关标签
  

闽ICP备14008679号