Skip to content

Miniprojects : Manaswi

Reading csv files and creating a graph database

from neo4j import GraphDatabase
import openpyxl

dataframe = openpyxl.load_workbook("Book.xlsx")

dataframe1 = dataframe.active

URI = "bolt://localhost"
AUTH = ("neo4j", "secretgraph")

with GraphDatabase.driver(URI, auth=AUTH) as driver:
    driver.execute_query("MATCH (n) DETACH DELETE n", database_="neo4j")
    driver.verify_connectivity()
    print("Connection established.")
    for row in range(1, dataframe1.max_row-1):
     for col in dataframe1.iter_cols(2, dataframe1.max_column):

       summary = driver.execute_query("""
       MERGE (a:Person {name: $name})
       MERGE (b:Person {name: $friendName})
       MERGE (a)-[:KNOWS]->(b)
       """,
       name=col[row].value, friendName=col[row+1].value,
       database_="neo4j",
    ).summary
    print("Created {nodes_created} nodes in {time} ms.".format(
    nodes_created=summary.counters.nodes_created,
    time=summary.result_available_after
    ))  
    records, summary, keys = driver.execute_query("""
    MATCH (p:Person)-[:KNOWS]->(:Person)
    RETURN p.name AS name
    """,
    database_="neo4j",
    )
    for record in records:
      print(record.data()) 


    records, summary, keys = driver.execute_query("""
     MATCH (p:Person)-[:KNOWS]->(f:Person)
     RETURN p.name AS person, f.name AS friend
     """, database_="neo4j")
    for record in records:
      print(record.data())