4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Always Free 環境で構築するグラフデータベース!

Last updated at Posted at 2020-05-10

##本記事の目的
Always Free環境でOracle Autonomous Databaseに保存されているデータをグラフとして読み込み、Graph Vizで可視化するまでの手順をご紹介します。

##前提条件

  • Graph Server and Clientがダウンロード済みであること

まだダウンロードしていない方はこちらからOracle Graph Server and Clientをダウンロードしてください。
インストール手順はDocumentこちらの記事を参考にしてください。

##Always Free環境の構築
###Computeインスタンス作成

こちらの記事を参考に、仮想クラウドネットワーク・Computeの作成を行ってください。

###Autonomous Database作成

####インスタンス作成

ナビゲーション・メニュー > データベース > Autonomous Data Warehouseを選択します。

image.png

「Autonomous Databaseを作成」ダイアログで以下の項目を入力します。

  • 基本データベース情報
    • 表示名
    • コンパートメント
    • Databaseの名前
  • ワークロードタイプ
    • Autonomous Data Warehouse または Autonomous Transaction Processing
  • データベースCPUコア数およびストレージの構成
    • Always Freeオプションを選択
  • 管理者資格証明
    • 以下の基準を満たすパスワードを入力
      • 12から30文字までの長さ
      • 1つ以上の小文字を含む
      • 少なくとも1つの大文字を含む
      • 少なくとも1つの番号が含む
      • 二重引用符( ")は含まない
      • 大/小文字に関係なく、文字列adminは含まない
  • ライセンス・タイプ
    • Always Freeの場合は調整不要

無題1.png

上記の入力が終わったら、「Autonomous Databaseの作成」をクリックします。

無題1.png

プロビジョニングが完了すると、下記のように緑色のマークに変化します。

無題1.png

####walletファイルダウンロード

DB接続をクリックします。

無題1.png

下記の画面から、ウォレットのダウンロードをクリックし、ウォレットをダウンロードします。

image.png

ダウンロードしたウォレットは、作成したComputeインスタンスの任意のディレクトリに解凍しておきましょう。

####今回使用するサンプルデータ作成

SQL Developer Webから、今回使用するサンプルデータを作成します。

サービスコンソール > SQL Developer Webをクリックしてください。

image.png

image.png

以下を指定してログインします。

  • ユーザー名 : ADMIN
  • パスワード : Autonomous Database作成時に指定したパスワード

無題1.png

ログイン完了後、以下のSQLで今回使用するスキーマを作成し、権限を付与します。

CREATE USER customer_360 IDENTIFIED BY pass;
GRANT CREATE SESSION to customer_360;
GRANT CREATE TABLE to customer_360;
GRANT ALTER SESSION to customer_360;
GRANT ALTER ANY TABLE to customer_360;

Autonomous Databaseに、サンプルグラフで使用するデータをあらかじめロードしておきます。
ロード時のスクリプトは、こちらを参照して下さい。

##Two-TierとThree-Tierの違い

image.png

アーキテクチャの観点では、Two-TierモードThree-Tierモードの2種類があります。

  • Two-Tierモード
    Graph Clientとなるアプリケーションが直接Oracle Databaseに接続する構成です。

  • Three-Tierモード
    Graph ClientとなるアプリケーションはGraph Serverに接続し、仲介するGraph ServerがOracle Databaseに接続します。

##グラフデータベース作成手順(Two-Tier / Three-Tier 共通)
以下の作業は全て作成したComputeインスタンスにて行ってください。

###1. opg-rdbms-jshell起動

opg-jshell-rdbms> var jdbcUrl = "jdbc:oracle:thin:@<tnsnames.ora内の接続記述子>?TNS_ADMIN=<Walletファイルを解凍したディレクトリを指定>"

実行例

opg-jshell-rdbms> var jdbcUrl = "jdbc:oracle:thin:@db201901151442_low?TNS_ADMIN=/etc/wallet"

###2. jdbc connection, user, password定義

opg-jshell-rdbms> var user = "<DB Username>"
user ==> "○○○"
opg-jshell-rdbms> var pass = "<DB User Password>"
pass ==> "△△△"

実行例

opg-jshell-rdbms> var user = "customer_360"
user ==> "customer_360"
opg-jshell-rdbms> var pass = "Welcome1"
pass ==> "Welcome1"

###3. DriverManager.getConnectionで接続

opg-jshell-rdbms> var conn = DriverManager.getConnection(jdbcUrl, user, pass)
conn ==> oracle.jdbc.driver.T4CConnection@6d484bf4

###4. Graph Clientからグラフを作成

以下のPGQLを記載したファイルを作成します。
(今回は、/opt/oracle/graph/graph_data/create_pg.pgqlに作成します。)

/opt/oracle/graph/graph_data/create_pg.pgql
CREATE PROPERTY GRAPH customer_360
    VERTEX TABLES(
        customer
            LABEL "customer"
            PROPERTIES(
                type as "type"
               ,name as "name"
               ,age as "age"
               ,location as "location"
               ,gender as "gender"
               ,student as "student"
               )
       ,account
            LABEL "account"
            PROPERTIES(
                type as "type"
               ,account_no as "account_no"
               ,balance as "balance"
               )
       ,merchant
            LABEL "merchant"
            PROPERTIES(
                type as "type"
               ,name as "name"
               )
       )
    EDGE TABLES(
        owned_by
            SOURCE KEY(from_id) REFERENCES account
            DESTINATION KEY(to_id) REFERENCES customer 
            LABEL "owned_by" 
            PROPERTIES ( 
                since AS "since" 
            ) 
       ,parent_of 
            SOURCE KEY(from_id) REFERENCES customer 
            DESTINATION KEY(to_id) REFERENCES customer 
            LABEL "parent_of" 
       ,purchased 
            SOURCE KEY(from_id) REFERENCES account 
            DESTINATION KEY(to_id) REFERENCES merchant 
            LABEL "purchased" 
            PROPERTIES ( 
                amount AS "amount" 
            ) 
       ,transfer 
            SOURCE KEY(from_id) REFERENCES account 
            DESTINATION KEY(to_id) REFERENCES account 
            LABEL "transfer" 
            PROPERTIES ( 
                amount AS "amount" 
              , date AS "date" 
            ) 
      ) 

作成したpgqlファイルをjshellで読み込み、PQGLを実行します。

opg-jshell-rdbms> conn.setAutoCommit(false)
opg-jshell-rdbms> var pgql = PgqlConnection.getConnection(conn)
opg-jshell-rdbms> pgql.prepareStatement(Files.readString(Paths.get("/opt/oracle/graph/graph_data/create_pg.pgql"))).execute()
opg-jshell-rdbms> Consumer<String> query = q -> {
  try(var s = pgql.prepareStatement(q)){
    s.execute();
    s.getResultSet().print();
 }catch(Exception e){
    throw new RuntimeException(e);
 }
}
opg-jshell-rdbms> query.accept("select count(v) from customer_360 match(v)");

件数が返ってくれば成功です。

##グラフデータベース作成手順(Three-Tierで使用する場合の追加手順)

###5. Databaseのパスワードを含んだJava Keystore作成

$ keytool -importpass -alias nwpg -keystore nwpgkeystore.p12 -storetype pkcs12
キーストアのパスワードを入力してください: <Keystoreのパスワードを入力>  
保存するパスワードを入力してください:  <保存するパスワードを入力>
パスワードを再入力してください: <保存するパスワードを再入力>

Java 8 以前のJDKを使用している場合は、-storetype pkcs12オプションが必要です。

###6. JVMにTNS_ADMIN環境変数をオーバーライドして読み込ませるように設定

$ export JAVA_OPTS=“-Doracle.net.tns_admin=<ダウンロードしたウォレットを展開したディレクトリ> -Doracle.jdbc.fanEnabled=false”

実行例

$ export JAVA_OPTS=“-Doracle.net.tns_admin=/home/oracle/wallet -Doracle.jdbc.fanEnabled=false”

###7. 手順5で作成したJava Keystoreを指定してGraph Serverを起動

$ cd /opt/oracle/graph
$ ./pgx/bin/start-server --secret-store /home/oracle/wallet/nwpgkeystore.p12

###8. Graph Vizで可視化

  • http://<サーバのIPアドレス>:7007/ui に接続
    image.png

Autonomous Databaseからグラフをロードできました!

##まとめ

今回は、Autonomous Databaseに保存されているデータを用いてグラフを作成しました。
サポート範囲が広がってきているPGXを活用して、どんどんネットワーク分析をしましょう!

(本記事は、@mtc465@eyebmah0 の協力のもと執筆しました。)

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?