Executive Summary

  1. Create a project and database in Neo4j

  2. Start and open the database, which should give you information on your server status (URI)
    • For example —> bolt://localhost:7687

  3. Create an admin user (if you don’t have one) and save the user and password

  4. Install Neo4j Python Driver —> pip3 install neo4j

  5. Define a connection class to connect to the graph database
    class Neo4jConnection:
        
        def __init__(self, uri, user, pwd):
            # Collecting user authentication details to connect to the graph database
            # Requires uri, user, and password
            self.__uri = uri
            self.__user = user
            self.__pwd = pwd
            self.__driver = None
            try:
                self.__driver = GraphDatabase.driver(self.__uri, auth=(self.__user, self.__pwd))
            except Exception as e:
                print("Failed to create the driver:", e)
            
        def close(self):
            # Close the connection to the graph database
            if self.__driver is not None:
                self.__driver.close()
            
        def query(self, query, db=None):
            # Takes in user's Cypher query and query the chosen database
            assert self.__driver is not None, "Driver not initialized!"
            session = None
            response = None
            try: 
                session = self.__driver.session(database=db) if db is not None else self.__driver.session() 
                response = list(session.run(query))
            except Exception as e:
                print("Query failed:", e)
            finally: 
                if session is not None:
                    session.close()
            return response
    
  6. Create a new database using Cypher

  7. Create entities using Cypher

  8. Create relationships using Cypher

  9. Make Cypher queries to the newly created database with entities and relations

  10. You can add Graph Data Science Algorithms by installing the plugins in Neo4j (extra)

Ryan

Ryan

Data Scientist

Leave a Reply