获取 NoSQL 句柄

了解如何使用 Oracle NoSQL Database Drivers 访问表。通过创建 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 句柄。客户端句柄的实例可以安全地由多个 goroutines 并发使用,并打算在多 goroutines 应用程序中共享。该句柄是使用您的凭证和其他验证信息配置的。
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");