使用 Get API 提取数据
使用 GetRequest
API 可使用主键提取单行数据。
默认情况下,所有读取操作最终都是一致的。这种类型的读取比使用绝对一致性的读取成本低。可以使用 Consistency
类中的 setConsistency()
方法更改 NoSQLHandle 实例的默认一致性。
GetRequest
类提供了一种简单而强大的数据读取方式,而查询可用于更复杂的读取请求。要从表中读取数据,请使用 GetRequest
类指定目标表和目标键,并使用 NoSQLHandle.get()
执行请求。操作结果在 GetResult
中可用。可以使用 NoSQLHandleConfig.setConsistency(oracle.nosql.driver.Consistency)
和 GetRequest.setConsistency()
方法更改 NoSQLHandle 实例的默认一致性。
//Fetch single row using get API
private static void getRow(NoSQLHandle handle,String colName,int Id) throws Exception {
MapValue key = new MapValue().put(colName, Id);
GetRequest getRequest = new GetRequest().setKey(key)
.setTableName(tableName);
GetResult getRes = handle.get(getRequest);
/* on success, GetResult.getValue() returns a non-null value */
if (getRes.getValue() != null) {
System.out.println("\t" +getRes.getValue().toString());
} else {
System.out.println("Get Failed");
}
}
注意:
要从子表提取数据,请在setTableName
方法中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.java,以了解如何从此处的父子表中提取数据。
使用 GetRequest
API 可使用主键提取单行数据。可以使用 borneo.NoSQLHandle.get()
方法读取单行。此方法允许您根据记录的主键值检索记录。borneo.GetRequest
类用于简单的 get 操作。它包含目标行的主键值,并返回 borneo.GetResult
的实例。在创建句柄之前,可以使用 borneo.NoSQLHandleConfig.set_consistency()
更改 borneo.NoSQLHandle
的默认一致性。可以使用 borneo.GetRequest.set_consistency()
对单个请求进行更改。
# Fetch single row using get API
def getRow(handle,colName,Id):
request = GetRequest().set_table_name('stream_acct')
request.set_key({colName: Id})
print('Query results: ')
result = handle.get(request)
print('Query results are' + str(result.get_value()))
注意:
要从子表提取数据,请在set_table_name
方法中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.py,以了解如何从此处的父子表中提取数据。
使用 GetRequest
API 可使用主键提取单行数据。可以使用 Client.Get
函数读取单行。使用此功能,您可以根据记录的主键值检索记录。nosqldb.GetRequest
用于简单的 get 操作。它包含目标行的主键值,并返回 nosqldb.GetResult
的实例。如果 get 操作成功,将返回非零值 GetResult.Version
。在创建客户机之前,可以使用 RequestConfig.Consistency
更改 nosqldb.RequestConfig
的默认一致性。可以使用 GetRequest.Consistency
字段对单个请求进行更改。
//fetch data from the table
func getRow(client *nosqldb.Client, err error, tableName string, colName string, Id int)(){
key:=&types.MapValue{}
key.Put(colName, Id)
req:=&nosqldb.GetRequest{
TableName: tableName,
Key: key,
}
res, err:=client.Get(req)
if err != nil {
fmt.Printf("GetResults() failed: %v\n", err)
return
}
if res.RowExists() {
fmt.Printf("Got row: %v\n", res.ValueAsJSON())
} else {
fmt.Printf("The row does not exist.\n")
}
}
注意:
要从子表提取数据,请在TableName
参数中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.go,以了解如何从此处的父子表中提取数据。
使用 get
API 可使用主键提取单行数据。可以使用 get
方法读取单个行。此方法允许您根据记录的主键值检索记录。有关方法详细信息,请参见 NoSQLClient 类。
使用一致性枚举设置读取操作的一致性。可以在初始配置中为读取操作设置缺省一致性,该配置用于使用一致性属性创建 NoSQLClient 实例。还可以通过在 get 方法的 GetOpt 参数中设置一致性属性来为单个读取操作更改该属性。
//fetches single row with get API
async function getRow(handle,idVal) {
try {
const result = await handle.get(TABLE_NAME,{acct_Id: idVal });
console.log('Got row: %O', result.row);
} catch(error) {
console.error(' Error: ' + error.message);
}
}
get
and other data-related methods. The type parameter allows the TypeScript compiler to provide type hints for the returned GetResult
method, as well as type-check the passed key. While type checking, the compiler inspects if the primary key field is a part of the table schema and if the type of the primary key field is one of the allowed types, that is either string, numeric, date or boolean. Download the full code QueryData.ts from the examples here.interface StreamInt {
acct_Id: Integer;
profile_name: String;
account_expiry: TIMESTAMP;
acct_data: JSON;
}
/*fetches single row with get API*/
async function getRow(handle: NoSQLClient,idVal: Integer) {
try {
const result = await handle.get<StreamInt>(TABLE_NAME,{acct_Id: idVal });
console.log('Got row: %O', result.row);
} catch(error) {
console.error(' Error: ' + error.message);
}
}
使用 GET API 获取使用主键的单个数据行。可以使用 GetAsync
方法读取单个行。此方法允许您根据行的主键值检索行。字段名应与表主键列名相同。也可以将选项传递为 GetOptions
。
可以使用一致性枚举设置读取操作的一致性。读取操作的缺省一致性可以设置为 NoSQLConfig
的一致性属性。您还可以使用一致性属性 GetOptions
更改单个 Get 操作的一致性。GetAsync
方法返回 Task<GetResult<RecordValue>>
。GetResult
实例包含返回的行、行版本和其他信息。如果表中不存在具有提供的主键的行,则行属性和版本属性的值将为 Null。
private static async Task getRow(NoSQLClient client,String colName, int Id){
var result = await client.GetAsync(TableName,
new MapValue
{
[colName] =Id
});
if (result.Row != null){
Console.WriteLine("Got row: {0}\n", result.Row.ToJsonString());
}
else {
Console.WriteLine("Row with primaryKey {0} doesn't exist",colName);
}
}
注意:
要从子表提取数据,请在TableName
参数中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.cs,以了解如何从此处的父子表中提取数据。
相关主题