Access Graph Studio Features Using Autonomous Database Graph Client
Using the AdbGraphClient
API, you can access Graph Studio
features in Autonomous Database programmatically using the Oracle Graph Client or through
your Java or Python application.
This API provides the following capabilities:
- Authenticate with Autonomous Database
- Manage the Graph Studio environment
- Execute graph queries and algorithms against the graph server (PGX)
- Execute graph queries directly against Oracle Database
To use the AdbGraphClient
API, you must have access to
Oracle Graph Client installation. The API is provided by the Oracle Graph Client
library which is a part of the Oracle Graph Server and Client distribution. See
Installing Oracle Graph Client on how to
install and get started with the graph client shell CLIs for Java or Python.
AdbGraphClient
API to establish a connection to Graph Studio, start an environment with allocated
memory, load a PGQL property graph into memory,
execute PGQL queries and run algorithms against the graph.
- Start the interactive graph shell CLI and connect to your Autonomous Database
instance with the
AdbGraphClient
using one of the following methods:Configuring theAdbGraphClient
using Tenancy Detailscd /opt/oracle/graph ./bin/opg4j --no_connect For an introduction type: /help intro Oracle Graph Server Shell 24.4.0 opg4j> import oracle.pg.rdbms.* opg4j> var config = AdbGraphClientConfiguration.builder() opg4j> config.database("<DB_name>") opg4j> config.tenancyOcid("<tenancy_OCID>") opg4j> config.databaseOcid("<database_OCID>") opg4j> config.username("ADBDEV") opg4j> config.password("<password_for_ADBDEV>") opg4j> config.endpoint("https://<hostname-prefix>.adb.<region>.oraclecloudapps.com/") opg4j> var client = new AdbGraphClient(config.build()) client ==> oracle.pg.rdbms.AdbGraphClient@7b8d1537
import oracle.pg.rdbms.*; var config = AdbGraphClientConfiguration.builder(); config.tenancyOcid("<tenancy_OCID>"); config.databaseOcid("<database_OCID>"); config.database("<DB_name>"); config.username("ADBDEV"); config.password("<password_for_ADBDEV>"); config.endpoint("https://<hostname-prefix>.adb.<region>.oraclecloudapps.com/"); var client = new AdbGraphClient(config.build());
cd /opt/oracle/graph ./bin/opg4py --no_connect Oracle Graph Server Shell 24.4.0 >>> from opg4py.adb import AdbClient >>> config = { ... 'tenancy_ocid': '<tenancy_OCID>', ... 'database': '<DB_name>', ... 'database_ocid': '<DB_OCID>', ... 'username': 'ADBDEV', ... 'password': '<password_for_ADBDEV>', ... 'endpoint': 'https://<hostname-prefix>.adb.<region>.oraclecloudapps.com/' ... } >>> client = AdbClient(config)
Configuring theAdbGraphClient
using JDBC ConnectionYou can also configure the
AdbGraphClient
to use a JDBC connection to connect to your Autonomous Database instance (as shown in the following code). See Connect with JDBC Thin Driver in Using Oracle Autonomous Database Serverless on how to obtain the JDBC URL to connect to the Autonomous Database.However, ensure that you have
READ
access to thev$pdbs
view in your Autonomous Database instance. By default, the ADMIN user hasREAD
access to thev$pdbs
view. For all other users (non-administrator users), theREAD
access can be granted by the ADMIN (GRANT SELECT ON v$pdbs TO <user>
).import oracle.pg.rdbms.* opg4j> var conn = DriverManager.getConnection(<jdbcUrl>, <username>, <password>) opg4j> var config = AdbGraphClientConfiguration.fromConnection(conn, <password>) opg4j> var client = new AdbGraphClient(config)
import oracle.pg.rdbms.*; AdbGraphClientConfiguration config = AdbGraphClientConfiguration.fromCredentials(<jdbcUrl>, <username>, <password>); AdbGraphClient client = new AdbGraphClient(config);
>>> from opg4py.adb import AdbClient >>> client = AdbClient.from_connection(<jdbcUrl>, <username>, <password>)
- Start the PGX server environment with the desired memory as shown in the
following code.This submits a job in Graph Studio for environment creation.
job.get()
waits for the environment to get started. You can always verify if the environment has started successfully withclient.isAttached()
. The method returns a booleantrue
if the environment is running.However, you can skip the step of creating an environment, if
client.isAttached()
returnstrue
in the first step of the code.opg4j> client.isAttached() $9 ==> false opg4j> var job=client.startEnvironment(10) job ==> oracle.pg.rdbms.Job@117e9a56[Not completed] opg4j> job.get() $11 ==> null opg4j> job.getName() $11 ==> "Environment Creation - 16 GBs" opg4j> job.getType() $12 ==> ENVIRONMENT_CREATION opg4j> job.getCreatedBy() $13 ==> "ADBDEV" opg4j> client.isAttached() $11 ==> true
if (!client.isAttached()) { var job = client.startEnvironment(10); job.get(); System.out.println("job details: name=" + job.getName() + "type= " + job.getType() +"created_by= " + job.getCreatedBy()); } job details: name=Environment Creation - 16 GBstype= ENVIRONMENT_CREATIONcreated_by= ADBDEV
>>> client.is_attached() False >>> job = client.start_environment(10) >>> job.get() >>> job.get_name() 'Environment Creation - 16 GBs' >>> job.get_created_by() 'ADBDEV' >>> client.is_attached() True
- Create an instance and a session object as shown:
opg4j> var instance = client.getPgxInstance() instance ==> ServerInstance[embedded=false,baseUrl=https://<hostname-prefix>.adb.<region>.oraclecloudapps.com/graph/pgx] opg4j> var session = instance.createSession("AdbGraphSession") session ==> PgxSession[ID=c403be26-ad0c-45cf-87b7-1da2a48bda54,source=AdbGraphSession]
ServerInstance instance = client.getPgxInstance(); PgxSession session = instance.createSession("AdbGraphSession");
>>> instance = client.get_pgx_instance() >>> session = instance.create_session("adb-session")
- Load a PGQL property graph from your Autonomous Database instance into
memory.
opg4j> var graph = session.readGraphByName("BANK_GRAPH", GraphSource.PG_PGQL) graph ==> PgxGraph[name=BANK_GRAPH,N=1000,E=5001,created=1647800790654]
PgxGraph graph = session.readGraphByName("BANK_GRAPH", GraphSource.PG_PGQL);
>>> graph = session.read_graph_by_name("BANK_GRAPH", "pg_pgql")
- Create an Analyst and execute a Pagerank algorithm on the graph as shown:
- Execute a PGQL query on the graph and print the result set as shown:
opg4j> graph.queryPgql("SELECT a.acct_id AS source, a.pagerank, t.amount, b.acct_id AS destination FROM MATCH (a)-[t]->(b) ORDER BY a.pagerank DESC LIMIT 3").print()
PgqlResultSet rs = graph.queryPgql("SELECT a.acct_id AS source, a.pagerank, t.amount, b.acct_id AS destination FROM MATCH (a)-[t]->(b) ORDER BY a.pagerank DESC LIMIT 3"); rs.print();
>>> rs = graph.query_pgql("SELECT a.acct_id AS source, a.pagerank, t.amount, b.acct_id AS destination FROM MATCH (a)-[t]->(b) ORDER BY a.pagerank DESC LIMIT 3").print()
On execution, the query produces the following output:+------------------------------------------------------+ | source | pagerank | amount | destination | +------------------------------------------------------+ | 387 | 0.007302836252205922 | 1000.0 | 188 | | 387 | 0.007302836252205922 | 1000.0 | 374 | | 387 | 0.007302836252205922 | 1000.0 | 577 | +------------------------------------------------------+
- Optionally, you can execute a PGQL query directly against the graph in the
database as shown in the following code.In order to establish a JDBC connection to the database, you must download the wallet and save it in a secure location. See JDBC Thin Connections with a Wallet on how to determine the JDBC URL connection string.
opg4j> String jdbcUrl="jdbc:oracle:thin:@<tns_alias>?TNS_ADMIN=<path_to_wallet>" opg4j> var conn = DriverManager.getConnection(jdbcUrl,"ADBDEV","<password_for_ADBDEV>") conn ==> oracle.jdbc.driver.T4CConnection@36ee8c7b opg4j> var pgqlConn = PgqlConnection.getConnection(conn) pgqlConn ==> oracle.pg.rdbms.pgql.PgqlConnection@5f27d271 opg4j> var pgqlStmt = pgqlConn.createStatement() pgqlStmt ==> oracle.pg.rdbms.pgql.PgqlExecution@4349f52c opg4j> pgqlStmt.executeQuery("SELECT a.acct_id AS source, t.amount, b.acct_id AS destination FROM MATCH (a)-[t]->(b) ON BANK_GRAPH LIMIT 3").print()
import oracle.pg.rdbms.pgql.PgqlConnection; import oracle.pg.rdbms.pgql.PgqlStatement; import oracle.pg.rdbms.pgql.PgqlResultSet; import oracle.pgx.api.*; import oracle.pg.rdbms.GraphServer; import oracle.pg.rdbms.pgql.jdbc.PgqlJdbcRdbmsDriver; …. DriverManager.registerDriver(new PgqlJdbcRdbmsDriver()); String jdbcUrl="jdbc:oracle:thin:@<tns_alias>?TNS_ADMIN=<path_to_wallet>"; Connection conn = DriverManager.getConnection(jdbcUrl,"ADBDEV","<password_for_ADBDEV>"); PgqlConnection pgqlConn = PgqlConnection.getConnection(conn); PgqlStatement pgqlStmt = pgqlConn.createStatement(); PgqlResultSet rs = pgqlStmt.executeQuery("SELECT a.acct_id AS source, t.amount, b.acct_id AS destination FROM MATCH (a)-[t]->(b) ON BANK_GRAPH LIMIT 3"); rs.print();
>>> jdbcUrl = "jdbc:oracle:thin:@<tns_alias>?TNS_ADMIN=<path_to_wallet>" >>> pgql_conn = opg4py.pgql.get_connection("ADBDEV","<password_for_ADBDEV>", jdbcUrl) >>> pgql_statement = pgql_conn.create_statement() >>> pgql_statement.execute_query("SELECT a.acct_id AS source, t.amount, b.acct_id AS destination FROM MATCH (a)-[t]->(b) ON BANK_GRAPH LIMIT 3").print()
On execution, the query produces the following output:+-------------------------------+ | SOURCE | AMOUNT | DESTINATION | +-------------------------------+ | 1000 | 1000 | 921 | | 1000 | 1000 | 662 | | 1000 | 1000 | 506 | +-------------------------------+
- Close the session after executing all graph queries as shown: