Skip to content

Instantly share code, notes, and snippets.

@davidfauth
Created April 22, 2020 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfauth/e6b17869b5a0efa18cb39134b7a7c034 to your computer and use it in GitHub Desktop.
Save davidfauth/e6b17869b5a0efa18cb39134b7a7c034 to your computer and use it in GitHub Desktop.
### 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