使用 Get API 提取数据
使用 GetRequest API 可使用主键提取单行数据。
缺省情况下,所有读取操作最终保持一致。与使用绝对一致性的读数相比,这种类型的读取成本更低。通过在 Consistency 类中使用 setConsistency() 方法,可以更改 NoSQLHandle 实例的默认一致性。
GetRequest 类提供了一种简单而强大的数据读取方法,而查询可用于更复杂的读取请求。要从表中读取数据,请使用 GetRequest 类指定目标表和目标键,并使用 NoSQLHandle.get() 执行请求。操作的结果在 GetResult 中可用。可以使用 NoSQLHandleConfig.setConsistency(oracle.nosql.driver.Consistency) 和 GetRequest.setConsistency() 方法更改 NoSQLHandle 实例的默认一致性。
Download the full code QueryData.java from the examples here.
//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() 对单个请求进行更改。
Download the full code QueryData.py from the examples here.
# 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 字段进行更改。
Download the full code QueryData.go from the examples here.
//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 参数中设置一致性属性来更改单个读取操作。
JavaScript: Download the full code QueryData.js from the examples here.
//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);
}
}
TypeScript: You can also supply an optional typeparameter for a row to get and other data-related methods. The typeparameter 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 typeof 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);
}
}
注:要从子表提取数据,请在 TABLE_NAME 参数中指定表的全名 (parent_tablename.child_tablename)。请从 TableJoins.js 此处下载完整的 JavaScript 代码以及完整的 TypeScript 代码 TableJoins.ts 此处,了解如何从父子表提取数据。
使用 GET API 可使用主键提取单行数据。可以使用 GetAsync 方法读取单个行。使用此方法可以基于行主键值检索行。字段名应与表主键列名相同。您也可以将选项作为 GetOptions 传递。
可以使用一致性枚举设置读取操作的一致性。读取操作的缺省一致性可以设置为 NoSQLConfig 的 Consistency 属性。您还可以使用 Consistency 属性 GetOptions 更改单个 Get 操作的一致性。GetAsync 方法返回 Task<GetResult<RecordValue>>。GetResult 实例包含返回的行、行版本和其他信息。如果表中不存在具有所提供主键的行,则“行”和“版本”属性的值均为 NULL。
Download the full code QueryData.cs from the examples here.
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,以了解如何从此处的父子表提取数据。