之前对图数据库学的比较浅薄,不需要那么复杂,直接插入即可:

def triples2neo4j(graph, triples, one2many=False, many2one=False):"""三元组插入neo4j"""for triple in triples:# 取出头实体、尾实体、关系head, head_typ, tail, tail_typ, rel = triplehead_node = Node(head_typ, name=head)tail_node = Node(tail_typ, name=tail)graph.merge(head_node, head_typ, "name")graph.merge(tail_node, tail_typ, "name")                  graph.create(Relationship(head_node, rel, tail_node))

--------------------------------------------------------2023-3-20---------------------------------------------------------

今天写了一个简单增量插入三元组的程序

1、查找实体类型对应的实例,放入list中,用于实例查重:

# 查找实体类型对应实例,返回list
def get_all_entities_of_ent_typ(graph, ent_typ):matcher = NodeMatcher(graph)ent_list = list(matcher.match(ent_typ))ent_list = [ent['name'] for ent in ent_list]return ent_list

2、对三元组中头实体已存在、尾实体已存在、三元组存在、三元组不存在来对实体进行处理,如果存在当前实体类型对应的实例则合并,不存在则插入。

3、关系通过简单的设定是否允许存在1对多和多对1的关系来提前设定。

# 三元组插入neo4j
def triples2neo4j(graph, triples, one2many=False, many2one=False): # 允许一对多关系,允许多对一关系for triple in triples:# 取出头实体、尾实体、关系ent_1, ent_2, rel = triplehead, head_typ = ent_1head_node = Node(head_typ, name=head)tail, tail_typ = ent_2tail_node = Node(tail_typ, name=tail)# head类型listhead_list = get_all_entities_of_ent_typ(graph, head_typ)# tail类型listtail_list = get_all_entities_of_ent_typ(graph, tail_typ)# 头实体和尾实体都存在if head in head_list and tail in tail_list:graph.merge(head_node, head_typ, "name")graph.merge(tail_node, tail_typ, "name")if list(RelationshipMatcher(graph).match((head_node, tail_node), r_type = rel)):print(f'三元组 ({head} ,{tail} ,{rel}) 已存在于图谱中,插入失败!')else:graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')# 头实体已存在elif head in head_list and tail not in tail_list:graph.merge(head_node, head_typ, "name")if list(RelationshipMatcher(graph).match((head_node, None), r_type = rel)):if one2many == False:print(f'头实体 {head} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')continuegraph.create(tail_node)graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')# 尾实体已存在elif head not in head_list and tail in tail_list:graph.merge(tail_node, tail_typ, "name")if list(RelationshipMatcher(graph).match((None, tail_node), r_type = rel)):if many2one == False:print(f'尾实体 {tail} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')   continue             graph.create(head_node)graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')# 头实体、尾实体均不存在else:                    graph.create(head_node)graph.create(tail_node)graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')

所有代码如下:

# 查找实体类型对应实例,返回list
def get_all_entities_of_ent_typ(graph, ent_typ):matcher = NodeMatcher(graph)ent_list = list(matcher.match(ent_typ))ent_list = [ent['name'] for ent in ent_list]return ent_list# 三元组插入neo4j
def triples2neo4j(graph, triples, one2many=False, many2one=False): # 允许一对多关系,允许多对一关系for triple in triples:# 取出头实体、尾实体、关系ent_1, ent_2, rel = triplehead, head_typ = ent_1head_node = Node(head_typ, name=head)tail, tail_typ = ent_2tail_node = Node(tail_typ, name=tail)# head类型listhead_list = get_all_entities_of_ent_typ(graph, head_typ)# tail类型listtail_list = get_all_entities_of_ent_typ(graph, tail_typ)# 头实体和尾实体都存在if head in head_list and tail in tail_list:graph.merge(head_node, head_typ, "name")graph.merge(tail_node, tail_typ, "name")if list(RelationshipMatcher(graph).match((head_node, tail_node), r_type = rel)):print(f'三元组 ({head} ,{tail} ,{rel}) 已存在于图谱中,插入失败!')else:graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')# 头实体已存在elif head in head_list and tail not in tail_list:graph.merge(head_node, head_typ, "name")if list(RelationshipMatcher(graph).match((head_node, None), r_type = rel)):if one2many == False:print(f'头实体 {head} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')continuegraph.create(tail_node)graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')# 尾实体已存在elif head not in head_list and tail in tail_list:graph.merge(tail_node, tail_typ, "name")if list(RelationshipMatcher(graph).match((None, tail_node), r_type = rel)):if many2one == False:print(f'尾实体 {tail} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')   continue             graph.create(head_node)graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')# 头实体、尾实体均不存在else:                    graph.create(head_node)graph.create(tail_node)graph.create(Relationship(head_node, rel, tail_node))print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')triples = [(['李沐','Per'], ['CMU', 'Sch'], '毕业于'),(['李沐', 'Per'], ['沐神的小迷弟', 'Per'], '迷弟'),(['李沐','Per'], ['中国', 'Cou'], '出生于'),(['李沐','Per'], ['亚马逊', 'Com'], '就职于'),(['沐神的小迷弟', 'Per'], ['西安交通大学', 'Sch'], '就读于'),(['李沐','Per'], ['上海交通大学', 'Sch'], '毕业于'),(['李沐','Per'], ['百度', 'Com'], '就职于'),]
triples2neo4j(graph, triples, one2many=False, many2one=False)

