Skip to content

Instantly share code, notes, and snippets.

@cj2001
Last active July 13, 2021 14:58
Show Gist options
  • Save cj2001/d3d23e67e932cafb356b2d87ec1ad653 to your computer and use it in GitHub Desktop.
Save cj2001/d3d23e67e932cafb356b2d87ec1ad653 to your computer and use it in GitHub Desktop.
Add author and category nodes to the graph
def add_categories(categories):
# Adds category nodes to the Neo4j graph.
query = '''
UNWIND $rows AS row
MERGE (c:Category {category: row.category})
RETURN count(*) as total
'''
return conn.query(query, parameters = {'rows':categories.to_dict('records')})
def add_authors(rows, batch_size=10000):
# Adds author nodes to the Neo4j graph as a batch job.
query = '''
UNWIND $rows AS row
MERGE (:Author {name: row.author})
RETURN count(*) as total
'''
return insert_data(query, rows, batch_size)
def insert_data(query, rows, batch_size = 10000):
# Function to handle the updating the Neo4j database in batch mode.
total = 0
batch = 0
start = time.time()
result = None
while batch * batch_size < len(rows):
res = conn.query(query,
parameters = {'rows': rows[batch*batch_size:(batch+1)*batch_size].to_dict('records')})
total += res[0]['total']
batch += 1
result = {"total":total,
"batches":batch,
"time":time.time()-start}
print(result)
return result
@MarcoBernardi
Copy link

Line 33 parenthesis error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment