使用 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 可用於簡單的取得作業。它包含目標資料列的主索引鍵值,並且傳回 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:您也可以為 get 和其他資料相關方法的資料列提供選擇性的類型參數。type 參數允許 TypeScript 編譯器提供傳回之 GetResult 方法的 type 提示,以及 type-check 傳送的金鑰。檢查 type 時,編譯器會檢查主索引鍵欄位是否為表格綱要的一部分,如果主索引鍵欄位的類型是允許的類型之一,也就是字串、數值、日期或布林值。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) 的完整名稱。Download the full JavaScript code TableJoins.js here and the full TypeScript code TableJoins.ts here to understand how to fetch data from a parent-child table.

使用 GET API 使用主索引鍵擷取單一資料列的資料。您可以使用 GetAsync 方法讀取單一資料列。此方法可讓您根據資料列的主索引鍵值擷取資料列。欄位名稱應與表格主索引鍵資料欄名稱相同。您也可以傳送選項作為 GetOptions

您可以使用「一致性」列舉設定讀取作業的一致性。讀取作業的預設一致性可以設為 NoSQLConfig 的「一致性」特性。您也可以使用 GetOptions 的「一致性」特性來變更單一 Get 作業的一致性。GetAsync 方法會傳回 Task<GetResult<RecordValue>>GetResult 執行處理包含傳回的資料列、資料列版本以及其他資訊。如果表格中沒有具有提供之主索引鍵的資料列,則「資料列」和「版本」特性的值將為空值。

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,瞭解如何從此處的父項子項表格擷取資料。