skip to Main Content

I am executing the following query in Python to return the list of all nodes stored in the Apache AGE.

with postgresql.cursor() as cursor:
    query = """SELECT * FROM cypher('graph', $$ MATCH (v) RETURN v $$) as (v agtype);""" 
    cursor.execute(query)
    for row in cursor:
        print(row[0])

But row[0] here is of the type str instead of <class 'age.models.Node'>. I need to access the id, label, and other properties of the node, which I could have easily done with <class 'age.models.Node'> by simply using row[0].id or row[0].label. I cannot do this with the str type.

I am currently trying to write unit test cases with pytest and factories from pytest_postgresql. It works properly when I run the same thing using psycopg2, and the expected data type is returned but fails with pytest_postgresql.

This is the code to set up the database.

postgresql_my_proc = factories.postgresql_proc()
postgresql_my = factories.postgresql('postgresql_my_proc')

Is it the issue with pytest_postgresql? Is it possible to fix this, and is there some workaround to achieve the same?

2

Answers


  1. You should use the Apache AGE Python Driver. This way, retrieving node IDs and labels would be very easy, as shown in the following sample:

    cursor = ag.execCypher("MATCH (n:Person) RETURN n")
    for row in cursor:
        vertex = row[0]
        print(vertex)
        print(vertex.id, vertex.label, vertex["name"])
    

    More samples can be seen here.

    Login or Signup to reply.
  2. I have just seen an example from the apache age GitHub documentation and they are using another way to retrieve these things.

    Single result column

    cursor = ag.execCypher("MATCH (n:Person {name: %s) RETURN n", params('Andy',))
    for row in cursor:
      vertex = row[0]
      print(vertex.id, vertex["name"], vertex) # row has id, label, properties
    

    Multi result columns

    cursor = ag.execCypher("MATCH (n:Person) RETURN label(n), n.name", cols=['label VARCHAR', 'name'])
    for row in cursor:
        label = row[0]
        name = row[1]
        print(label, name)  
    

    you can get more information from this file.

    Github File

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search