运行结果:

简单的neo4j三元组增量插入-通过py2neo实现相关推荐

  1. 数据结构 排序【简单排序(冒泡、插入)、希尔排序、堆排序、排序方法的综合比较、2套 排序汇总代码】

    目   录 第9章 排序(上) 9.1 简单排序(冒泡.插入) 1.前提 2.简单排序(冒泡排序) 3.简单排序(插入排序) 4.时间复杂度下界 9.2 希尔排序 9.3 堆排序 排序方法综合比较 排 ...

  2. python在docx指定位置插表格_超简单Python将指定数据插入到docx模板指定位置渲染并保存...

    超简单Python将指定数据插入到docx模板渲染并生成 最近有一个需求,制作劳动合同表,要从excel表格中将每个人的数据导入到docx劳动合同中,重复量很大,因此可以使用python高效解决.为了 ...

  3. 简单模拟word中对插入直线的操作

    简单模拟word中对插入直线的操作.可以画直线,然后可以选择直线进行移动.拉伸.删除.并能显示直线的距离. 操作:点击按钮开始画线.鼠标左键点击一条直线进行移动,选择端点进行拉伸. 鼠标右键点击直线, ...

  4. Neo4j企业版报错:py2neo.errors.ProtocolError: Cannot decode response content as JSON

    py2neo.errors.ProtocolError: Cannot decode response content as JSON 环境为neo4j-enterprise-5.1.0.py2neo ...

  5. 排序算法(01)— 三种简单排序(冒泡、插入、选择)

    一.概述 排序是数据处理中十分常见且核心的操作,虽说实际项目开发中很小几率会需要我们手动实现,毕竟每种语言的类库中都有n多种关于排序算法的实现.但是了解这些精妙的思想对我们还是大有裨益的. 1.1 排 ...

  6. 一学就废的三种简单排序【冒泡、插入、选择】

    文章目录 其他排序算法 冒泡排序 算法实现 代码实例 插入排序 算法实现 代码实例 选择排序 算法实现 代码实例 其他排序算法 一学就废的归并排序 冒泡排序 排列顺序从前到后或者从后往前都可,本文选择 ...

  7. 简单选择排序_Python3三种简单排序(冒泡、插入、选择)的比较

    冒泡排序 相邻的两个元素对比,大的数后推,遍历整个列表一次后,将最大项以冒泡的方式排列到列表末尾. 简易版冒泡排序示例如下 def bubble(sl): """ 冒泡排 ...

  8. Neo4j图数据库,用py2neo中的OGM操作(类似ORM)

    # Object-Graph Mapping将图数据库中的节点映射为python对象,通过对象的方式对节点进行访问和操作.# 将图中的每种标签定义为一个python类,其继承自GraphObject, ...

  9. SQL Server 批量插入数据方案 SqlBulkCopy 的简单封装,让批量插入更方便

    在线工具 一.Sql Server插入方案介绍 关于 SqlServer 批量插入的方式,有三种比较常用的插入方式,Insert.BatchInsert.SqlBulkCopy,下面我们对比以下三种方 ...

最新文章

  1. 4566: [Haoi2016]找相同字符 SAM
  2. iOS开发小技巧--边接受数据边写入文件的两种方法
  3. 智能车竞赛“猪尾汇” 是不是二呀?
  4. 转载--redis密码管理
  5. 钢体pdc钻头计算机辅助设计和绘图,PDC钻头三维设计软件的研究与设计
  6. c#中已知一个外部窗口的句柄,怎么关闭
  7. c语言让电脑发出滴滴声代码,centos命令行控制电脑发出滴滴声——使用beep把警告变为music...
  8. NPOI导出Excel2007-xlsx
  9. matlab 绘制一个二维正弦曲线(repmat)
  10. 计算机初级基础知识教程,计算机基础知识教程 适合初学者的计算机入门知识...
  11. ubuntu14.04小米无线网卡驱动安装
  12. 新手如何自己做网站?
  13. 三年级计算机帮助我们学本领,三年级作文学本领40
  14. qsnctf queen wp
  15. 如何练成大力金刚指:IKBC - C104 白色黑轴机械键盘 体验测评
  16. 如何把PDF转成护眼模式/反色/黑底白字
  17. bls java_Java PairingFactory.getPairing方法代碼示例
  18. MarkdownNote
  19. 常见字母组合发音规律
  20. Cognos Analytics教程之为什么我喜欢 Cognos Analytics:IBM Cognos Analytics 的 15 个特性

热门文章

  1. java毕业设计高校学习社区mybatis+源码+调试部署+系统+数据库+lw
  2. 微信支付记录删除后怎么恢复?赶紧收藏这两个小技巧
  3. G1D5-Intriguing properties of neural networks
  4. java实现pdf转为word
  5. 5G消息RCS、chatboot
  6. VVC帧内预测(六)MIP
  7. Excel只保留2位小数,删掉其他小数位
  8. 阿卜杜拉·法兹里和两个哥哥的故事(二)
  9. total commander
  10. 2022春招第一波投递时间预测,早看早知道