每次重新启动服务器时,都使用现有创建的neo4j图

问题描述

我用neo4j(py2neo),Flask和HTML开发了一个Web应用程序。我正在通过读取csv文件来创建Neo4j图。该CSV文件大小较大。因此,无论何时启动或重新启动,每次都会创建服务器Neo4j图,并且此设置花费大量时间。无论如何,我可以使用创建的图形并将其存储在计算机中。因此,我的项目结构是我拥有models.py,其中包含连接图形,创建具有关系的图形并将数据返回给route.py。

我的models.py看起来像这样


from py2neo import Graph,Node,Relationship,NodeMatcher
import pandas as pd


class Query:
    "Model the tags"
    print("I am coming here")
    graph = Graph("http://blah:blah@127.0.0.1/db/data")
    print("hi i am second")
    
    try:
        graph.run("Match () Return 1 Limit 1")
        df=pd.read_csv("data.csv")
        print("I am not reading till")
        graph.delete_all()

        matcher = NodeMatcher(graph)
        row_count = len(df)      

        for i in range(row_count):            
            tags = df['tags'][i]
            each_tag = tags.split('|')
            tag_data = [x.lower() for x in each_tag]
            for j,first_tag in enumerate(tag_data[:-1]):
                match_first_tag = matcher.match("May21tag",tagName=first_tag).first()               
                graph.push(match_first_tag)
                for second_tag in tag_data[j+1:]:
                    my_tag = second_tag
                    second_tag = matcher.match("May21tag",tagName=second_tag).first()
                    graph.push(second_tag)                    
                    create_relationship = Relationship(match_first_tag,"tagged",second_tag)
                    graph.merge(create_relationship)
    except Exception as e:
        print('not ok',e)

    def __init__(tech):
        tech_word = tech
        print("in init")

    def fetch_nodes(self,tech_word):
        graph = Graph("http://blah:blah@127.0.0.1/db/data")
        matcher = NodeMatcher(graph)
        match_nodes = matcher.match(tagName=tech_word).first()
        return (list(r.end_node["tagName"] for r in self.graph.match(nodes=(match_nodes,)).limit(6)))


所以这里发生的是每当我运行app.py时,我都能看到图形和关系每次都在创建。因此需要时间。我需要的是,每当我启动或重新启动服务器时,我都想使用具有关系的现有图形。

解决方法

尝试删除Query__init__方法定义中的所有内容{1>}。