abhishek2046
abhishek2046

Reputation: 584

SQL query execution returns a str type instead of composite vertex and edge type in Apache AGE

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?

Upvotes: 0

Views: 155

Answers (5)

Zainab Saad
Zainab Saad

Reputation: 675

You can use psycopg2 to query postgres database using python scripts.

Connect to postgres running instance using:

import psycopg2, age
GRAPH_NAME = 'graph'
conn = psycopg2.connect(
host = 'localhost',
port = '5432',
dbname = 'agedb',
user = 'zainab',
password = '167984'
)
age.setUpAge(conn, GRAPH_NAME)

Then you can execute the following script to retrieve all the vertices' label, id, properties:

with conn.cursor() as cursor:
query = """SELECT * FROM ag_catalog.cypher('%s', $$ MATCH (v) RETURN v $$) as (v 
agtype);""" %s (GRAPH_NAME,)

try:

    cursor.execute(query)
    rows = cursor.fetchall()
except Exception as ex:
    print(type(ex), ex)
for row in rows:
    print(row[0].label, row[0].id, row[0].properties)

Upvotes: 0

Tito
Tito

Reputation: 268

It would be great if you could share the output and other parts of the code instead of a snippet.

However, to help solve this. Firstly, make sure the Models module is imported using import age.models. Secondly, the Python Driver has functions that simplifies the process for querying AGE using Python and I suggest you should use that, some of the methods include execCypher(), age.connect() , etc.

Upvotes: 0

Nnaemeka Daniel John
Nnaemeka Daniel John

Reputation: 474

You query may be returning a str type because you didn't load the AGE python drivers. You should visit the AGE python samples on github using this link to get an insight on what you might be doing wrong.

Upvotes: 0

Waleed Ahmed Shahid
Waleed Ahmed Shahid

Reputation: 151

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

Upvotes: 0

Wendel
Wendel

Reputation: 650

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.

Upvotes: 0

Related Questions