クイック・スタート

次のタスクを実行して、環境を設定し、Embedded Python ExecutionのREST APIを使用して最初のEmbedded Python Executionジョブを作成します。

前提条件

Embedded Python Execution関数のREST APIでは、Oracle Autonomous DatabaseサービスのOracle Machine Learning for Python (OML4Py)スクリプト・リポジトリにスクリプトの名前を指定する必要があります。スクリプトには、ユーザー定義のPython関数が含まれます。スクリプトを作成してスクリプト・リポジトリに保存する方法の詳細は、Embedded Python Executionおよびスクリプト・リポジトリについてを参照してください。

アカウント情報の取得

Oracleからのアカウント作成電子メールには、Oracle Autonomous Databaseサービスのアイデンティティ・ドメイン名、ユーザー名およびパスワードが含まれます。これらの情報がわからない場合は、サービス管理者に問い合せてください。

プロビジョニングされているOracle Autonomous Databaseから取得できる次のURLも必要です。
  1. Oracle Cloud Infrastructureアカウントにサインインします。OCIユーザー名とパスワードが必要です。
  2. ハンバーガ・メニューをクリックし、プロビジョニングされているAutonomous Databaseインスタンスを選択します。Autonomous Databaseのプロビジョニングの詳細は、Oracle Autonomous Databaseのプロビジョニングを参照してください。
  3. 「データベース・アクション」をクリックします。
  4. データベース・アクション・ページをスクロール・ダウンし、「関連サービス」タイルの下の「Oracle Machine Learning RESTful Services」をクリックします。「Oracle Machine Learning RESTful Services」ダイアログが開きます。「コピー」をクリックして、次のURLを取得します:
    • OMLによって提供されるREST APIのREST認証トークンの取得:
      <oml-cloud-service-location-url>/omlusers/
    • すべての組込みPython REST API:
      <oml-cloud-service-location-url>/oml/

    URL <oml-cloud-service-location-url>には、テナンシID、場所およびデータベース名が含まれます。たとえば、https://qtraya2braestch-omldb.adb.us-sanjose-1.oraclecloudapps.comなどです。

    この例では、
    • qtraya2braestchはテナンシIDです。
    • omldbはデータベース名です。
    • us-sanjose-1はデータセンター・リージョンです
    • oraclecloudapps.comはルート・ドメインです

cURLのインストール

このドキュメントの例では、cURLコマンド行ツールを使用して、Embedded Python ExecutionのREST APIにアクセスする方法を示しています。

