SDK を使用してバッチ操作の結果を確認する

完了

TransactionalBatchResponse クラスには、バッチ操作の結果を調査するための複数のメンバーが含まれています。 StatusCode プロパティは、特に HttpStatusCode 型であり、一般的なトランザクション エラーのエラー メッセージを簡単に識別できる HTTP 状態コードを返します。

response.StatusCode

IsSuccessStatusCode プロパティは、結果を抽出する前にアプリケーション コードで直接使用できるブール型プロパティです。

if (batchResponse.IsSuccessStatusCode)
{
}

GetOperationResultAtIndex<> ジェネリック メソッドは、指定されたインデックスで逆シリアル化された個々の項目を返します。

TransactionalBatchOperationResult<Product> result = response.GetOperationResultAtIndex<Product>(0);
Product firstProductResult = result.Resource;

TransactionalBatchOperationResult<Product> result = response.GetOperationResultAtIndex<Product>(1);
Product secondProductResult = result.Resource;

メソッドによって返される execute_item_batch オブジェクトは、バッチ要求内の各操作の結果を調べる手段を提供します。 各操作の結果には、0 から始まるインデックスを使用してアクセスでき、HTTP 状態コードや操作の応答本文などの詳細が提供されます。

バッチ内のすべての操作を反復処理して、結果にアクセスできます。

for result in batch_response:
    resource_body = result.get("resourceBody")
    print(f"Operation result: {resource_body}")

バッチ応答オブジェクトの get メソッドを使用して、個々の操作の状態コード、応答本文、および要求料金を確認できます。

first_item_result = batch_response[0]
first_item_status_code = first_item_result.get("statusCode")
first_item_request_charge = first_item_result.get("requestCharge")
first_item_result = first_item_result.get("resourceBody")
print(f"First item status: {first_item_status_code}; request charge: {first_item_request_charge}; result: {first_item_result}")

second_item_result = batch_response[1]
second_item_status_code = second_item_result.get("statusCode")
second_item_request_charge = second_item_result.get("requestCharge")
second_item_result = second_item_result.get("resourceBody")
print(f"Second item status: {second_item_status_code}; request charge: {second_item_request_charge}; result: {second_item_result}")

バッチ操作でエラーを処理する場合は、CosmosBatchOperationError 例外を使用して、失敗した操作とそのインデックスを特定します。

try:
    batch_response = container.execute_item_batch(batch, partition_key=partition_key)
except exceptions.CosmosBatchOperationError as e:
    error_operation_index = e.error_index
    error_operation_response = e.operation_responses[error_operation_index]
    error_operation = batch[error_operation_index]
    print("\nError operation: {}, error operation response: {}\n".format(error_operation, error_operation_response))

JavaScript SDK では、container.items.batch メソッドを使用してバッチ操作を実行します。 各操作には、型 (operationType) と操作のデータを含める必要があります。

バッチ操作の結果を反復処理して、各操作の状態と出力を調べることができます。

response.result.forEach((operationResult, index) => {
    const { statusCode, requestCharge, resourceBody } = operationResult;
    console.log(`Operation ${index + 1}: Status code: ${statusCode}, Request charge: ${requestCharge}, Resource: ${JSON.stringify(resourceBody)}`);
});

バッチ応答オブジェクトの get メソッドを使用して、個々の操作の状態コード、応答本文、および要求料金を確認できます。

// Retrieve the results of the first item in the batch:
const first_item_result = response.result[0];
const first_item_status_code = first_item_result.statusCode;
const first_item_request_charge = first_item_result.requestCharge;
const first_item_resource = first_item_result.resourceBody;
console.log(`Operation 1: Status code: ${first_item_status_code}, Request charge: ${first_item_request_charge}, Resource: ${JSON.stringify(first_item_resource)}`);

// Retrieve the results of the second item in the batch:
const second_item_result = response.result[1];
const second_item_status_code = second_item_result.statusCode;
const second_item_request_charge = second_item_result.requestCharge;
const second_item_resource = second_item_result.resourceBody;
console.log(`Operation 2: Status code: ${second_item_status_code}, Request charge: ${second_item_request_charge}, Resource: ${JSON.stringify(second_item_resource)}`);