Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
### neo4jfourloadexample.py
### 22 April 2020
### David Fauth
### @davefauth
import os
import sys
import csv
from io import StringIO
from neo4j import GraphDatabase
from neo4j import TRUST_ALL_CERTIFICATES
from neo4j import TRUST_SYSTEM_CA_SIGNED_CERTIFICATES
from neo4j import READ_ACCESS
uri = "neo4j://localhost:7687"
user = "neo4j"
password = "password"
# --- Unencrypted Neo4j Instance ---#
#
# driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))
#
# ---------------------------------#
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"), encrypted=True, trust=TRUST_ALL_CERTIFICATES)
def createDB():
print("creating db")
# self.driver = GraphDatabase.driver(uri, auth=(user, password))
with driver.session(database="system") as session:
session.run("DROP DATABASE example IF EXISTS").consume()
session.run("CREATE DATABASE example").consume()
def importData(inputfile):
i = 0
query=""
with open(inputfile) as csvDataFile:
with driver.session(database="testdb") as session:
csvReader = csv.reader(csvDataFile)
tx = session.begin_transaction()
for row in csvReader:
# print(row[0])
i = i + 1
query = "CREATE (n:" + row[0] + "{name:\"" + row[1] + "\",job:\"" + row[2] + "\",phone:\"" + row[3] + "\"})"
tx.run(query)
if (i % 100 == 0):
tx.commit()
tx = session.begin_transaction()
tx.commit()
def readData():
with driver.session(database="testdb", default_access_mode=READ_ACCESS) as session:
result = session.run("MATCH (a:Student) RETURN a.name as name")
return [record["name"] for record in result]
def readSingleValue():
with driver.session(database="testdb", default_access_mode=READ_ACCESS) as session:
message = session.run("MATCH (a:Student) RETURN a.name as name limit 1").single().get("name")
print(message)
if __name__ == "__main__":
inputfile = sys.argv[1]
createDB()
importData(inputfile)
readData()
readSingleValue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.