Các bước sử dụng tính năng xử lý hàng loạt như sau:
Tạo một công việc theo lô mới
Để tạo tài nguyên BatchJob, hãy gọi MutateBatchJob. Khi tạo đối tượng BatchJob, bạn có thể tuỳ ý đặt metadata.execution_limit_seconds thành một số nguyên dương để định cấu hình giới hạn trên gần đúng (tính bằng giây) về thời gian công việc có thể chạy trước khi tự động bị huỷ (xem phần Hướng dẫn sử dụng bổ sung).
Java
private String createBatchJob(BatchJobServiceClient batchJobServiceClient, long customerId) { BatchJobOperation operation = BatchJobOperation.newBuilder().setCreate(BatchJob.newBuilder().build()).build(); String batchJobResourceName = batchJobServiceClient .mutateBatchJob(Long.toString(customerId), operation) .getResult() .getResourceName(); System.out.printf("Created a mutate job with resource name: '%s'.%n", batchJobResourceName); return batchJobResourceName; }
C#
private static string CreateBatchJob(BatchJobServiceClient batchJobService, long customerId) { BatchJobOperation operation = new BatchJobOperation() { Create = new BatchJob() { } }; string batchJobResourceName = batchJobService.MutateBatchJob(customerId.ToString(), operation) .Result.ResourceName; Console.WriteLine($"Created a batch job with resource name: " + $"'{batchJobResourceName}'."); return batchJobResourceName; }
PHP
private static function createBatchJob( BatchJobServiceClient $batchJobServiceClient, int $customerId ): string { // Creates a batch job operation to create a new batch job. $batchJobOperation = new BatchJobOperation(); $batchJobOperation->setCreate(new BatchJob()); // Issues a request to the API and get the batch job's resource name. $batchJobResourceName = $batchJobServiceClient->mutateBatchJob( MutateBatchJobRequest::build($customerId, $batchJobOperation) )->getResult()->getResourceName(); printf( "Created a batch job with resource name: '%s'.%s", $batchJobResourceName, PHP_EOL ); return $batchJobResourceName; }
Python
def create_batch_job( batch_job_service: BatchJobServiceClient, customer_id: str, batch_job_operation: BatchJobOperation, ) -> str: """Creates a batch job for the specified customer ID. Args: batch_job_service: an instance of the BatchJobService message class. customer_id: a str of a customer ID. batch_job_operation: a BatchJobOperation instance set to "create" Returns: a str of a resource name for a batch job. """ try: response: MutateBatchJobResponse = batch_job_service.mutate_batch_job( customer_id=customer_id, operation=batch_job_operation ) resource_name: str = response.result.resource_name print(f'Created a batch job with resource name "{resource_name}"') return resource_name except GoogleAdsException as exception: handle_googleads_exception(exception) # This line will likely not be reached due to sys.exit(1) in handle_googleads_exception # but to satisfy the type checker, we add a return statement. return "" # Or raise an exception
Ruby
def create_batch_job(client, batch_job_service, customer_id) # Creates a batch job operation to create a new batch job. operation = client.operation.create_resource.batch_job # Issues a request to the API and get the batch job's resource name. response = batch_job_service.mutate_batch_job( customer_id: customer_id, operation: operation ) batch_job_resource_name = response.result.resource_name puts "Created a batch job with resource name: '#{batch_job_resource_name}'" batch_job_resource_name end
Perl
sub create_batch_job { my ($batch_job_service, $customer_id) = @_; # Create a batch job operation. my $batch_job_operation = Google::Ads::GoogleAds::V26::Services::BatchJobService::BatchJobOperation-> new({create => Google::Ads::GoogleAds::V26::Resources::BatchJob->new({})}); my $batch_job_resource_name = $batch_job_service->mutate({ customerId => $customer_id, operation => $batch_job_operation })->{result}{resourceName}; printf "Created a batch job with resource name: '%s'.\n", $batch_job_resource_name; return $batch_job_resource_name; }
curl
# Creates a batch job. # # Variables: # API_VERSION, # CUSTOMER_ID, # MANAGER_CUSTOMER_ID, # OAUTH2_ACCESS_TOKEN: # See https://developers.google.com/google-ads/api/rest/auth#request_headers # for details. curl -f --request POST \ "https://googleads.googleapis.com/v${API_VERSION}/customers/${CUSTOMER_ID}/batchJobs:mutate" \ --header "Content-Type: application/json" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \ --data @- <<EOF { "operation": { "create": {} } } EOF
Tại thời điểm này trong quy trình, status của công việc là PENDING.
Thêm các thao tác biến đổi vào công việc theo lô
Thêm một hoặc nhiều đối tượng MutateOperation vào công việc theo lô đã tạo ở bước trước bằng cách gọi AddBatchJobOperations. Sau đó, phản hồi sẽ chứa những thông tin sau:
total_operations: Tổng số thao tác đã thêm cho đến nay cho công việc này.next_sequence_token: Mã thông báo trình tự cần truyền vào trườngsequence_tokenkhi bạn gọi lại phương thức này để thêm các thao tác khác.
Yêu cầu AddBatchJobOperations đầu tiên cho một công việc theo lô phải bỏ qua sequence_token. Khi bạn gọi lại AddBatchJobOperations để thêm các thao tác khác, hãy chỉ định mã thông báo trình tự đã nhận được trước đó trong trường sequence_token của yêu cầu. Nếu bạn gọi phương thức này bằng một danh sách mutate_operations trống, thì hệ thống sẽ trả về lỗi BatchJobError.EMPTY_OPERATIONS. Nếu bạn gọi phương thức này bằng bất kỳ mã thông báo trình tự nào khác với mã thông báo đã nhận được trước đó, thì hệ thống sẽ trả về lỗi BatchJobError.INVALID_SEQUENCE_TOKEN. Nếu bạn cố gắng thêm các thao tác sau khi công việc bắt đầu chạy, thì hệ thống sẽ trả về lỗi BatchJobError.CANNOT_MODIFY_JOB_AFTER_JOB_STARTS_RUNNING.
Mặc dù status của công việc theo lô là PENDING, nhưng sequence_token cũng có sẵn dưới dạng next_add_sequence_token trên tài nguyên BatchJob mà bạn có thể truy xuất sau.
Nếu đang tạo các đối tượng phụ thuộc, chẳng hạn như một chiến dịch hoàn chỉnh bao gồm một chiến dịch mới và các nhóm quảng cáo, quảng cáo và từ khoá tương ứng, thì bạn có thể sử dụng mã tạm thời để chỉ định tên tài nguyên.
Java
private void addAllBatchJobOperations( BatchJobServiceClient batchJobServiceClient, long customerId, String batchJobResourceName) { AddBatchJobOperationsResponse response = batchJobServiceClient.addBatchJobOperations( AddBatchJobOperationsRequest.newBuilder() .setResourceName(batchJobResourceName) .addAllMutateOperations(buildAllOperations(customerId)) .build()); System.out.printf( "%d mutate operations have been added so far.%n", response.getTotalOperations()); // You can use this next sequence token for calling addBatchJobOperations() next time. System.out.printf( "Next sequence token for adding next operations is '%s'.%n", response.getNextSequenceToken()); }
C#
private static void AddAllBatchJobOperations(BatchJobServiceClient batchJobService, long customerId, string batchJobResourceName) { AddBatchJobOperationsResponse response = batchJobService.AddBatchJobOperations( new AddBatchJobOperationsRequest() { ResourceName = batchJobResourceName, MutateOperations = { BuildAllOperations(customerId) } }); Console.WriteLine($"{response.TotalOperations} mutate operations have been added" + $" so far."); // You can use this next sequence token for calling AddBatchJobOperations() next time. Console.WriteLine($"Next sequence token for adding next operations is " + $"'{response.NextSequenceToken}'."); }
PHP
private static function addAllBatchJobOperations( BatchJobServiceClient $batchJobServiceClient, int $customerId, string $batchJobResourceName ): void { $response = $batchJobServiceClient->addBatchJobOperations( AddBatchJobOperationsRequest::build( $batchJobResourceName, '', self::buildAllOperations($customerId) ) ); printf( "%d mutate operations have been added so far.%s", $response->getTotalOperations(), PHP_EOL ); // You can use this next sequence token for calling addBatchJobOperations() next time. printf( "Next sequence token for adding next operations is '%s'.%s", $response->getNextSequenceToken(), PHP_EOL ); }
Python
def add_all_batch_job_operations( batch_job_service: BatchJobServiceClient, operations: List[MutateOperation], resource_name: str, ) -> None: """Adds all mutate operations to the batch job. As this is the first time for this batch job, we pass null as a sequence token. The response will contain the next sequence token that we can use to upload more operations in the future. Args: batch_job_service: an instance of the BatchJobService message class. operations: a list of a mutate operations. resource_name: a str of a resource name for a batch job. """ try: response: AddBatchJobOperationsResponse = ( batch_job_service.add_batch_job_operations( resource_name=resource_name, sequence_token=None, # type: ignore mutate_operations=operations, ) ) print( f"{response.total_operations} mutate operations have been " "added so far." ) # You can use this next sequence token for calling # add_batch_job_operations() next time. print( "Next sequence token for adding next operations is " f"{response.next_sequence_token}" ) except GoogleAdsException as exception: handle_googleads_exception(exception)
Ruby
def add_all_batch_job_operations( client, batch_job_service, customer_id, batch_job_resource_name) response = batch_job_service.add_batch_job_operations( resource_name: batch_job_resource_name, mutate_operations: build_all_operations(client, customer_id), ) puts "#{response.total_operations} mutate operations have been added so far." # You can use this next sequence token for calling # add_all_batch_job_operations() next time puts "Next sequence token for adding next operations is " \ "'#{response.next_sequence_token}'" end
Perl
sub add_all_batch_job_operations { my ($batch_job_service, $customer_id, $batch_job_resource_name) = @_; my $add_batch_job_operations_response = $batch_job_service->add_operations({ resourceName => $batch_job_resource_name, sequenceToken => undef, mutateOperations => build_all_operations($customer_id)}); printf "%d batch operations have been added so far.\n", $add_batch_job_operations_response->{totalOperations}; # You can use this next sequence token for calling add_operations() next time. printf "Next sequence token for adding next operations is '%s'.\n", $add_batch_job_operations_response->{nextSequenceToken}; }
curl
# Adds operations to a batch job. # # Variables: # API_VERSION, # CUSTOMER_ID, # MANAGER_CUSTOMER_ID, # OAUTH2_ACCESS_TOKEN: # See https://developers.google.com/google-ads/api/rest/auth#request_headers # for details. # BATCH_JOB_RESOURCE_NAME: # The resource name of the batch job to which the operations should be added # as returned by the previous step. curl -f --request POST \ "https://googleads.googleapis.com/v${API_VERSION}/${BATCH_JOB_RESOURCE_NAME}:addOperations" \ --header "Content-Type: application/json" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \ --data @- <<EOF { "mutateOperations": [ { "campaignBudgetOperation": { "create": { "resourceName": "customers/${CUSTOMER_ID}/campaignBudgets/-1", "name": "batch job budget #${RANDOM}", "deliveryMethod": "STANDARD", "amountMicros": 5000000 } } }, { "campaignOperation": { "create": { "advertisingChannelType": "SEARCH", "status": "PAUSED", "name": "batch job campaign #${RANDOM}", "campaignBudget": "customers/${CUSTOMER_ID}/campaignBudgets/-1", "resourceName": "customers/${CUSTOMER_ID}/campaigns/-2", "manualCpc": { } } }, } ] } EOF
Nhấp để xem nội dung của hàm thao tác bản dựng trong GitHub cho thư viện ứng dụng của bạn:
Java
buildAllOperations()
C#
BuildAllOperations()
PHP
buildAllOperations()
Python
build_all_operations()
Ruby
build_all_operations()
Perl
build_all_operations
Chạy công việc theo lô
Sau khi thêm tất cả các thao tác, bạn có thể yêu cầu Google Ads API chạy công việc theo lô bằng cách gọi RunBatchJob cho các thao tác đã tải lên.
Việc gọi RunBatchJob sau khi công việc đã bắt đầu chạy sẽ trả về lỗi BatchJobError.CANNOT_MODIFY_JOB_AFTER_JOB_STARTS_RUNNING.
Java
private OperationFuture runBatchJob( BatchJobServiceClient batchJobServiceClient, String batchJobResourceName) { OperationFuture operationResponse = batchJobServiceClient.runBatchJobAsync(batchJobResourceName); // BEWARE! The above call returns an OperationFuture. The execution of that future depends on // the thread pool which is owned by batchJobServiceClient. If you use this future, you *must* // keep the service client in scope too. // See https://developers.google.com/google-ads/api/docs/client-libs/java/lro for more detail. System.out.printf( "Mutate job with resource name '%s' has been executed.%n", batchJobResourceName); return operationResponse; }
C#
private Operation<Empty, BatchJobMetadata> RunBatchJob( BatchJobServiceClient batchJobService, string batchJobResourceName) { Operation<Empty, BatchJobMetadata> operationResponse = batchJobService.RunBatchJob(batchJobResourceName); Console.WriteLine($"Batch job with resource name '{batchJobResourceName}' has been " + $"executed."); return operationResponse; }
PHP
private static function runBatchJob( BatchJobServiceClient $batchJobServiceClient, string $batchJobResourceName ): OperationResponse { $operationResponse = $batchJobServiceClient->runBatchJob(RunBatchJobRequest::build($batchJobResourceName)); printf( "Batch job with resource name '%s' has been executed.%s", $batchJobResourceName, PHP_EOL ); return $operationResponse; }
Python
def run_batch_job( batch_job_service: BatchJobServiceClient, resource_name: str ) -> Operation: """Runs the batch job for executing all uploaded mutate operations. Args: batch_job_service: an instance of the BatchJobService message class. resource_name: a str of a resource name for a batch job. Returns: a google.api_core.operation.Operation instance. """ try: response: Operation = batch_job_service.run_batch_job( resource_name=resource_name ) print( f'Batch job with resource name "{resource_name}" has been ' "executed." ) return response except GoogleAdsException as exception: handle_googleads_exception(exception) # This line will likely not be reached due to sys.exit(1) in handle_googleads_exception # but to satisfy the type checker, we add a return statement. # In a real application, you might want to return a dummy Operation or raise an error. return Operation( op_type_name="type.googleapis.com/google.protobuf.Empty", complete=True, done_callbacks=[], metadata_type=None, result_type=None, ) # type: ignore
Ruby
def run_batch_job(batch_job_service, batch_job_resource_name) operation_response = batch_job_service.run_batch_job( resource_name: batch_job_resource_name, ) puts "Batch job with resource name '#{batch_job_resource_name}' " \ "has been executed." operation_response end
Perl
sub run_batch_job { my ($batch_job_service, $batch_job_resource_name) = @_; my $batch_job_lro = $batch_job_service->run({resourceName => $batch_job_resource_name}); printf "Batch job with resource name '%s' has been executed.\n", $batch_job_resource_name; return $batch_job_lro; }
curl
# Runs a batch job. # # Variables: # API_VERSION, # CUSTOMER_ID, # MANAGER_CUSTOMER_ID, # OAUTH2_ACCESS_TOKEN: # See https://developers.google.com/google-ads/api/rest/auth#request_headers # for details. # BATCH_JOB_RESOURCE_NAME: # The resource name of the batch job to run as returned by the previous step. curl -f --request POST \ "https://googleads.googleapis.com/v${API_VERSION}/${BATCH_JOB_RESOURCE_NAME}:run" \ --header "Content-Type: application/json" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \ --data @- <<EOF {} EOF
Phản hồi được trả về là một đối tượng Operation chạy trong thời gian dài, chứa siêu dữ liệu của công việc theo lô và thông tin về trạng thái công việc.
Thăm dò trạng thái của công việc theo lô cho đến khi hoàn tất
Thăm dò trạng thái của công việc theo lô bằng phương thức Operations.GetOperation trên dịch vụ google.longrunning.Operations (tách biệt với BatchJobService) cho đến khi trường Operation.done là true. Trong Google Ads API,
Operations.GetOperation là phương thức duy nhất được hỗ trợ trên
google.longrunning.Operations (WaitOperation,
ListOperations, CancelOperation và DeleteOperation trả về
UNIMPLEMENTED):
- Thư viện ứng dụng: Gọi trình trợ giúp thăm dò ý kiến hoặc chờ trên trình bao bọc thao tác chạy trong thời gian dài do
RunBatchJobtrả về (hoặc truyền tên thao tác đếnOperationService::poll_until_donetrong thư viện ứng dụng Perl dựa trên REST), thăm dò ý kiếnOperations.GetOperationcho đến khi thao tác hoàn tất (hoặc cho đến khi hết thời gian chờ thăm dò ý kiến đã định cấu hình; trong Perl, hãy xác minh rằngdonelàtruetrên hàm băm được trả về trước khi liệt kê kết quả). Trong Java, hãy luôn mởBatchJobServiceClientcho đến khiOperationFuturehoàn tất. - REST: Gửi yêu cầu HTTP
GETđếnhttps://googleads.googleapis.com/v26/OPERATION_NAME(trong đóOPERATION_NAMElà tên tài nguyênOperation.name, được định dạng làcustomers/CUSTOMER_ID/operations/OPERATION_ID) với thời gian đợi luỹ thừa cho đến khi trườngOperation.donelàtrue.
Java
private void pollBatchJob(OperationFuture operationResponse) { try { operationResponse.get(MAX_TOTAL_POLL_INTERVAL_SECONDS, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { System.err.printf("Failed polling the mutate job. Exception: %s%n", e); System.exit(1); } }
C#
private static void PollBatchJob(Operation<Empty, BatchJobMetadata> operationResponse) { PollSettings pollSettings = new PollSettings( Expiration.FromTimeout(TimeSpan.FromSeconds(MAX_TOTAL_POLL_INTERVAL_SECONDS)), TimeSpan.FromSeconds(1)); operationResponse.PollUntilCompleted(pollSettings); }
PHP
private static function pollBatchJob(OperationResponse $operationResponse): void { $operationResponse->pollUntilComplete( [ 'initialPollDelayMillis' => self::POLL_FREQUENCY_SECONDS * 1000, 'totalPollTimeoutMillis' => self::MAX_TOTAL_POLL_INTERVAL_SECONDS * 1000 ] ); }
Python
def poll_batch_job( operations_response: Operation, event: asyncio.Event ) -> None: """Polls the server until the batch job execution finishes. Sets the initial poll delay time and the total time to wait before time-out. Args: operations_response: a google.api_core.operation.Operation instance. event: an instance of asyncio.Event to invoke once the operations have completed, alerting the awaiting calling code that it can proceed. """ loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() def done_callback(future: Coroutine[Any, Any, Any]) -> None: # The operations_response object will call callbacks from a daemon # thread so we must use a threadsafe method of setting the event here # otherwise it will not trigger the awaiting code. loop.call_soon_threadsafe(event.set) # operations_response represents a Long-Running Operation or LRO. The class # provides an interface for polling the API to check when the operation is # complete. Below we use the asynchronous interface, but there's also a # synchronous interface that uses the Operation.result method. # See: https://googleapis.dev/python/google-api-core/latest/operation.html operations_response.add_done_callback(done_callback) # type: ignore
Ruby
def poll_batch_job(operation_response) operation_response.wait_until_done! end
Perl
sub poll_batch_job { my ($operation_service, $batch_job_lro) = @_; $operation_service->poll_until_done({ name => $batch_job_lro->{name}, pollFrequencySeconds => POLL_FREQUENCY_SECONDS, pollTimeoutSeconds => POLL_TIMEOUT_SECONDS }); }
curl
# Gets the status of a batch job. # # Variables: # API_VERSION, # CUSTOMER_ID, # MANAGER_CUSTOMER_ID, # OAUTH2_ACCESS_TOKEN: # See https://developers.google.com/google-ads/api/rest/auth#request_headers # for details. # BATCH_JOB_OPERATION_NAME: # The operation name of the running batch job as returned by the previous # step. curl -f --request GET \ "https://googleads.googleapis.com/v${API_VERSION}/${BATCH_JOB_OPERATION_NAME}" \ --header "Content-Type: application/json" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \
Ngoài ra, hãy truy vấn tài nguyên batch_job bằng phương thức GoogleAdsService.Search hoặc GoogleAdsService.SearchStream, chọn batch_job.status và các trường con BatchJobMetadata cụ thể (chẳng hạn như batch_job.metadata.estimated_completion_ratio và batch_job.metadata.executed_operation_count) cho đến khi batch_job.status là DONE. Mỗi yêu cầu Operations.GetOperation hoặc yêu cầu GAQL không được phân trang được tính là một thao tác đối với hạn mức API hằng ngày; khi bạn theo dõi nhiều công việc theo lô đồng thời, một truy vấn GAQL (WHERE batch_job.id IN
(...) hoặc WHERE batch_job.status = 'RUNNING') sẽ kiểm tra tất cả các công việc đó trong một yêu cầu thay vì thăm dò từng thao tác chạy trong thời gian dài.
Khi Operation.done là true, thao tác chạy trong thời gian dài sẽ trả về một phản hồi trống mà không có lỗi theo từng thao tác. Hãy gọi ListBatchJobResults để kiểm tra kết quả của từng thao tác.
Truy xuất một thao tác diễn ra trong thời gian dài hiện có
Nếu ứng dụng của bạn thăm dò ý kiến về công việc theo lô từ một quy trình riêng biệt hoặc cần tiếp tục thăm dò ý kiến sau khi khởi động lại, thì bạn có thể truy xuất tên thao tác chạy trong thời gian dài bằng cách sử dụng GoogleAdsService.Search hoặc GoogleAdsService.SearchStream:
Truy vấn tài nguyên
batch_jobđể đọc trườnglong_running_operation:SELECT batch_job.id, batch_job.status, batch_job.long_running_operation FROM batch_job WHERE batch_job.id = BATCH_JOB_IDThay thế
BATCH_JOB_IDbằng mã nhận dạng dạng số của công việc theo lô.Xác minh rằng
batch_job.statuslàRUNNINGhoặcDONE(long_running_operationchưa được đặt trong khi công việc làPENDINGvì không có thao tác chạy trong thời gian dài cho đến khiRunBatchJobđược gọi) và đọc chuỗilong_running_operationtừ đối tượngBatchJobđược trả về.Thăm dò ý kiến hoặc kiểm tra thao tác bằng cách sử dụng chuỗi tên thao tác đó:
- Thư viện ứng dụng: Lấy ứng dụng thao tác từ ứng dụng
BatchJobService(getOperationsClient()trong Java và PHP,RunBatchJobOperationsClienttrong C#,transport.operations_clienttrong Python,operations_clienttrong Ruby hoặc$api_client->OperationService()trong Perl) để thông tin xác thực Google Ads và tiêu đềlogin-customer-idđược định cấu hình tự động, thay vì tạo thực thể cho mộtOperationsClientđộc lập. GọiOperations.GetOperation(hoặcOperationService::gettrong Perl) để kiểm tra một trạng thái hoặc sử dụng tên thao tác để tiếp tục trình bao bọc thao tác chạy trong thời gian dài và thăm dò ý kiến cho đến khi hoàn tất (runBatchJobOperationCallable().resumeFutureCalltrong Java,PollOnceRunBatchJobtrong C#,resumeOperationtrong PHP,google.api_core.operation.from_gapictrênOperationdoget_operationtrả về trong Python,wait_until_done!trênGapic::Operationdoget_operationtrả về trong Ruby hoặcnametrongOperationService::poll_until_donetrong Perl). - REST: Gửi yêu cầu HTTP
GETđếnhttps://googleads.googleapis.com/v26/OPERATION_NAME.
Vì
OPERATION_NAMEmã hoá phiên bản lớn của API màBatchJobđược tạo, nênOperations.GetOperation(ngay cả thông qua điểm cuối RESTv26đang hoạt động) sẽ không thành công và gặp lỗiRequestError.UNSUPPORTED_VERSIONnếu phiên bản đó đã ngừng hoạt động. Thay vào đó, hãy truy vấn trực tiếpbatch_job.statusbằngGoogleAdsService.- Thư viện ứng dụng: Lấy ứng dụng thao tác từ ứng dụng
Tìm hiểu về siêu dữ liệu của hoạt động
Khi bạn thăm dò ý kiến về hoạt động chạy trong thời gian dài hoặc truy vấn tài nguyên batch_job, BatchJobMetadata sẽ cung cấp thông tin chi tiết về tiến trình của công việc theo lô:
creation_date_time: Dấu thời gian khi công việc theo lô được tạo.start_date_time: Dấu thời gian khi công việc theo lô bắt đầu chạy (được điền sẵn khi bạn truy vấnbatch_job.metadata.start_date_timebằngGoogleAdsService; bị bỏ qua khỏiOperation.metadatatrongRunBatchJobvàOperations.GetOperation).completion_date_time: Dấu thời gian khi công việc theo lô kết thúc (chỉ xuất hiện nếu công việc đã hoàn tất).estimated_completion_ratio: Giá trị ước tính về tỷ lệ phần trăm của công việc (từ 0 đến 1) đã hoàn thành.operation_count: Tổng số thao tác trong công việc theo lô.executed_operation_count: Số lượng thao tác đã được thực thi cho đến nay.execution_limit_seconds: Giới hạn trên gần đúng (tính bằng giây) cho thời gian chạy công việc theo lô trước khi bị huỷ (được điền sẵn khi bạn truy vấnbatch_job.metadata.execution_limit_secondsbằngGoogleAdsServicenếu bạn đã đặt giới hạn dương khi tạo công việc; bị bỏ qua khỏiOperation.metadatatrongRunBatchJobvàOperations.GetOperation).
Các trường này, đặc biệt là estimated_completion_ratio và số lượng thao tác, có thể giúp bạn đánh giá tiến trình của công việc, ước tính thời gian còn lại và điều chỉnh tần suất thăm dò ý kiến. Ví dụ: bạn có thể thăm dò ý kiến ít thường xuyên hơn khi estimated_completion_ratio thấp và thường xuyên hơn khi giá trị này gần bằng 1.
Liệt kê tất cả kết quả của công việc theo lô
Khi công việc theo lô kết thúc, hãy gọi ListBatchJobResults để truy xuất trạng thái và phản hồi cho từng thao tác trong công việc:
Java
private void fetchAndPrintResults( BatchJobServiceClient batchJobServiceClient, String batchJobResourceName) { System.out.printf( "Mutate job with resource name '%s' has finished. Now, printing its results...%n", batchJobResourceName); // Gets all the results from running mutate job and prints their information. ListBatchJobResultsPagedResponse batchJobResults = batchJobServiceClient.listBatchJobResults( ListBatchJobResultsRequest.newBuilder() .setResourceName(batchJobResourceName) .setPageSize(PAGE_SIZE) .build()); for (BatchJobResult batchJobResult : batchJobResults.iterateAll()) { System.out.printf( "Mutate job #%d has a status '%s' and response of type '%s'.%n", batchJobResult.getOperationIndex(), batchJobResult.getStatus().getMessage().isEmpty() ? "N/A" : batchJobResult.getStatus().getMessage(), batchJobResult .getMutateOperationResponse() .getResponseCase() .equals(ResponseCase.RESPONSE_NOT_SET) ? "N/A" : batchJobResult.getMutateOperationResponse().getResponseCase()); } }
C#
private static void FetchAndPrintResults(BatchJobServiceClient batchJobService, string batchJobResourceName) { Console.WriteLine($"batch job with resource name '{batchJobResourceName}' has " + $"finished. Now, printing its results..."); ListBatchJobResultsRequest request = new ListBatchJobResultsRequest() { ResourceName = batchJobResourceName, PageSize = PAGE_SIZE, }; ListBatchJobResultsResponse resp = new ListBatchJobResultsResponse(); // Gets all the results from running batch job and prints their information. foreach (BatchJobResult batchJobResult in batchJobService.ListBatchJobResults(request)) { if (!batchJobResult.IsFailed) { Console.WriteLine($"batch job result #{batchJobResult.OperationIndex} is " + $"successful and response is of type " + $"'{batchJobResult.MutateOperationResponse.ResponseCase}'."); } else { Console.WriteLine($"batch job result #{batchJobResult.OperationIndex} " + $"failed with error message {batchJobResult.Status.Message}."); foreach (GoogleAdsError error in batchJobResult.Failure.Errors) { Console.WriteLine($"Error found: {error}."); } } } }
PHP
private static function fetchAndPrintResults( BatchJobServiceClient $batchJobServiceClient, string $batchJobResourceName ): void { printf( "Batch job with resource name '%s' has finished. Now, printing its results...%s", $batchJobResourceName, PHP_EOL ); // Gets all the results from running batch job and print their information. $batchJobResults = $batchJobServiceClient->listBatchJobResults( ListBatchJobResultsRequest::build($batchJobResourceName)->setPageSize(self::PAGE_SIZE) ); foreach ($batchJobResults->iterateAllElements() as $batchJobResult) { /** * @var BatchJobResult $batchJobResult */ printf( "Batch job #%d has a status '%s' and response of type '%s'.%s", $batchJobResult->getOperationIndex(), $batchJobResult->getStatus() ? $batchJobResult->getStatus()->getMessage() : 'N/A', $batchJobResult->getMutateOperationResponse() ? $batchJobResult->getMutateOperationResponse()->getResponse() : 'N/A', PHP_EOL ); } }
Python
def fetch_and_print_results( client: GoogleAdsClient, batch_job_service: BatchJobServiceClient, resource_name: str, ) -> None: """Prints all the results from running the batch job. Args: client: an initialized GoogleAdsClient instance. batch_job_service: an instance of the BatchJobService message class. resource_name: a str of a resource name for a batch job. """ print( f'Batch job with resource name "{resource_name}" has finished. ' "Now, printing its results..." ) list_results_request: ListBatchJobResultsRequest = client.get_type( "ListBatchJobResultsRequest" ) list_results_request.resource_name = resource_name list_results_request.page_size = 1000 # Gets all the results from running batch job and prints their information. batch_job_results: ListBatchJobResultsResponse = ( batch_job_service.list_batch_job_results(request=list_results_request) ) for batch_job_result in batch_job_results: status: str = batch_job_result.status.message status = status if status else "N/A" result: Any = batch_job_result.mutate_operation_response result = result or "N/A" print( f"Batch job #{batch_job_result.operation_index} " f'has a status "{status}" and response type "{result}"' )
Ruby
def fetch_and_print_results(batch_job_service, batch_job_resource_name) puts "Batch job with resource name '#{batch_job_resource_name}' has " \ "finished. Now, printing its results..." \ # Gets all the results from running batch job and print their information. batch_job_results = batch_job_service.list_batch_job_results( resource_name: batch_job_resource_name, page_size: PAGE_SIZE, ) batch_job_results.each do |result| puts "Batch job ##{result.operation_index} has a status " \ "#{result.status ? result.status.message : 'N/A'} and response of type " \ "#{result.mutate_operation_response ? result.mutate_operation_response.response : 'N/A'}" end end
Perl
sub fetch_and_print_results { my ($batch_job_service, $batch_job_resource_name) = @_; printf "Batch job with resource name '%s' has finished. " . "Now, printing its results...\n", $batch_job_resource_name; # Get all the results from running batch job and print their information. my $list_batch_job_results_response = $batch_job_service->list_results({ resourceName => $batch_job_resource_name, pageSize => PAGE_SIZE }); foreach my $batch_job_result (@{$list_batch_job_results_response->{results}}) { printf "Batch job #%d has a status '%s' and response of type '%s'.\n", $batch_job_result->{operationIndex}, $batch_job_result->{status} ? $batch_job_result->{status}{message} : "N/A", $batch_job_result->{mutateOperationResponse} ? [keys %{$batch_job_result->{mutateOperationResponse}}]->[0] : "N/A"; } }
curl
# Gets the results of a batch job. # # Variables: # API_VERSION, # CUSTOMER_ID, # MANAGER_CUSTOMER_ID, # OAUTH2_ACCESS_TOKEN: # See https://developers.google.com/google-ads/api/rest/auth#request_headers # for details. # BATCH_JOB_RESOURCE_NAME: # The operation name of the running batch job as returned by the previous # step. curl -f --request GET \ "https://googleads.googleapis.com/v${API_VERSION}/${BATCH_JOB_RESOURCE_NAME}:listResults?pageSize=1000" \ --header "Content-Type: application/json" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}"
Trong ListBatchJobResultsRequest, trường page_size sẽ mặc định là giá trị tối đa của 1000 nếu bị bỏ qua hoặc được đặt thành 0. Nếu bạn đặt page_size thành một giá trị lớn hơn 1000 hoặc nhỏ hơn 0, thì hàm sẽ trả về lỗi BatchJobError.INVALID_PAGE_SIZE.
Mỗi BatchJobResult xác định thao tác tương ứng bằng cách sử dụng operation_index, chỉ mục dựa trên 0 của MutateOperation đã tải lên trong công việc theo lô.
- Thao tác thành công:
mutate_operation_responseđược điền sẵn (responsechứaresource_namecủa tài nguyên đã thay đổi, cùng với tất cả các trường có thể thay đổi nếuresponse_content_typeđược đặt thànhMUTABLE_RESOURCE) vàstatuskhông được đặt. - Thao tác không thành công (hoặc chưa thực hiện):
mutate_operation_responsekhông được đặt vàstatuschứa thông tin chi tiết về lỗi cho thao tác.
Huỷ hoặc xoá một công việc theo lô
Cách bạn dừng hoặc huỷ một công việc theo lô phụ thuộc vào việc RunBatchJob đã được gọi hay chưa:
- Trước khi gọi
RunBatchJob: Khistatuscủa công việc làPENDING, bạn có thể loại bỏ công việc theo lô bằng cách gọiMutateBatchJobvớiBatchJobOperation.remove. Nếu bạn gọiremovesau khi công việc bắt đầu chạy, thì hệ thống sẽ trả về lỗiBatchJobError.CAN_ONLY_REMOVE_PENDING_JOB. - Sau khi gọi
RunBatchJob: Bạn không thể huỷ một công việc theo lô đang chạy theo cách thủ công (Operations.CancelOperationkhông được triển khai trong Google Ads API và trả vềUNIMPLEMENTED). Để giới hạn thời gian thực thi sao cho công việc tự động huỷ nếu chạy quá lâu, hãy đặtmetadata.execution_limit_secondsthành một số nguyên dương (giới hạn trên gần đúng) khi bạn tạo công việc trongMutateBatchJob. Vì các thao tác chuẩn trong một công việc theo lô thực thi khi bật chế độ lỗi một phần (các thao tác trong lô phụ nguyên tử được khôi phục cùng nhau), nên tính năng tự động huỷ sẽ không khôi phục các thao tác đã hoàn tất. Sau khistatuscủa một công việc bị huỷ (hoặc bị huỷ bỏ) chuyển sangDONE, việc gọiListBatchJobResultssẽ trả về các mụcBatchJobResultcho tiền tố đã thực thi (operation_index<executed_operation_count) cùng với kết quả thực thi riêng lẻ, theo sau là các mụcBatchJobResultcho tất cả các thao tác chưa thực thi còn lại (operation_indextừexecuted_operation_countđếnoperation_count- 1) vớimutate_operation_responsechưa đặt vàstatusđược đặt thànhABORTEDvớiInternalError.INTERNAL_ERRORkhi bị huỷ doexecution_limit_seconds(hoặc thành trạng thái lỗi ở cấp công việc nếu công việc bị huỷ bỏ do lỗi thực thi nghiêm trọng).
Xử lý lỗi
BatchJobService thực thi các thao tác chuẩn khi bật chế độ lỗi một phần (các thao tác thành công sẽ được thực hiện ngay cả khi các thao tác khác trong cùng một công việc không thành công, ngoại trừ trong các lô phụ nguyên tử) và tự động thử lại các thao tác không thành công do lỗi tạm thời. Tuy nhiên, không phải trường hợp lỗi nào cũng có thể tránh được. Bạn có thể khắc phục những thao tác không thành công do lỗi xác thực và gửi lại trong một công việc theo lô mới. Tương tự, khi một công việc theo lô
bị huỷ hoặc bị huỷ bỏ trước khi hoàn tất tất cả các thao tác, bạn có thể
gửi lại các thao tác chưa được thực thi (operation_index
>= executed_operation_count) cùng với mọi
thao tác không thành công từ tiền tố đã thực thi trong một công việc theo lô mới.