使用 API 來新增表格資料

在表格中新增資料列。當您將資料儲存在表格資料列中時,應用程式可以輕易地從表格擷取、新增或刪除資訊。

您可以使用 PutRequest 類別 / put 方法來執行無條件和條件式置入:
  • 覆寫任何現有的資料列。「覆寫」是預設功能。
  • 只有在資料列不存在時才成功。在此情況下,請使用 IfAbsent 方法。
  • 只有在資料列存在時才成功。在此情況下,請使用 IfPresent 方法。
  • 只有在資料列存在且版本符合特定版本時才成功。在此案例使用 IfVersion 方法,使用 setMatchVersion 方法指定要比對的版本。

PutRequest 類別提供 setValueFromJson 方法,採用 JSON 字串,並用來填入要插入表格的資料列。JSON 字串應指定與表格欄位名稱對應的欄位名稱。

Download the full code AddData.java from the examples here.
private static void writeRows(NoSQLHandle handle, MapValue value) 
                                               throws Exception {
   PutRequest putRequest =
     new PutRequest().setValue(value).setTableName(tableName);
   PutResult putResult = handle.put(putRequest);

   if (putResult.getVersion() != null) {
      System.out.println("Added a row to the stream_acct table");
   } else {
      System.out.println("Put failed");
   }
}

borneo.PutRequest 類別代表用來插入單一資料列之 borneo.NoSQLHandle.put() 方法的輸入。

您也可以在表格中加入 JSON 資料。如果是固定綱要表格,JSON 會轉換成目標綱要。JSON 資料可直接插入到 JSON 類型的資料欄中。使用 JSON 資料類型可讓您建立不含固定綱要的表格資料,以便更靈活地使用資料。

Download the full code AddData.py from the examples here.
def insert_record(handle,table_name,acct_data):
  request = PutRequest().set_table_name(table_name)
                        .set_value_from_json(acct_data)

  handle.put(request)
  print('Added a row to the stream_acct table')

nosqldb.PutRequest 代表 nosqldb.Put() 函數的輸入,可用來插入單一資料列。

提供的資料列資料值 (在 PutRequest 中) 是 *types.MapValueMapValue 中每個項目的 key 部分必須符合目標表格的資料欄名稱,而 value 部分必須是資料欄的有效值。JSON 資料也可以直接插入到 JSON 類型的資料欄中。使用 JSON 資料類型可讓您建立不含固定綱要的表格資料,以便更靈活地使用資料。

Download the full code AddData.go from the examples here.
func insertData(client *nosqldb.Client, err error, 
                tableName string,value1 *types.MapValue )(){
  putReq := &nosqldb.PutRequest{
    	TableName: tableName,
    	Value: value1,
  }
  putRes, err := client.Put(putReq)
  if err != nil {
     fmt.Printf("failed to put single row: %v\n", err)
     return
  }
  fmt.Printf("Added a row to the stream_acct table\n")
}

您可以使用 put 方法將單一資料列插入表格。如需方法詳細資訊,請參閱 NoSQLClient 類別。

JavaScript: Download the full code AddData.js from the examples here.
/* Adding 3 records in acct_stream table */
let putResult = await handle.put(TABLE_NAME, JSON.parse(acct1));
let putResult1 = await handle.put(TABLE_NAME, JSON.parse(acct2));
let putResult2 = await handle.put(TABLE_NAME, JSON.parse(acct3));

console.log("Added rows to the stream_acct table");
TypeScript: Download the full code AddData.ts from the examples here.
interface StreamInt {
   acct_Id: Integer;
   profile_name: String;
   account_expiry: TIMESTAMP;
   acct_data: JSON;
}
/* Adding 3 records in acct_stream table */
let putResult = await handle.put<StreamInt>(TABLE_NAME, JSON.parse(acct1));
let putResult1 = await handle.put<StreamInt>(TABLE_NAME, JSON.parse(acct2));
let putResult2 = await handle.put<StreamInt>(TABLE_NAME, JSON.parse(acct3));

console.log("Added rows to the stream_acct table");