次の手順では、Windows 64ビット・システムでのcURLのインストール方法を示します。
  1. ブラウザで、cURLホーム・ページ(http://curl.haxx.se)にアクセスし、左上にある「Download」をクリックします。
  2. 「cURL Releases and Downloads」ページで、使用しているオペレーティング・システムに対応したcURLソフトウェアのSSL対応バージョンを見つけ、ZIPファイルをダウンロードするリンクをクリックしてソフトウェアをインストールします。

サーバーに安全に接続するには、SSLをサポートするバージョンのcURLをインストールする必要があります。

cURLを実行し、次の表に定義したコマンド行オプションを必要に応じて1つ以上指定します。

cURLオプション 説明
-d, --data @file.json ローカル・マシン上にあるJSON形式のリクエストの本文が含まれるファイルを識別します。または、-dを使用してリクエスト本文を渡すこともできます。たとえば、-d '{"input":"select * from IRIS", "parameters":"{\"oml_input_type\":\"pandas.DataFrame\"}", "parallelFlag":true}'です。
-H, --header 次のいずれか、または両方を定義します。
  • サーバーでユーザーを認証するための資格証明を含む認可ヘッダー。
  • リクエスト・ドキュメントのコンテンツ・タイプ

  • レスポンスに使用可能な特定のメディア・タイプを指定するために使用するAcceptヘッダー。

-i, --include 出力にHTTPヘッダーを含めます。
-X リクエストの種類を指定します(GET、POSTなど)。

Rest API認証トークンの取得

認証の説明に従って、認証トークンを取得します。

リクエストの送信

RESTクライアントを設定した後、接続が機能していることを確認するリクエストを送信できます。たとえば、次のcURLコマンドは、OML4Pyスクリプト・リポジトリで使用可能なPythonスクリプトをリストします。

curl -i -X GET --header "Authorization: Bearer ${token}" 
<oml-cloud-service-location-url>/oml/api/py-scripts/v1/scripts

ノート:

このコールには認証が必要です。$tokenは、認可APIを介して取得されたトークンに割り当てられる環境変数を表します。

<oml-cloud-service-location-url> URLには、テナンシID、場所およびデータベース名が含まれます。たとえば、https://qtraya2braestch-omldb.adb.us-sanjose-1.oraclecloudapps.comなどです。

この例では、
  • qtraya2braestchはテナンシIDです。
  • omldbはデータベース名です。
  • us-sanjose-1はデータセンター・リージョンです
  • oraclecloudapps.comはルート・ドメインです。

リクエストが成功すると、次のような本文の200レスポンス・ヘッダーを受信します。

HTTP/1.1 200 OK
Date: Thu, 13 Jan 2022 08:36:42 GMT
Content-Type: application/json
Content-Length: 13
Connection: keep-alive
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Security-Policy: frame-ancestors 'none'
Set-Cookie: JSESSIONID=node0zdh3s4o3x13ua24jd16y44ro111.node0; Path=/oml; Secure; HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT

JSON形式のレスポンス本文は次のとおりです。

{"result":[
{"owner":"OML_USER","date":"2020-08-27T15:53:56.000Z","name":"return_df","description":null,"script":"def return_df(num, scale):\n    import pandas as pd\n    id = list(range(0, int(num)))\n    res = [i/scale for i in id]\n    return pd.DataFrame({\"ID\":id, \"RES\":res})"},
{"owner":"OML_USER","date":"2020-08-27T16:09:17.000Z","name":"RandomRedDots","description":null,"script":"def RandomRedDots (num_dots_1=100, num_dots_2=10):\n  import numpy as np\n  import pandas as pd\n  import matplotlib.pyplot as plt  \n  d = {'id': range(1,10), 'val': [x/100 for x in range(1,10)]}\n  df = pd.DataFrame(data=d)\n  fig = plt.figure(1)\n  ax = fig.add_subplot(111)\n  ax.scatter(range(0,int(num_dots_1)), np.random.rand(int(num_dots_1)),c='r')\n  fig.suptitle(\"Random Red Dots\")\n  fig2 = plt.figure(2)\n  ax2 = fig2.add_subplot(111)\n  ax2.scatter(range(0,int(num_dots_2)), np.random.rand(int(num_dots_2)),c='r')\n  fig2.suptitle(\"Random Red Dots\")\n  return df"},
{"owner":"OML_USER","date":"2020-08-26T20:38:57.000Z","name":"compute_random_mean","description":null,"script":"def compute_random_mean(index):\n    import numpy as np\n    import scipy\n    from statistics import mean \n    np.random.seed(index)\n    res = np.random.random((100,1))*10\n    return mean(res[1])"},
{"owner":"OML_USER","date":"2020-08-18T21:35:06.000Z","name":"group_count","description":null,"script":"def group_count(dat):\n  import oml\n  import pandas as pd\n  return pd.DataFrame([(dat[\"SPECIES\"][0], dat[\"SEPAL_LENGTH\"][0], dat.shape[0])],   columns = [\"SPECIES\",\"SEPAL_LENGTH\", \"COUNT\"])"},
{"owner":"OML_USER","date":"2020-08-21T18:22:38.000Z","name":"my_predict","description":null,"script":"def my_predict(dat):\n    import pandas as pd\n    import oml\n    obj_dict = oml.ds.load(name=\"ds_regr\", to_globals=False)  \n    regr = obj_dict[\"regr\"] \n    pred = regr.predict(dat[['Sepal_Length', \n                             'Sepal_Width',\n                             'Petal_Length']])\n    return pd.concat([dat[['Species', 'Petal_Width']], \n                     pd.DataFrame(pred, \n                                  columns=['Pred_Petal_Width'])], \n                                  axis=1)"},
]}