当前位置:   article > 正文

neo4j 节点显示名称_[Neo4j] neo4j 的python操作

neo4j 节点显示名称

启动neo4j

neo4j.bat console

269cc1736141001d6072c42de2377cda.png

浏览器访问http://localhost:7474/

安装anaconda

安装py2neo包

pip install py2neo

代码分析:

(连接)打开neo4j本地服务器,python连接。

3b928a4ae1508df648d73383b2f7c600.png

python连接代码

56b6959a92c1edd4775bd46765d500be.png

(数据加载)需要创建的节点及其关系

为了简单演示:我是简单的创建了几个节点及关系。大家可自行更改。
这里演示的数据如下:

e4c33145091be46d9dd9a3f000de4a79.png

重点(neo4j与python代码演示)

①创建节点函数

20ba9cfce9c836fca760a95be72d1d57.png

②查询节点函数

da4c258884b3c9f4df70252a9575a3bf.png

③节点之间创建关系函数

be856e28134c761878c6b40f45ebbb78.png

④加载进neo4j图库

ce9137c2fa724dccf62de3400096c730.png

代码:

  1. import pandas as pd
  2. from py2neo import Node, Relationship, Graph, NodeMatcher, RelationshipMatcher
  3. # 创建节点
  4. def CreateNode(m_graph, m_label, m_attrs):
  5. m_n = "_.name=" + "'" + m_attrs['name'] + "'" # _.name='老师' _.name='超市'
  6. print(111,m_n)
  7. matcher = NodeMatcher(m_graph)
  8. re_value = matcher.match(m_label).where(m_n).first()
  9. print(222,re_value) # (_0:Name {name: 'u8001u5e08'}) (_2:Name {name: 'u8d85u5e02'})
  10. if re_value is None:
  11. m_mode = Node(m_label, **m_attrs)
  12. n = graph.create(m_mode)
  13. return n
  14. return None
  15. Names = ["老师", "超市", "程序员", "小卖部", "阳仔公司"]
  16. action = ["传授", "销售", "敲", "售卖", "提供"]
  17. things = ["知识", "日常用品", "代码", "日常用品", "餐饮服务"]
  18. data = pd.DataFrame({"名称": Names, "字段": things, "方式": action})
  19. print(data)
  20. username = 'neo4j'
  21. password = 'xxxxx'
  22. graph = Graph('http://localhost:7474', username=username, password=password)
  23. label1 = "Name"
  24. label2 = "things"
  25. for i, j in data.iterrows():
  26. # 名称
  27. attr1 = {"name": j.名称}
  28. CreateNode(graph, label1, attr1)
  29. # 产品
  30. attr2 = {"name": j.字段}
  31. CreateNode(graph, label2, attr2)

1c0a6bc17dc8eb543f3b53dd1cdc99a1.png

7e79620e4da7bd9f83cb9720bc12b8c9.png

7fb59284708d9084fb46b110195494f0.png

