取得 NoSQL 處理作業

瞭解如何使用 Oracle NoSQL Database 驅動程式存取表格。透過建立 NoSQL 處理程式開始開發應用程式。使用 NoSQLHandle 存取表格及執行所有作業。

若要建立以 NoSQLHandle 表示的連線,請使用 NoSQLHandleFactory.createNoSQLHandle 方法和 NoSQLHandleConfig 類別取得控點。NoSQLHandleConfig 類別可讓應用程式指定控點組態。如需更多資訊,請參閱 Java API 參考指南

使用下列程式碼取得 NoSQL 處理作業:

/* Configure a handle for the desired Region and AuthorizationProvider.
 * By default this SignatureProvider constructor reads authorization
 * information from ~/.oci/config and uses the default user profile and
 * private key for request signing. Additional SignatureProvider
 * constructors are available if a config file is not available or
 * desirable.
 */
AuthorizationProvider ap = new SignatureProvider();

/* Use the us-ashburn-1 region */
NoSQLHandleConfig config = new NoSQLHandleConfig(Region.US_ASHBURN_1, ap);
config.setAuthorizationProvider(ap);

/* Sets a default compartment for all requests from this handle. This
 * may be overridden in individual requests or by using a
 * compartment-name prefixed table name.
 */
config.setDefaultCompartment("mycompartment");

// Open the handle
NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config);

// Use the handle to execute operations

控點具有相關聯的記憶體和網路資源。使用 NoSQLHandle.close 方法可在應用程式使用控點完成時釋放資源。

為了將網路活動、資源配置及取消配置負荷降到最低,最好避免重複建立和關閉控制碼。例如,在每個作業周圍建立和關閉控點會導致應用程式效能不佳。處理作業會允許並行作業,因此單一控制碼就足以存取多重執行緒應用程式中的表格。建立多個控點會產生額外的資源負荷,而不會提供任何效能優點。

控點是先建立 borneo.NoSQLHandleConfig 執行處理來設定通訊端點、授權資訊,以及處理組態的預設值所建立。borneo.NoSQLHandleConfig 代表服務的連線。建立之後,必須使用 borneo.NoSQLHandle.close() 方法將其關閉,才能清除資源。控點具有執行緒安全特性,並旨在共用。

取得 Oracle NoSQL Cloud Service 的 NoSQL 處理作業範例:

from borneo import NoSQLHandle, NoSQLHandleConfig, Regions
from borneo.iam import SignatureProvider
# create AuthorizationProvider
provider = SignatureProvider()
# create handle config using the correct desired region
# as endpoint, add a default compartment.
config = NoSQLHandleConfig(Regions.US_ASHBURN_1).
set_authorization_provider(provider).
set_default_compartment('mycompartment')
# create the handle
handle = NoSQLHandle(config)

注意:若要減少建立控點的資源使用量和超載,最好避免建立和關閉過多的 borneo.NoSQLHandle 執行處理。

任何 Oracle NoSQL Database Cloud Service go 應用程式中的第一步是建立用於傳送要求至服務的 nosqldb.Client 處理。「用戶端」控點的例項可安全用於多個地域並行使用,且意圖在多重地域應用程式中共用。控點是使用您的證明資料和其他認證資訊來設定。

provider, err := iam.NewSignatureProviderFromFile(cfgfile, profile, passphrase, compartment)
cfg := nosqldb.Config
{
   Region: "us-phoenix-1", AuthorizationProvider: provider,
}
client, err := nosqldb.NewClient(cfg)
// use client for all NoSQL DB operations

NoSQLClient 類別代表服務的主要存取點。若要建立 NoSQLClient 執行處理,您必須提供適當的組態資訊。此資訊由 Config 物件表示,並可提供給 NoSQLClient 類別的建構子。或者,您可以選擇將此資訊儲存在 JSON 組態檔和 NoSQLClient 的建構子中,並搭配該檔案的路徑 (絕對或相對於應用程式的目前目錄)。如需方法詳細資訊,請參閱 NoSQLClient 類別。

下方的第一個範例使用 Config 物件建立 Cloud Service 的 NoSQLClient 執行處理。它也會新增預設區間,並覆寫組態物件中的預設逾時值。

import { NoSQLClient, Region } from 'oracle-nosqldb';

let client = new NoSQLClient({
    region: Region.US_ASHBURN_1,
    timeout: 20000,
    ddlTimeout: 40000,
    compartment: 'mycompartment',
    auth: {
        iam: {
            configFile: '~/myapp/.oci/config',
            profileName: 'Jane'
        }
    }
});

第二個範例將相同的組態儲存在 JSON 檔案 config.json 中,並使用它來建立 NoSQLClient 執行處理。

config.json 檔案範例:

{
"region": "US_ASHBURN_1",
 "timeout": 20000,
 "ddlTimeout": 40000,
 "compartment": "mycompartment",
 "auth": {
    "iam": {
       "configFile": "~/myapp/.oci/config",
       "profileName": "Jane"
    }
  }
}

應用程式碼:

import { NoSQLClient } from 'oracle-nosqldb';
let client = new NoSQLClient('config.json');

類別 NoSQLClient 代表服務的主要存取點。若要建立 NoSQLClient 執行處理,您必須提供適當的組態資訊。此資訊由 NoSQLConfig 類別表示,執行處理可提供給 NoSQLClient 的建構子。或者,您可以選擇將組態資訊儲存在 JSON 組態檔中,然後使用 NoSQLClient 的建構子,將路徑 (絕對或相對於目前的目錄) 帶到該檔案。

下方的第一個範例使用 NoSQLConfig 建立 Cloud Service 的 NoSQLClient 執行處理。它也會新增預設區間,並覆寫 NoSQLConfig 中的部分預設逾時值。

var client = new NoSQLClient(
    new NoSQLConfig
    {
        Region = Region.US_ASHBURN_1,
        Timeout = TimeSpan.FromSeconds(10),
        TableDDLTimeout = TimeSpan.FromSeconds(20),
        Compartment = "mycompartment",
        AuthorizationProvider = new IAMAuthorizationProvider(
            "~/myapp/.oci/config", "Jane")
    });

第二個範例將相同的組態儲存在 JSON 檔案 config.json 中,並使用它來建立 NoSQLClient 執行處理。

config.json

{
    "Region": "us-ashburn-1",
    "Timeout": 20000,
    "TableDDLTimeout": 40000,
    "compartment": "mycompartment",
    "AuthorizationProvider":
    {
        "AuthorizationType": "IAM",
        "ConfigFile": "~/myapp/.oci/config",
        "ProfileName": "Jane"
    }
}

應用程式碼:

var client = new NoSQLClient("config.json");

任何 Oracle NoSQL Database Cloud Service Rust 應用程式中的第一步,都是根據 HandleBuilder 結構中提供的組態,建立用來傳送要求給服務的 Handle。「控制碼」的執行處理可安全用於並行使用,並用於在多執行緒或非同步應用程式中共用。下列程式碼範例顯示如何使用使用者組態檔連線至雲端服務:

let handle = Handle::builder()
.cloud_auth_from_file("~/.oci/config")?
.build().await?;

配置檔案的預設路徑為 ~/.oci/config,其中 ~ 代表使用者的主目錄。在 Windows 上,~USERPROFILE 環境變數的值。檔案可以包含多個設定檔。SDK 預設會使用名為 DEFAULT 的設定檔來儲存證明資料

若要使用這些預設值,請在 ~/.oci 目錄中以下列內容建立名為 config 的檔案:

[DEFAULT]
tenancy=<your-tenancy-id>
user=<your-user-id>
fingerprint=<fingerprint-of-your-public-key>
key_file=<path-to-your-private-key-file>
pass_phrase=<optional-passphrase>
region=<optional-region-identifier>

請注意,您也可以在 OCI 組態檔中同時指定區域 ID 與證明資料。依照預設,驅動程式會在預設路徑和預設設定檔中尋找 OCI 組態檔中的證明資料和區域。只有在未使用 HandleBuilder::cloud_region() 時,才需要區域。The pass_phrase is only required if the RSA key file requires one.