- プロパティ・グラフのグラフ開発者ガイド
- Oracleプロパティ・グラフ・スタート・ガイド
- プロパティ・グラフのサポートの概要
- Autonomous DatabaseでのOracle Graphの使用
- Autonomous Databaseグラフ・クライアントの使用
1.10.1 Autonomous Databaseグラフ・クライアントの使用
AdbGraphClient
APIを使用して、Oracle Graph Clientを使用するかJavaまたはPythonアプリケーションを介して、Autonomous DatabaseのGraph Studio機能にプログラムでアクセスできます。
このAPIには、次の機能があります。
- Autonomous Databaseで認証する
- Graph Studio環境を管理する
- グラフ・サーバー(PGX)に対してグラフ問合せおよびアルゴリズムを実行する
- Oracle Databaseに対してグラフ問合せを直接実行する
AdbGraphClient
APIを使用するには、Oracle Graph Clientインストールへのアクセス権が必要です。APIは、Oracle Graph Server and Clientディストリビューションの一部であるOracle Graph Clientライブラリによって提供されます。JavaまたはPython用のグラフ・クライアント・シェルCLIのインストール方法および開始方法の詳細は、Oracle Graph Clientのインストールを参照してください。
また、Autonomous Databaseグラフ・クライアントを使用する前に、「Autonomous Databaseグラフ・クライアントを使用するための前提条件」で説明されている前提条件をすべて満たしていることを確認してください。
次の例は、
AdbGraphClient
APIを使用してGraph Studioへの接続を確立し、割り当てられたメモリーで環境を起動し、PGビュー・グラフをメモリーにロードし、PGQL問合せを実行し、グラフに対してアルゴリズムを実行する方法を示しています。
- 次のように、対話型グラフ・シェルCLIを起動してAutonomous Databaseインスタンスに接続します:
cd /opt/oracle/graph ./bin/opg4j --no_connect For an introduction type: /help intro Oracle Graph Server Shell 22.2.0 opg4j> import oracle.pg.rdbms.* opg4j> var config = AdbGraphClientConfiguration.builder() config ==> AdbGraphClientConfiguration.AdbGraphClientConfigurationBuilder(endpoint=null, tenant=null, database=null, username=null, password=null, httpClient$value=null, refreshTimeBeforeTokenExpiry$value=0, refreshTimeBeforeTokenExpiryTimeUnit$value=null, executorService$value=null, jobPollInterval$value=0, jobPollIntervalTimeUnit$value=null, graphStudioApiVersion$value=null) opg4j> config.database("<DB_name>") $3 ==> AdbGraphClientConfiguration.AdbGraphClientConfigurationBuilder(endpoint=null, tenant=null, database=<DB_name>, username=null, password=null, httpClient$value=null, refreshTimeBeforeTokenExpiry$value=0, refreshTimeBeforeTokenExpiryTimeUnit$value=null, executorService$value=null, jobPollInterval$value=0, jobPollIntervalTimeUnit$value=null, graphStudioApiVersion$value=null) opg4j> config.tenant("<tenant_OCID>") $4 ==> AdbGraphClientConfiguration.AdbGraphClientConfigurationBuilder(endpoint=null, tenant=<tenant_OCID>, database=<DB_name>, username=null, password=null, httpClient$value=null, refreshTimeBeforeTokenExpiry$value=0, refreshTimeBeforeTokenExpiryTimeUnit$value=null, executorService$value=null, jobPollInterval$value=0, jobPollIntervalTimeUnit$value=null, graphStudioApiVersion$value=null) opg4j> config.username("ADBDEV") $5 ==> AdbGraphClientConfiguration.AdbGraphClientConfigurationBuilder(endpoint=null, tenant=<tenant_OCID>, database=<DB_name>, username=ADBDEV, password=null, httpClient$value=null, refreshTimeBeforeTokenExpiry$value=0, refreshTimeBeforeTokenExpiryTimeUnit$value=null, executorService$value=null, jobPollInterval$value=0, jobPollIntervalTimeUnit$value=null, graphStudioApiVersion$value=null) opg4j> config.password("<password_for_ADBDEV>") $6 ==> AdbGraphClientConfiguration.AdbGraphClientConfigurationBuilder(endpoint=null, tenant=<tenant_OCID>, database=<DB_name>, username=ADBDEV, password=<password_for_ADBDEV>, httpClient$value=null, refreshTimeBeforeTokenExpiry$value=0, refreshTimeBeforeTokenExpiryTimeUnit$value=null, executorService$value=null, jobPollInterval$value=0, jobPollIntervalTimeUnit$value=null, graphStudioApiVersion$value=null) opg4j> config.endpoint("https://<hostname-prefix>.adb.<region>.oraclecloudapps.com/") $7 ==> AdbGraphClientConfiguration.AdbGraphClientConfigurationBuilder(endpoint=https://<hostname-prefix>.adb.<region>.oraclecloudapps.com/, tenant=<tenant_OCID>, database=<DB_name>, username=ADBDEV, password=<password_for_ADBDEV>, httpClient$value=null, refreshTimeBeforeTokenExpiry$value=0, refreshTimeBeforeTokenExpiryTimeUnit$value=null, executorService$value=null, jobPollInterval$value=0, jobPollIntervalTimeUnit$value=null, graphStudioApiVersion$value=null) opg4j> var client = new AdbGraphClient(config.build()) client ==> oracle.pg.rdbms.AdbGraphClient@7b8d1537
import oracle.pg.rdbms.*; var config = AdbGraphClientConfiguration.builder(); config.tenant("<tenant_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 22.2.0 >>> from opg4py.adb import AdbClient >>> config = { ... 'tenant': '<tenant_OCID>', ... 'database': '<DB_name>', ... 'username': 'ADBDEV', ... 'password': '<password_for_ADBDEV>', ... 'endpoint': 'https://<hostname-prefix>.adb.<region>.oraclecloudapps.com/' ... } >>> client = AdbClient(config) >>> client.__enter__() <opg4py.adb.AdbClient object at 0x7f35a071acc0>
- 次のコードに示すように、望ましいメモリーでPGXサーバー環境を起動します。これにより、Graph Studioで環境作成のためのジョブが発行されます。
job.get()
は、環境が起動されるまで待機します。環境がclient.isAttached()
で正常に起動したかどうかは常に確認できます。環境が稼働中の場合、メソッドはブールtrue
を戻します。ただし、
client.isAttached()
がコードの最初のステップでtrue
を戻した場合は、環境の作成ステップをスキップできます。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
- 次のように、インスタンスおよびセッション・オブジェクトを作成します。
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")
- Autonomous DatabaseインスタンスからメモリーにPGビュー・グラフをロードします。
opg4j> var graph = session.readGraphByName("BANK_GRAPH", GraphSource.PG_VIEW) graph ==> PgxGraph[name=BANK_GRAPH,N=1000,E=5001,created=1647800790654]
PgxGraph graph = session.readGraphByName("BANK_GRAPH", GraphSource.PG_VIEW);
>>> graph = session.read_graph_by_name("BANK_GRAPH", "pg_view")
- 次のように、アナリストを作成してグラフに対してPagerankアルゴリズムを実行します。
- 次のように、グラフに対してPGQL問合せを実行して結果セットを出力します。
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()
実行すると、問合せによって次の出力が生成されます。+------------------------------------------------------+ | source | pagerank | amount | destination | +------------------------------------------------------+ | 387 | 0.007302836252205922 | 1000.0 | 188 | | 387 | 0.007302836252205922 | 1000.0 | 374 | | 387 | 0.007302836252205922 | 1000.0 | 577 | +------------------------------------------------------+
- 必要に応じて、次のコードに示すように、データベース内のグラフに対してPGQL問合せを直接実行できます。データベースへのJDBC接続を確立するために、ウォレットをダウンロードして安全な場所に保存する必要があります。JDBC URL接続文字列の確認方法は、ウォレットを使用したJDBC Thin接続を参照してください。
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()
実行すると、問合せによって次の出力が生成されます。+-------------------------------+ | SOURCE | AMOUNT | DESTINATION | +-------------------------------+ | 1000 | 1000 | 921 | | 1000 | 1000 | 662 | | 1000 | 1000 | 506 | +-------------------------------+
- 次のように、すべてのグラフ問合せを実行した後、セッションを閉じます。