Created
December 10, 2018 18:25
-
-
Save oldarmyc/5ca7ca2974405af8044fe02855584f7d to your computer and use it in GitHub Desktop.
Neo4j example connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
To start working with Neo4j you would need to pip install the package. | |
pip install neo4j | |
Documentation is located here: | |
https://neo4j.com/docs/api/python-driver/current/ | |
""" | |
from neo4j import GraphDatabase | |
import json | |
# Get credentials from kubernetes. The credentials were setup as a dictionary | |
credentials = None | |
with open('/var/run/secrets/user_credentials/neo4j_credentials') as f: | |
credentials = json.load(f) | |
# Check and make sure the credentials were pulled correctly | |
if credentials: | |
# Setup the connection | |
uri = f"bolt://{credentials.get('host_name')}:7687" | |
driver = GraphDatabase.driver( | |
uri, | |
auth=( | |
credentials.get('username'), | |
credentials.get('password') | |
) | |
) | |
# Define function that does the query to the database | |
def print_info(tx): | |
for record in tx.run( | |
'MATCH (tom:Person {name: "Tom Hanks"})-' | |
'[:ACTED_IN]->(tomHanksMovies) RETURN tom,tomHanksMovies' | |
): | |
# Print out the records that it finds | |
print(record) | |
# Scope the session and call the query | |
with driver.session() as session: | |
session.read_transaction(print_info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment