6.9.2.3 プロパティ・グラフ・ビューの作成

CREATE PROPERTY GRAPH文を使用してプロパティ・グラフ・ビューを作成できます。

JShellを使用したプロパティ・グラフ・ビューの作成

JShellを使用してプロパティ・グラフ・ビューを作成するには、次に示すステップを実行します。

  1. 次のようにJShellを起動してデータベースを操作します。
    ./bin/opg4j --no_connect
  2. 次のようにOracle Databaseに接続します。
    opg4j> var jdbcUrl="jdbc:oracle:thin:@<host_name>:<port>/<service>"
    jdbcUrl ==> "jdbc:oracle:thin:@<host_name>:<port>/<db_service>"
    opg4j> var conn = DriverManager.getConnection(jdbcUrl,"<username>","<password>");
    conn ==> oracle.jdbc.driver.T4CConnection@3e9a20e
    
    前述のコードで:
    • <host_name>: データベース・ホスト
    • <port>: データベース・ポート
    • <service>: データベースSID
    • <username>: ユーザー名
    • <password>: データベース・パスワード
  3. 次のようにPGQL接続を作成します。
    opg4j> var pgqlConn = PgqlConnection.getConnection(conn) 
    pgqlConn ==> oracle.pg.rdbms.pgql.PgqlConnection@4301fa39
  4. 次に示すコマンドを実行して、プロパティ・グラフ・ビューを作成します。
    opg4j> var pgqlStmt = pgqlConn.createStatement(); //create a PGQL Statement
    pgqlStmt ==> oracle.jdbc.driver.OracleStatementWrapper@6f976c
    opg4j> String pgql = 
    ...> "CREATE PROPERTY GRAPH <pgview> "
    ...> + "VERTEX TABLES ( bank_nodes AS Accounts "
    ...> + "KEY (id) "
    ...> + "LABEL Accounts "
    ...> + "PROPERTIES (id, label) "
    ...> + ") "
    ...> + "EDGE TABLES ( bank_edges_amt AS Transfers "
    ...> + "KEY (src_id, dest_id, amount) "
    ...> + "SOURCE KEY (src_id) REFERENCES Accounts "
    ...> + "DESTINATION KEY (dest_id) REFERENCES Accounts "
    ...> + "LABEL Transfers "
    ...> + "PROPERTIES (src_id, dest_id, amount, label) "
    ...> + ") OPTIONS (PG_VIEW) ";
    opg4j> pgqlStmt.execute(pgql);
    $8 ==> false

    プロパティ・グラフ・ビューが作成されます。

Javaを使用したプロパティ・グラフ・ビューの作成

次の例では、Javaを使用してリレーショナル・データベース表からプロパティ・グラフ・ビューを作成する方法を示します。この例では、表bank_nodesおよびbank_edges_amtがすでにデータベースに存在していることを前提としています。

import java.sql.Connection;
import java.sql.Statement;
import oracle.pg.rdbms.pgql.PgqlConnection;
import oracle.pg.rdbms.pgql.PgqlStatement;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;

/**
 * This example shows how to create a Property Graph View from relational
 * data stored in Oracle Database by executing a PGQL create statement.
 */
public class CreatePgView
{

  public static void main(String[] args) throws Exception
  {
    int idx=0;
    String host               = args[idx++]; 
    String port               = args[idx++]; 
    String sid                = args[idx++]; 
    String user               = args[idx++]; 
    String password           = args[idx++];
    String pgview             = args[idx++];

    Connection conn = null;
    PgqlStatement pgqlStmt = null;

    try {

      //Get a jdbc connection
      PoolDataSource  pds = PoolDataSourceFactory.getPoolDataSource();
      pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
      pds.setURL("jdbc:oracle:thin:@"+host+":"+port +"/"+sid);
      pds.setUser(user);
      pds.setPassword(password);     
      conn = pds.getConnection();
      conn.setAutoCommit(false);

      // Get a PGQL connection
      PgqlConnection pgqlConn = PgqlConnection.getConnection(conn);

      // Create a PgqlStatement
      pgqlStmt = pgqlConn.createStatement();

      // Execute PGQL to create property graph view
      String pgql = 
        "CREATE PROPERTY GRAPH " + pgview + " " +
        "VERTEX TABLES ( bank_nodes as Accounts " +
        "KEY (id) " +
        "LABEL \"Accounts\"" +
        "PROPERTIES (id, label)" +
        ") " +
        "EDGE TABLES ( bank_edges_amt as Transfers " +
        "KEY (src_id, dest_id, amount) " +
        "SOURCE KEY (src_id) REFERENCES Accounts " +
        "DESTINATION KEY (dest_id) REFERENCES Accounts " +
        "LABEL \"Transfers\"" +
        "PROPERTIES (src_id, dest_id, amount, label)" +
        ") OPTIONS (PG_VIEW) ";

      pgqlStmt.execute(pgql);

    }
    finally {
      // close the statement
      if (pgqlStmt != null) {
        pgqlStmt.close();
      }
      // close the connection
      if (conn != null) {
        conn.close();
      }
    }
  }
}

プロパティ・グラフ・ビューの作成を確認するには、Oracle Databaseで作成されるメタデータ表を確認します。