方法 PutAsync 和相關方法 PutIfAbsentAsyncPutIfPresentAsyncPutIfVersionAsync 用於將單一列插入表格或更新單一列。

上述每個 Put 方法都會傳回 Task<PutResult<RecordValue>>PutResult 執行處理包含已完成 Put 作業的相關資訊,例如成功狀態 (如果不符合對應的條件,條件置放作業可能會失敗) 和產生的 RowVersion。請注意,結果的「成功」特性僅表示與條件「放置」作業相關的成功完成,對於無條件「置放」一律為真。如果 Put 作業因任何其他原因而失敗,則會發出異常狀況。使用資料類型 JSON 的欄位可更靈活地使用資料,因為 JSON 欄位中的資料沒有預先定義的綱要。若要將值放入 JSON 欄位,請在資料列值中提供 MapValue 執行處理作為其欄位值。您也可以透過 FieldValue.FromJsonString 從 JSON 字串建立其值。

Download the full code AddData.cs from the examples here.
private static async Task insertData(NoSQLClient client, String acctdet){
   var putResult = await client.PutAsync(TableName, 
                              FieldValue.FromJsonString(acctdet).AsMapValue);
   if (putResult.ConsumedCapacity != null)
   {
      Console.WriteLine(" Added a row to the stream_acct table");      
   }
}

您可以使用 MultiWrite API 在單一資料庫作業中新增多個資料列。

您可以使用 WriteMultipleRequest 類別,執行與主索引鍵共用相同分區索引鍵部分之表格關聯的 PutRequest 作業序列,作為單一單元寫入作業。您也可以使用 WriteMultipleRequest 類別,同時將資料新增至父項和子項表格。這是自動修改多個相關列的有效方法。如果作業成功,WriteMultipleResult.getSuccess() 方法會傳回 true。

請參閱 Oracle NoSQL Java SDK API Reference ,瞭解各種類別和方法的詳細資訊。

Download the full code MultiWrite.java from the examples here.
private static void writeMul(NoSQLHandle handle,String parent_tblname, 
String parent_data, String child_tblname, String child_data){
      WriteMultipleRequest umRequest = new WriteMultipleRequest();
      PutRequest putRequest =
         new PutRequest().setValueFromJson(parent_data,null).setTableName(parent_tblname);
      umRequest.add(putRequest, false);
      putRequest =
         new PutRequest().setValueFromJson(child_data,null).setTableName(child_tblname);
      umRequest.add(putRequest, false);
      WriteMultipleResult umResult = handle.writeMultiple(umRequest);
}

您可以使用 borneo.WriteMultipleRequest 類別,執行與主索引鍵共用相同分區索引鍵部分之表格關聯的 PutRequest 作業序列,作為單一單元寫入作業。您也可以使用 borneo.WriteMultipleRequest 類別,同時將資料新增至父項和子項表格。這是自動修改多個相關列的有效方法。

請參閱 Oracle NoSQL Python SDK API Reference ,瞭解各種類別和方法的詳細資訊。

Download the full code MultiWrite.py from the examples here.
def mul_write(handle,parent_tblname,parent_data,
child_tblname, child_data):
    request = PutRequest()
    request.set_value_from_json(parent_data)
    request.set_table_name('ticket')
    wm_req.add(request, True)
    request1 = PutRequest()
    request1.set_table_name(child_tblname)
    request1.set_value_from_json(child_data)
    wm_req.add(request1, True)
    result = handle.write_multiple(wm_req)

您可以使用 WriteMultipleRequest 類別,執行與主索引鍵共用相同分區索引鍵部分之表格關聯的 PutRequest 作業序列,作為單一單元寫入作業。您也可以使用 WriteMultipleRequest 類別,同時將資料新增至父項和子項表格。這是自動修改多個相關列的有效方法。

請參閱 Oracle NoSQL Go SDK API Reference ,瞭解各種類別和方法的詳細資訊。

Download the full code MultiWrite.go from the examples here.
//multiple write from the table
func mul_write(client *nosqldb.Client, err error, parenttbl_name string,
parent_data string, childtbl_name string, child_data string)(){
   value, err := types.NewMapValueFromJSON(parent_data)
	putReq := &nosqldb.PutRequest{
      TableName: parenttbl_name,
      Value:     value,
   }
	wmReq := &nosqldb.WriteMultipleRequest{
		TableName: "",
		Timeout:   10 * time.Second,
	}
   wmReq.AddPutRequest(putReq, true)

	value1, err := types.NewMapValueFromJSON(child_data)
	putReq1 := &nosqldb.PutRequest{
      TableName: childtbl_name,
      Value:     value1,
   }
   wmReq.AddPutRequest(putReq1, true)
	wmRes, err := client.WriteMultiple(wmReq)
	if err != nil {
		fmt.Printf("WriteMultiple() failed: %v\n", err)
		return
	}
	if wmRes.IsSuccess() {
		fmt.Printf("WriteMultiple() succeeded\n")
	} else {
		fmt.Printf("WriteMultiple() failed\n")
	}
}

您可以使用 writeMany 方法,執行一系列與主索引鍵共用相同分區索引鍵部分之表格關聯的放置作業,作為單一單元寫入作業。您也可以使用 writeMany 方法,同時將資料新增至父項和子項表格。這是自動修改多個相關列的有效方法。

如需方法詳細資訊,請參閱 NoSQLClient 類別。

JavaScript: Download the full code MultiWrite.js from the examples here.
const ops = [
               {
                  tableName: 'ticket',
                  put: {
                     "ticketNo": "1762344493810",
                     "confNo" : "LE6J4Z"
                  },
                  abortOnFail: true
               },
               {
                  tableName: 'ticket.bagInfo',
                  put: {
                     "ticketNo":"1762344493810",
                     "id":"79039899165297",
                     "tagNum":"17657806255240",
                     "routing":"MIA/LAX/MEL",
                     "lastActionCode":"OFFLOAD",
                     "lastActionDesc":"OFFLOAD",
                     "lastSeenStation":"MEL",
                     "lastSeenTimeGmt":"2019-02-01T16:13:00Z",
                     "bagArrivalDate":"2019-02-01T16:13:00Z"
                   },
                  abortOnFail: true
               }
            ];

const res = await handle.writeMany(ops, null);
TypeScript: Download the full code MultiWrite.ts from the examples here.
const ops = [
               {
                  tableName: 'ticket',
                  put: {
                     "ticketNo": "1762344493810",
                     "confNo" : "LE6J4Z"
                  },
                  abortOnFail: true
               },
               {
                  tableName: 'ticket.bagInfo',
                  put: {
                     "ticketNo":"1762344493810",
                     "id":"79039899165297",
                     "tagNum":"17657806255240",
                     "routing":"MIA/LAX/MEL",
                     "lastActionCode":"OFFLOAD",
                     "lastActionDesc":"OFFLOAD",
                     "lastSeenStation":"MEL",
                     "lastSeenTimeGmt":"2019-02-01T16:13:00Z",
                     "bagArrivalDate":"2019-02-01T16:13:00Z"
                   },
                  abortOnFail: true
               }
            ];

const res = await handle.writeMany(ops, null);

您可以使用 PutManyAsync 方法,執行一系列與主索引鍵共用相同分區索引鍵部分之表格關聯的放置作業,作為單一單元寫入作業。您也可以使用 PutManyAsync 方法,同時將資料新增至父項和子項表格。這是自動修改多個相關列的有效方法。

請參閱 Oracle NoSQL Dotnet SDK API Reference ,瞭解所有類別和方法的詳細資訊。

Download the full code MultiWrite.cs from the examples here.
private static async Task mul_write(NoSQLClient client,string parentbl_name,
string data1, string childtbl_name, string data2){
    var result = await client.WriteManyAsync(
        new WriteOperationCollection()
          .AddPut(parentbl_name, FieldValue.FromJsonString(data1).AsMapValue)
          .AddPut(childtbl_name, FieldValue.FromJsonString(data2).AsMapValue)
         );
    }