11.4 Quick Start: Using the Python Client as a Module
This section describes how to use the Python client as a module in Python applications.
Remote Server
For this mode, all you need is the Python client to be
installed. In your Python program, you must authenticate with the
remote server before you can create a session as illustrated in the
following example. Note that you must replace the values for
base_url
, jdbc_url
,
username
, and password
with values to match your environment details.
import pypgx
import opg4py
import opg4py.graph_server as graph_server
pgql_conn = opg4py.pgql.get_connection("<username>","<password>", "<jdbc_url>")
pgql_statement = pgql_conn.create_statement()
pgql = """
CREATE PROPERTY GRAPH bank_graph
VERTEX TABLES (
bank_accounts
LABEL ACCOUNTS
PROPERTIES (ID, NAME)
)
EDGE TABLES (
bank_txns
SOURCE KEY (from_acct_id) REFERENCES bank_accounts (ID)
DESTINATION KEY (to_acct_id) REFERENCES bank_accounts (ID)
LABEL TRANSFERS
PROPERTIES (FROM_ACCT_ID, TO_ACCT_ID, AMOUNT, DESCRIPTION)
) OPTIONS(PG_PGQL)
"""
pgql_statement.execute(pgql)
instance = graph_server.get_instance("<base_url>", "<username>", "<password>")
session = instance.create_session("my_session")
graph = session.read_graph_by_name('BANK_GRAPH', 'pg_view')
analyst = session.create_analyst()
analyst.pagerank(graph)
rs = graph.query_pgql("SELECT id(x), x.pagerank FROM MATCH (x) LIMIT 5")
rs.print()
To execute, save the above program into a file named
program.py
and run the following
command:
python3 program.py
You will see the following output:
+-------------------------------------------+
| id(x) | pagerank |
+-------------------------------------------+
| BANK_ACCOUNTS(2) | 9.749447313256548E-4 |
| BANK_ACCOUNTS(4) | 0.004584001759076056 |
| BANK_ACCOUNTS(6) | 5.358461393401424E-4 |
| BANK_ACCOUNTS(8) | 0.0013051552434930175 |
| BANK_ACCOUNTS(10) | 0.0015040122009364232 |
+-------------------------------------------+
Converting PGQL result set into pandas dataframe
pandas.DataFrame
object using the
to_pandas()
method. This makes it easier to
perform various data filtering operations on the result set and it
can also be used in Lambda functions. For
example,example_query = (
"SELECT n.name AS name, n.age AS age "
"WHERE (n)"
)
result_set = sample_graph.query_pgql(example_query)
result_df = result_set.to_pandas()
result_df['age_bin'] = result_df['age'].apply(lambda x: int(x)/20) # create age bins based on age ranges
Note:
To view the complete set of available Python APIs, see OPG4PY Python API Reference.Embedded Server
For this mode, the Python client and the Graph Server RPM package must be installed on the same machine.
import os
os.environ["PGX_CLASSPATH"] = "/opt/oracle/graph/lib/*"
instance = graph_server.get_embedded_instance()
session = instance.create_session("python_pgx_client")
print(session)
To execute, save the above program into a file named
program.py
and run the following
command.
python3 program.py
After successful login, you must see a similar message indicating a PGX session was created:
PgxSession(id: 32fc7037-18f1-4381-ba94-107e5f63aec2, name: python_pgx_client)
Note:
To view the complete set of available Python APIs, see OPG4PY Python API Reference.Parent topic: Quick Starts for Using PGQL Property Graphs