获取 NoSQL 句柄
了解如何使用 Oracle NoSQL Database Drivers 访问表。通过创建 NoSQL Handle 开始开发应用程序。使用 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 Handle 的示例:
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");
The first step in any Oracle NoSQL Database Cloud Service Rust application is to create a Handle used to send requests to the service, based on the configuration given in a HandleBuilder struct. Handle 的实例可以安全地同时使用,并打算在多线程或异步应用程序中共享。以下代码示例说明如何使用用户配置文件连接到云服务:
let handle = Handle::builder()
.cloud_auth_from_file("~/.oci/config")?
.build().await?;
配置文件的缺省路径为 ~/.oci/config,其中 ~ 代表用户的起始目录。在 Windows 上,~ 是 USERPROFILE 环境变量的值。该文件可能包含多个配置文件。默认情况下,SDK 使用名为 DEFAULT 的配置文件来存储身份证明
要使用这些默认值,请在 ~/.oci directory 中使用以下内容创建名为 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 配置文件中同时指定区域标识符和身份证明。默认情况下,驱动程序将在 OCI 配置文件中的默认路径和默认配置文件中查找身份证明和区域。仅当未使用 HandleBuilder::cloud_region() 时才需要区域。仅当 RSA 密钥文件需要一个时,才需要 pass_phrase。