为每次服务器重启使用现有创建的 neo4j 图形

Use the existing created neo4j graph for every server restart

我用 neo4j(py2neo)、Flask 和 HTML 开发了一个网络应用程序。我正在通过读取 csv 文件来创建 Neo4j 图。此 CSV 文件较大。因此,每当我启动或重新启动服务器时,Neo4j 图形都会被创建,并且此设置需要花费大量时间。无论如何我可以使用创建的图形并将其存储在我的机器中。所以我的项目结构是 models.py,其中包含连接图形、创建具有关系的图形,以及 returns 数据到 routes.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__fetch_nodes 方法定义之外的所有内容。