Connecting to Neo4j

Anaconda Enterprise enables you to connect to the Neo4j graph database platform to work with data stored while in a notebook session.

To access Neo4j within the platform, you’ll need to pip install the required driver using the following command:

pip install neo4j

For more information, see the Neo4j Bolt Driver 1.7 for Python documentation.

NOTE: Any packages you install from the command line are available during the current session only. If you want them to persist, add them to the project’s anaconda-project.yml file. For more information, see Developing a project.

After you’ve installed the required driver, you can then use code such as this to access Neo4j from within a notebook session:

from neo4j import GraphDatabase

import json

"""
Get credentials from Kubernetes. The credentials were set up as a dictionary. For example:
{
    "username": "USERNAME",
    "password": "PASSWORD"
}
"""
credentials = None
with open('/var/run/secrets/user_credentials/neo4j_credentials') as f:
    credentials = json.load(f)

# Verify that 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 queries 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)

See Storing secrets for information about adding credentials to the platform, to make them available in your projects. Any secrets you add will be available across all sessions and deployments associated with your user account.