python操作neo4j

一、说在前面

1、使用工具:py2neo  ,官方操作文档 :https://py2neo.org/v4/index.html

2、还包括一些neo4j的命令操作

二、案例说明

1、数据展示

python操作neo4j

 

 

 2、这个案例主要是读取Excel中的结构化数据购买方、销售方(节点)和金额(边),并实现在图中创建实体

python操作neo4j

 

 

 

三、相关代码

1、DataToNeo4jClass.py(连接neo4j,创建节点和关系的工具)

python操作neo4j
# -*- coding: utf-8 -*-
from py2neo import Node, Graph, Relationship,NodeMatcher


class DataToNeo4j(object):
    """将excel中数据存入neo4j"""

    def __init__(self):
        """建立连接"""
        link = Graph("http://localhost:7474", username="neo4j", password="wzs208751")
        self.graph = link
        #self.graph = NodeMatcher(link)
        # 定义label
        self.buy = 'buy'
        self.sell = 'sell'
        self.graph.delete_all()
        self.matcher = NodeMatcher(link)
        
        """
        node3 = Node('animal' , name = 'cat')
        node4 = Node('animal' , name = 'dog')  
        node2 = Node('Person' , name = 'Alice')
        node1 = Node('Person' , name = 'Bob')  
        r1 = Relationship(node2 , 'know' , node1)    
        r2 = Relationship(node1 , 'know' , node3) 
        r3 = Relationship(node2 , 'has' , node3) 
        r4 = Relationship(node4 , 'has' , node2)    
        self.graph.create(node1)
        self.graph.create(node2)
        self.graph.create(node3)
        self.graph.create(node4)
        self.graph.create(r1)
        self.graph.create(r2)
        self.graph.create(r3)
        self.graph.create(r4)
        """


    def create_node(self, node_buy_key,node_sell_key):
        """建立节点"""
        for name in node_buy_key:
            buy_node = Node(self.buy, name=name)
            self.graph.create(buy_node)
        for name in node_sell_key:
            sell_node = Node(self.sell, name=name)
            self.graph.create(sell_node)
            
        

    def create_relation(self, df_data):
        """建立联系"""      
        m = 0
        for m in range(0, len(df_data)):
            try:    
                print(list(self.matcher.match(self.buy).where("_.name=" + "'" + df_data['buy'][m] + "'")))
                print(list(self.matcher.match(self.sell).where("_.name=" + "'" + df_data['sell'][m] + "'")))
                rel = Relationship(self.matcher.match(self.buy).where("_.name=" + "'" + df_data['buy'][m] + "'").first(),
                                   df_data['money'][m], self.matcher.match(self.sell).where("_.name=" + "'" + df_data['sell'][m] + "'").first())

                self.graph.create(rel)
            except AttributeError as e:
                print(e, m)
View Code

2、invoice_neo4j.py

python操作neo4j
# -*- coding: utf-8 -*-
from utils.DataToNeo4jClass import DataToNeo4j
import os
import pandas as pd
#pip install py2neo==5.0b1 注意版本,要不对应不了

invoice_data = pd.read_excel('./Invoice_data_Demo.xls', header=0)
#print(invoice_data)

#可以先阅读下文档:https://py2neo.org/v4/index.html

def data_extraction():
    """节点数据抽取"""

    # 取出购买方名称到list
    node_buy_key = []
    for i in range(0, len(invoice_data)):
        node_buy_key.append(invoice_data['购买方名称'][i])
    
    node_sell_key = []
    for i in range(0, len(invoice_data)):
        node_sell_key.append(invoice_data['销售方名称'][i])
        
    # 去除重复的发票名称
    node_buy_key = list(set(node_buy_key))
    node_sell_key = list(set(node_sell_key))

    # value抽出作node
    node_list_value = []
    for i in range(0, len(invoice_data)):
        for n in range(1, len(invoice_data.columns)):
            # 取出表头名称invoice_data.columns[i]
            node_list_value.append(invoice_data[invoice_data.columns[n]][i])
    # 去重
    node_list_value = list(set(node_list_value))
    # 将list中浮点及整数类型全部转成string类型
    node_list_value = [str(i) for i in node_list_value]

    return node_buy_key, node_sell_key,node_list_value


def relation_extraction():
    """联系数据抽取"""

    links_dict = {}
    sell_list = []
    money_list = []
    buy_list = []

    for i in range(0, len(invoice_data)):
        money_list.append(invoice_data[invoice_data.columns[19]][i])#金额
        sell_list.append(invoice_data[invoice_data.columns[10]][i])#销售方方名称
        buy_list.append(invoice_data[invoice_data.columns[6]][i])#购买方名称


    # 将数据中int类型全部转成string
    sell_list = [str(i) for i in sell_list]
    buy_list = [str(i) for i in buy_list]
    money_list = [str(i) for i in money_list]

    # 整合数据,将三个list整合成一个dict
    links_dict['buy'] = buy_list
    links_dict['money'] = money_list
    links_dict['sell'] = sell_list
    # 将数据转成DataFrame
    df_data = pd.DataFrame(links_dict)
    print(df_data)
    return df_data

relation_extraction()
create_data = DataToNeo4j()

create_data.create_node(data_extraction()[0], data_extraction()[1])
create_data.create_relation(relation_extraction())
View Code

四、Neo4j增删改查命令

增:
增加一个节点
create (n:Person {name:'我',age:31})
带有关系属性
create (p:Person{name:"我",age:"31"})-[:包工程{金额:10000}]->(n:Person{name:"好大哥",age:"35"})
删
create (n:Person {name:'TYD',age:31})
match (n:Person{name:"TYD"}) delete n
删除关系
match (p:Person{name:"我",age:"31"})-[f:包工程]->(n:Person{name:"好大哥",age:"35"})
 delete f
改:
加上标签
match (t:Person) where id(t)=789 set t:好人return t
加上属性
match (a:好人) where id(a)=789 set a.战斗力=200 return a
修改属性
match (a:好人) where id(a)=789 set a.战斗力=500 return a
查:(查操作太多啦,直接参考neo4j例子就好)
match (p:Person) - [:包工程] -> (n:Person) return p,n

快速清空数据库:
MATCH (n)
DETACH DELETE n

 

上一篇:Neo4j和Titan的对比


下一篇:neo4j入门