A.10.5.3 Basic Query Execution
You can execute PGQL queries using the opg4py.pgql
Python
wrapper.
Executing PGQL Queries Using the Python Client
- Set the graph for querying as
shown:
>>> pgql_conn.set_graph("<graph_name>")
where <graph_name> is the name of the graph.
- Define and execute the PGQL
SELECT
query. For example,>>> pgql = "SELECT e.from_acct_id, e.to_acct_id, e.amount FROM MATCH (n:accounts) -[e:transfers]-> (m:accounts) on bank_graph limit 10"
- Execute and print the result set as
shown:
>>> pgql_result_set = pgql_statement.execute_query(pgql) >>> pgql_result_set.print() +------------------------------------+ | FROM_ACCT_ID | TO_ACCT_ID | AMOUNT | +------------------------------------+ | 781.0 | 712.0 | 1000.0 | | 190.0 | 555.0 | 1000.0 | | 191.0 | 329.0 | 1000.0 | | 198.0 | 57.0 | 1000.0 | | 220.0 | 441.0 | 1000.0 | | 251.0 | 387.0 | 1000.0 | | 254.0 | 188.0 | 1000.0 | | 259.0 | 305.0 | 1000.0 | | 261.0 | 145.0 | 1000.0 | | 263.0 | 40.0 | 1000.0 | +------------------------------------+ PgqlResultSet(java_pgql_result_set: oracle.pg.rdbms.pgql.PgqlResultSet, # of results: 0)
Also, you can convert the PGQL result set obtained in the preceding code
to a Pandas dataframe using the to_pandas()
method.
Note:
Thepandas
package must be installed in your system to
successfully execute the call to to_pandas()
. This package is
automatically installed at the time of the Python client installation for versions
Python 3.8 and Python 3.9. However, if your call to to_pandas()
fails, verify if the pandas
module is installed in your system. In
case the module is found missing or your Python version differs from the earlier
mentioned versions, then install the pandas
package
manually.
Parent topic: Using the Python Client to Execute PGQL Queries