Fetching results from cypher bolt statement

Question:

I am trying to access neo4j using neo4j python driver.I am running the following code to get a property of a thing A., I open the driver and session directly from GraphDatabase of neo4j and use session.run() to execute graph queries. These queries return a BoltStatementResult object.My question is how this object can be converted to actual result that I need(Property of thing A).?

    from neo4j import GraphDatabase

uri = "bolt://abc:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))

def matchQuestion(tx, intent,thing):

    result = tx.run("MATCH (e:thing) WHERE e.name = {thing}"
           "RETURN e.description", thing=thing)

    print(result)

with driver.session() as session:
    session.read_transaction(matchQuestion, "define","A")
Asked By: reshmak

||

Answers:

result = tx.run("MATCH (e:thing) WHERE e.name = {thing}"
           "RETURN e.description AS description", thing=thing)

for line in result:
    print line["description"]

or

print result.single()

You could also specify the item position like –

print result.single()[0]
Answered By: krishna reddy

3 steps:
1.Connect with neo4j using a singleton class.(Best practice. you can use normal also.
2.Fetch function body
3.Fetch function execution

from neo4j import GraphDatabase

class Neo4jDriverSingleton:
"""
Singleton class for interacting with Neo4j database

"""
    _instance = None

    def __new__(cls, uri, user, password):
        if cls._instance is None:
            cls._instance = super(Neo4jDriverSingleton, cls).__new__(cls)
            cls._instance._driver = GraphDatabase.driver(uri, auth=(user, password))
        return cls._instance

    def close(self):
        self._driver.close()

    def session(self):
        """
        Return a new session object for interacting with Neo4j.

        :return: Neo4j session
        """
        return self._driver.session()



def fetch_data(session):
  """
  Generator function to fetch data from Neo4j database

   :param session: Neo4j session
    Yields: records from Neo4j database
  """
     # Cypher query to fetch data from Neo4j
    fetch_query = "MATCH (n) RETURN n"

    try:
        # Execute the fetch query
        result = session.run(fetch_query)

        # Iterate through the result and yield records one by one
        for record in result:
            yield record["n"]  # Assuming 'n' is the property you want to access

    except Exception as e:
        print(f"An error occurred while fetching data: {str(e)}")


# In your main code block:
if __name__ == "__main__":
    try:
    # Create a Neo4j driver instance using the Singleton
    neo4j_driver = Neo4jDriverSingleton(neo4j_uri, neo4j_user, neo4j_password)

    # Fetch data from Neo4j database using the generator
    for record in fetch_data(session):
        print(record)

    except Exception as e:
    print(f"An error occurred: {str(e)}")

    finally:
    # The generator will automatically close the result when exhausted
    neo4j_driver.close()
Answered By: swapnil wagh