关于py2neo中的merge,create,当反复执行时,会出现什么。。。
先导包和创建点和关系
先把包导了,把Log也打印出来:
from py2neo import Node, Relationship, Graph, NodeMatcher,Subgraph
import logging
logging.basicConfig(level=logging.DEBUG, format="%(message)s")
两个点,一条关系,一个subgraph
a=Node('test',name='a')
b=Node('test',name='b')
r=Relationship(a,'knows',b,since=1000)
sub=Subgraph(relationships=[r])graph = Graph("bolt://localhost:11003", name="*****", password="******")
测试反复create node
先试试create:
反复执行下面的代码:
tx=graph.begin()
tx.create(a)
graph.commit(tx)
第一遍的log,从Log中可以看到执行成功,从neo4j中查询也是能查到一个node
Attempting to acquire read-write connection to 'neo4j'
Using connection pool <py2neo.client.ConnectionPool object at 0x0000026605283DF0>
Trying to acquire connection from pool <py2neo.client.ConnectionPool object at 0x0000026605283DF0>
Connection <py2neo.client.bolt.Bolt4x3 object at 0x0000026605283B80> acquired by thread <_MainThread(MainThread, started 10172)>
[#CDAE] C: BEGIN {'db': 'neo4j'}
[#CDAE] C: (Sent 16 bytes)
[#CDAE] S: SUCCESS {}
**[#CDAE] C: RUN 'UNWIND $data AS r\nCREATE (_:test)\nSET _ += r\nRETURN id(_)' {'data': [{'name': 'a'}]} {}**
[#CDAE] C: PULL {'n': -1, 'qid': -1}
[#CDAE] C: (Sent 96 bytes)
[#CDAE] S: SUCCESS {'t_first': 1, 'fields': ['id(_)'], 'qid': 0}
[#CDAE] S: RECORD * 1
[#CDAE] S: SUCCESS {'stats': {'labels-added': 1, 'nodes-created': 1, 'properties-set': 1}, 'type': 'rw', 't_last': 0, 'db': 'neo4j'}
[#CDAE] C: COMMIT
[#CDAE] C: (Sent 6 bytes)
[#CDAE] S: SUCCESS {'bookmark': 'FB:kcwQo7NK3Nv+SpKE636V8mZyF8oAARe2kA=='}
Releasing connection <py2neo.client.bolt.Bolt4x3 object at 0x0000026605283B80> from thread <_MainThread(MainThread, started 10172)>
执行第2,3,4,5…遍:
发现python并没有向neo4j发送什么东西,自然就不会再创建node了。也就是说,反复执行并不会重复创建node,执行merge也有类似的情况。
Attempting to acquire read-write connection to 'neo4j'
Using connection pool <py2neo.client.ConnectionPool object at 0x0000026605283DF0>
Trying to acquire connection from pool <py2neo.client.ConnectionPool object at 0x0000026605283DF0>
Connection <py2neo.client.bolt.Bolt4x3 object at 0x0000026605283B80> acquired by thread <_MainThread(MainThread, started 10172)>
[#CDAE] C: BEGIN {'db': 'neo4j'}
[#CDAE] C: (Sent 16 bytes)
[#CDAE] S: SUCCESS {}
[#CDAE] C: COMMIT
[#CDAE] C: (Sent 6 bytes)
[#CDAE] S: SUCCESS {'bookmark': 'FB:kJA='}
Releasing connection <py2neo.client.bolt.Bolt4x3 object at 0x0000026605283B80> from thread <_MainThread(MainThread, started 10172)>
可以看到,neo4j中只有这一个点:
奇怪的问题
如果在neo4j里如果我们把点和关系全部清空,再在python执行create时,也不会重新创建点和关系。而当有这种需要反复删除和创建的场景却无法创建点和关系时,问题就变的很可恶了!!
问题分析
然而这是为什么呢?需要我们对源码进行分析。通过pycharm跟踪发现,在create的过程中,会把subgraph中的nodes和relationships进行is_bound的判断,也就是判断是否已绑定过这一entity,当nodes或relationship的graph为空时,即没有graph与entity绑定,则会创建这个entity,如果不为空,则认为entity已经和graph绑定了,也就是创建过了,所以也就不会重复创建了。
但是这个设计会带来被删除的实体无法重复创建的问题。
node_dict = {}for node in self.nodes:if not self.**_is_bound**(node, tx.graph):key = frozenset(node.labels)node_dict.setdefault(key, []).append(node)# Convert relationships into a dictionary of# {rel_type: [Rel, Rel, ...]}rel_dict = {}for relationship in self.relationships:if not self.**_is_bound**(relationship, tx.graph):key = type(relationship).__name__rel_dict.setdefault(key, []).append(relationship)
def _is_bound(cls, entity, graph):if entity.graph is None:return Falseelif entity.graph == graph:return Trueelse:raise ValueError("Entity %r is already bound to graph %r" % (entity, graph))
解决思路
如果在neo4j中已经删除node或relationship,但想利用py2neo重复创建点,可以把这个entity的graph赋值为None,就可以再次创建了。
a.graph=None
关于py2neo中的merge,create,当反复执行时,会出现什么。。。相关推荐
- Oracle中的MERGE语句
转自http://blog.chinaunix.net/space.php?uid=16981447&do=blog&cuid=430716 做了简单的格式整理,加入了一点点原创的东西 ...
- MSSQL Server 2008中的MERGE(不仅仅是合并)
本篇文章由 泉州SEO www.234yp.com 整理发布,mssql www.234yp.com/Article/168194.html 谢谢合作! mssql 就像标题呈现的一样,SQL Ser ...
- Oracle中,使 CREATE TABLE AS SELECT 支持ORDER BY
Oracle中,使 CREATE TABLE AS SELECT 支持ORDER BY 文章发表:kehui 发表日期:2002-01-21 阅读次数:1103 大家都知道,"CREA ...
- 使用opencv中的merge()函数为BGR图像添加alpha通道
先上代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # 图像处理开发需求.图像处理接私活挣零花钱,请加微信/QQ 2487872782 # 图像处理 ...
- mysql中的merge into,SQL Server 2008中利用merge into关键实现insert/update自动匹配(类似于MySQL中的For Update关键字)...
SQL Server 2008中利用merge into关键实现insert/update自动匹配(类似于MySQL中的For Update关键字) 语法请参考: 按照语法编写语句 DECLARE @ ...
- php 正则去除script,javascript正则实现php中的加入和去除反斜杠函数效果
php中要给指定的指定的字符加上或去除反斜杠,可使用现成的函数:addcslashes() & stripcslashes() 来实现,实例代码: $str="select * fr ...
- oracle中的merge into用法解析
oracle中的merge into用法解析 merge into的形式 MERGE INTO [target-table] A USING [source-table sql] B ON([cond ...
- mysql中新建数据库create table的COLLATE是什么?
在mysql中执行show create table <tablename>指令,可以看到一张表的建表语句,example如下: CREATE TABLE `table1` (`id` b ...
- Git中的merge命令实现和工作方式
想象一下有如下情形:代码库中存在两个分支,并且每个分支都进行了修改,最后你想要将其中的一个分支合并到其他的分支中.个人博客网址 http://swinghu.github.com/ 那么要问合并的处理 ...
最新文章
- Windows Phone 7编程学习点滴一——页面切换、返回键重载和工具栏
- Python Django jsonpickle序列化隐藏部分字段代码示例
- can3--socketcan之mcp251x.c
- Linux-Ubuntu部署Springboot项目应用到生产环境(jar方式)
- SQL Server 2017 AlwaysOn AG 自动初始化(十二)
- 长城汽车申请“哈弗单身狗”、“哈弗奶狗”、“哈弗溜狗”等商标
- nginx post请求超时_Nginx 的超时 timeout 配置详解
- 【Python】Django CSRF问题
- 细数国内无人机的江湖门派
- 推荐几个正经网站,让你的数据结构和算法学习事半功倍!
- 云桌面有哪些优势-为什么企业使用云桌面是趋势
- 2019数据安装勾选_如何安装勾选认证平台安全控件,以及如何勾选认证发票
- [源码]Meepo路由
- Microbiome杂志和主编简介
- ERP系统生产委外管理,内外无缝连接
- 计算机网络中的32种掩码和反掩码
- RTOS文件系统对比:LittleFS Vs. SPIFFS
- GitChat新作,如何较为优雅地实现新手引导功能!
- AppsFlyer 研究(十四)创建 Facebook App 获取FaceBook App ID
- Cisco路由器的配置及应用