I have developed a web application with neo4j(py2neo), Flask and HTML. I am creating the Neo4j graph by reading the csv file. This CSV file size is larger. So whenever I start or restart the server Neo4j graph is getting created everytime and it takes lot of time for this setup. Is there anyway I can use the created graph and store it in my machine. So My project struture is I have models.py which contains connecting the graph, creating the graph with relations, and returns the data to routes.py.
My models.py looks like this
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)))
So what is happening here is whenever I run the app.py I can see the graph and relationship are getting created everytime. So it takes time. WHat I need here is I wanted to use the existing graph with relations whenever I start or restart the server.
Try deleting everything in Query
except the __init__
and fetch_nodes
method definitions.