赞
踩
启动neo4j
neo4j.bat console
浏览器访问http://localhost:7474/
安装anaconda
安装py2neo包
pip install py2neo
(连接)打开neo4j本地服务器,python连接。
python连接代码
为了简单演示:我是简单的创建了几个节点及关系。大家可自行更改。
这里演示的数据如下:
②查询节点函数
③节点之间创建关系函数
④加载进neo4j图库
代码:
- import pandas as pd
- from py2neo import Node, Relationship, Graph, NodeMatcher, RelationshipMatcher
-
-
- # 创建节点
- def CreateNode(m_graph, m_label, m_attrs):
- m_n = "_.name=" + "'" + m_attrs['name'] + "'" # _.name='老师' _.name='超市'
- print(111,m_n)
- matcher = NodeMatcher(m_graph)
- re_value = matcher.match(m_label).where(m_n).first()
- print(222,re_value) # (_0:Name {name: 'u8001u5e08'}) (_2:Name {name: 'u8d85u5e02'})
- if re_value is None:
- m_mode = Node(m_label, **m_attrs)
- n = graph.create(m_mode)
- return n
- return None
-
-
-
- Names = ["老师", "超市", "程序员", "小卖部", "阳仔公司"]
- action = ["传授", "销售", "敲", "售卖", "提供"]
- things = ["知识", "日常用品", "代码", "日常用品", "餐饮服务"]
- data = pd.DataFrame({"名称": Names, "字段": things, "方式": action})
- print(data)
-
- username = 'neo4j'
- password = 'xxxxx'
- graph = Graph('http://localhost:7474', username=username, password=password)
-
- label1 = "Name"
- label2 = "things"
- for i, j in data.iterrows():
- # 名称
- attr1 = {"name": j.名称}
- CreateNode(graph, label1, attr1)
- # 产品
- attr2 = {"name": j.字段}
- CreateNode(graph, label2, attr2)
-
代码:
- import pandas as pd
- from py2neo import Node, Relationship, Graph, NodeMatcher, RelationshipMatcher
-
-
- # 创建节点
- def CreateNode(m_graph, m_label, m_attrs):
- m_n = "_.name=" + "'" + m_attrs['name'] + "'" # _.name='老师' _.name='超市'
- print(111,m_n)
- matcher = NodeMatcher(m_graph)
- re_value = matcher.match(m_label).where(m_n).first()
- print(222,re_value) # (_0:Name {name: 'u8001u5e08'}) (_2:Name {name: 'u8d85u5e02'})
- if re_value is None:
- m_mode = Node(m_label, **m_attrs)
- n = graph.create(m_mode)
- return n
- return None
-
-
- # 查询节点
- def MatchNode(m_graph, m_label, m_attrs):
- m_n = "_.name=" + "'" + m_attrs['name'] + "'"
- matcher = NodeMatcher(m_graph)
- re_value = matcher.match(m_label).where(m_n).first()
- return re_value
-
-
- # 创建关系
- def CreateRelationship(m_graph, m_label1, m_attrs1, m_label2, m_attrs2, m_r_name):
- reValue1 = MatchNode(m_graph, m_label1, m_attrs1)
- reValue2 = MatchNode(m_graph, m_label2, m_attrs2)
- if reValue1 is None or reValue2 is None:
- return False
- m_r = Relationship(reValue1, m_r_name, reValue2)
- n = graph.create(m_r)
- return n
-
-
- Names = ["老师", "超市", "程序员", "小卖部", "阳仔公司"]
- action = ["传授", "销售", "敲", "售卖", "提供"]
- things = ["知识", "日常用品", "代码", "日常用品", "餐饮服务"]
- data = pd.DataFrame({"名称": Names, "字段": things, "方式": action})
- print(data)
-
- username = 'neo4j'
- password = 'xxxx'
- graph = Graph('http://localhost:7474', username=username, password=password)
-
- label1 = "Name"
- label2 = "things"
- for i, j in data.iterrows():
- # 名称
- attr1 = {"name": j.名称}
- CreateNode(graph, label1, attr1)
- # 产品
- attr2 = {"name": j.字段}
- CreateNode(graph, label2, attr2)
- #
- m_r_name = j.方式
- reValue = CreateRelationship(graph,label1,attr1,label2,attr2,m_r_name)
- print(reValue)
- # coding:utf-8
- from py2neo import Graph, Node, Relationship
-
- # 连接neo4j数据库,输入地址、用户名、密码
- graph = Graph('http://localhost:7474', username='neo4j', password='neo4j123')
- graph.delete_all()
-
- # 创建结点
- test_node_1 = Node('ru_yi_zhuan', name='皇帝') # 标签 ru_yi_zhuan,属性 皇帝
- test_node_2 = Node('ru_yi_zhuan', name='皇后') # 标签 ru_yi_zhuan,属性 皇后
- test_node_3 = Node('ru_yi_zhuan', name='公主') # 标签 ru_yi_zhuan,属性 公主
-
- graph.create(test_node_1)
- graph.create(test_node_2)
- graph.create(test_node_3)
-
- # 创建关系
- # 分别建立了test_node_1指向test_node_2和test_node_2指向test_node_1两条关系,
- # 关系的类型为"丈夫、妻子",两条关系都有属性count,且值为1。
- node_1_zhangfu_node_1 = Relationship(test_node_1, '丈夫', test_node_2)
- node_1_zhangfu_node_1['count'] = 1
- node_2_qizi_node_1 = Relationship(test_node_2, '妻子', test_node_1)
- node_2_munv_node_1 = Relationship(test_node_2, '母女', test_node_3)
- node_2_qizi_node_1['count'] = 1
-
- graph.create(node_1_zhangfu_node_1)
- graph.create(node_2_qizi_node_1)
- graph.create(node_2_munv_node_1)
-
- print(graph)
- print(test_node_1)
- print(test_node_2)
- print(node_1_zhangfu_node_1)
- print(node_2_qizi_node_1)
- print(node_2_munv_node_1)
参考:
Neo4j的Python程序操作_哔哩哔哩 (゜-゜)つロ 干杯~-bilibiliwww.bilibili.comCopyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。