代码:

  1. import pandas as pd
  2. from py2neo import Node, Relationship, Graph, NodeMatcher, RelationshipMatcher
  3. # 创建节点
  4. def CreateNode(m_graph, m_label, m_attrs):
  5. m_n = "_.name=" + "'" + m_attrs['name'] + "'" # _.name='老师' _.name='超市'
  6. print(111,m_n)
  7. matcher = NodeMatcher(m_graph)
  8. re_value = matcher.match(m_label).where(m_n).first()
  9. print(222,re_value) # (_0:Name {name: 'u8001u5e08'}) (_2:Name {name: 'u8d85u5e02'})
  10. if re_value is None:
  11. m_mode = Node(m_label, **m_attrs)
  12. n = graph.create(m_mode)
  13. return n
  14. return None
  15. # 查询节点
  16. def MatchNode(m_graph, m_label, m_attrs):
  17. m_n = "_.name=" + "'" + m_attrs['name'] + "'"
  18. matcher = NodeMatcher(m_graph)
  19. re_value = matcher.match(m_label).where(m_n).first()
  20. return re_value
  21. # 创建关系
  22. def CreateRelationship(m_graph, m_label1, m_attrs1, m_label2, m_attrs2, m_r_name):
  23. reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
  24. reValue2 = MatchNode(m_graph, m_label2, m_attrs2)
  25. if reValue1 is None or reValue2 is None:
  26. return False
  27. m_r = Relationship(reValue1, m_r_name, reValue2)
  28. n = graph.create(m_r)
  29. return n
  30. Names = ["老师", "超市", "程序员", "小卖部", "阳仔公司"]
  31. action = ["传授", "销售", "敲", "售卖", "提供"]
  32. things = ["知识", "日常用品", "代码", "日常用品", "餐饮服务"]
  33. data = pd.DataFrame({"名称": Names, "字段": things, "方式": action})
  34. print(data)
  35. username = 'neo4j'
  36. password = 'xxxx'
  37. graph = Graph('http://localhost:7474', username=username, password=password)
  38. label1 = "Name"
  39. label2 = "things"
  40. for i, j in data.iterrows():
  41. # 名称
  42. attr1 = {"name": j.名称}
  43. CreateNode(graph, label1, attr1)
  44. # 产品
  45. attr2 = {"name": j.字段}
  46. CreateNode(graph, label2, attr2)
  47. #
  48. m_r_name = j.方式
  49. reValue = CreateRelationship(graph,label1,attr1,label2,attr2,m_r_name)
  50. print(reValue)

cd9b204908d26b2f1f58f7143b9ad106.png
  1. # coding:utf-8
  2. from py2neo import Graph, Node, Relationship
  3. # 连接neo4j数据库,输入地址、用户名、密码
  4. graph = Graph('http://localhost:7474', username='neo4j', password='neo4j123')
  5. graph.delete_all()
  6. # 创建结点
  7. test_node_1 = Node('ru_yi_zhuan', name='皇帝') # 标签 ru_yi_zhuan,属性 皇帝
  8. test_node_2 = Node('ru_yi_zhuan', name='皇后') # 标签 ru_yi_zhuan,属性 皇后
  9. test_node_3 = Node('ru_yi_zhuan', name='公主') # 标签 ru_yi_zhuan,属性 公主
  10. graph.create(test_node_1)
  11. graph.create(test_node_2)
  12. graph.create(test_node_3)
  13. # 创建关系
  14. # 分别建立了test_node_1指向test_node_2test_node_2指向test_node_1两条关系,
  15. # 关系的类型为"丈夫、妻子",两条关系都有属性count,且值为1
  16. node_1_zhangfu_node_1 = Relationship(test_node_1, '丈夫', test_node_2)
  17. node_1_zhangfu_node_1['count'] = 1
  18. node_2_qizi_node_1 = Relationship(test_node_2, '妻子', test_node_1)
  19. node_2_munv_node_1 = Relationship(test_node_2, '母女', test_node_3)
  20. node_2_qizi_node_1['count'] = 1
  21. graph.create(node_1_zhangfu_node_1)
  22. graph.create(node_2_qizi_node_1)
  23. graph.create(node_2_munv_node_1)
  24. print(graph)
  25. print(test_node_1)
  26. print(test_node_2)
  27. print(node_1_zhangfu_node_1)
  28. print(node_2_qizi_node_1)
  29. print(node_2_munv_node_1)

6a13d1bcaae73677005c2b00baafd604.png

13e9e53804b3bdbd47a6bfa8311f3794.png

7a7754c4fd5d13e4b95e145b320e9b11.png

4e2a61713e10c7a6468280456aa69984.png

参考:

Neo4j的Python程序操作_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili​www.bilibili.com
ba0ccd9a2d1f5a263d7aae081743457c.png
python创建neo4j图库节点及节点之间的关系_伍曾雨阳的博客-CSDN博客​blog.csdn.net
32e88a095d5d22dabdc26925130ef327.png
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/161299
推荐阅读
相关标签
  

闽ICP备14008679号