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

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 添加属性

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Neo4j中批量添加属性,可以使用Python的py2neo库来实现。以下是一个示例代码: ```python # -*- coding: utf-8 -*- from py2neo import Graph, NodeMatcher # 连接Neo4j url = "http://localhost:7474" username = "neo4j" password = "******" graph = Graph(url, auth=(username, password)) print("neo4j info: {}".format(str(graph))) # 查询节点 node_matcher = NodeMatcher(graph) nodes = node_matcher.match('Test') # 批量添加属性 for node in nodes: node\["new_property"\] = "new_value" graph.push(nodes) ``` 在这个示例中,我们首先连接到Neo4j数据库。然后,我们使用NodeMatcher来查询所有的Test节点。接下来,我们使用一个循环来遍历每个节点,并为它们添加一个新的属性。最后,我们使用graph.push()方法将更改保存到数据库中。 请注意,这只是一个示例代码,你需要根据你的实际情况进行修改。确保替换url、username、password和节点的label和属性名称为你自己的值。 #### 引用[.reference_title] - *1* [neo4j批量追加属性、节点、关系](https://blog.csdn.net/sober0314/article/details/128136169)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Neo4j入门(四)批量更新节点属性](https://blog.csdn.net/jclian91/article/details/120357611)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值