创建节点
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()
# (_0:Name {name: '\u8001\u5e08'}) (_2:Name {name: '\u8d85\u5e02'})
print(222, re_value)
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)
graph = Graph("http://192.168.92.145:7474", auth=("neo4j", "123456"))
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()
# (_0:Name {name: '\u8001\u5e08'}) (_2:Name {name: '\u8d85\u5e02'})
print(222, re_value)
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 = '123456'
graph = Graph("http://192.168.92.145:7474", auth=(username, 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数据库,输入地址、用户名、密码
username = 'neo4j'
password = '123456'
graph = Graph("http://192.168.92.145:7474", auth=(username, password))
# 删除图数据
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)