बैच प्रोसेसिंग का इस्तेमाल करने का तरीका यहां बताया गया है:
नया बैच जॉब बनाना
BatchJob संसाधन बनाने के लिए, MutateBatchJob को कॉल करें. BatchJob ऑब्जेक्ट बनाते समय, आपके पास metadata.execution_limit_seconds को सेट करने का विकल्प होता है. इससे, सेकंड में एक ऊपरी सीमा कॉन्फ़िगर की जा सकती है. इससे यह तय होता है कि कोई जॉब अपने-आप रद्द होने से पहले, कितनी देर तक चल सकता है. ज़्यादा जानकारी के लिए, इस्तेमाल करने के बारे में अतिरिक्त दिशा-निर्देश देखें.
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::V25::Services::BatchJobService::BatchJobOperation-> new({create => Google::Ads::GoogleAds::V25::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, # DEVELOPER_TOKEN, # 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 "developer-token: ${DEVELOPER_TOKEN}" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \ --data @- <<EOF { "operation": { "create": {} } } EOF
इस प्रोसेस के इस चरण में, नौकरी का status PENDING है.
बैच जॉब में म्यूटेट ऑपरेशन जोड़ना
AddBatchJobOperations को कॉल करके, पिछले चरण में बनाए गए बैच जॉब में एक या उससे ज़्यादा MutateOperation ऑब्जेक्ट जोड़ें. इसके बाद, रिस्पॉन्स में ये शामिल होते हैं:
total_operations: इस जॉब के लिए अब तक जोड़े गए ऑपरेशन की कुल संख्या.next_sequence_token: ज़्यादा कार्रवाइयां जोड़ने के लिए, इस तरीके को फिर से कॉल करते समय,sequence_tokenफ़ील्ड में पास करने के लिए सीक्वेंस टोकन.
बैच जॉब के लिए पहले AddBatchJobOperations अनुरोध में sequence_token को शामिल नहीं किया जाना चाहिए. ज़्यादा कार्रवाइयां जोड़ने के लिए, AddBatchJobOperations को फिर से कॉल करते समय, अनुरोध के sequence_token फ़ील्ड में पहले से मिला क्रम टोकन डालें. खाली mutate_operations सूची के साथ इस तरीके को कॉल करने पर, BatchJobError.EMPTY_OPERATIONS गड़बड़ी दिखती है. साथ ही, पहले से मिले हुए किसी भी क्रम के टोकन के अलावा किसी अन्य टोकन के साथ इसे कॉल करने पर, BatchJobError.INVALID_SEQUENCE_TOKEN गड़बड़ी दिखती है. इसके अलावा, नौकरी शुरू होने के बाद कार्रवाइयां जोड़ने की कोशिश करने पर, BatchJobError.CANNOT_MODIFY_JOB_AFTER_JOB_STARTS_RUNNING गड़बड़ी दिखती है.
बैच जॉब के status PENDING होने पर, sequence_token को BatchJob संसाधन पर next_add_sequence_token के तौर पर भी उपलब्ध कराया जाता है. इसे बाद में फिर से पाया जा सकता है.
अगर आपको नए कैंपेन और उससे जुड़े विज्ञापन ग्रुप, विज्ञापन, और कीवर्ड वाला कोई पूरा कैंपेन जैसे डिपेंडेंट ऑब्जेक्ट बनाने हैं, तो संसाधन का नाम तय करने के लिए अस्थायी आईडी का इस्तेमाल किया जा सकता है.
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, # DEVELOPER_TOKEN, # 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 "developer-token: ${DEVELOPER_TOKEN}" \ --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
अपनी क्लाइंट लाइब्रेरी के लिए, GitHub में बिल्ड ऑपरेशन फ़ंक्शन का कॉन्टेंट देखने के लिए क्लिक करें:
Java
buildAllOperations()
C#
BuildAllOperations()
PHP
buildAllOperations()
Python
build_all_operations()
Ruby
build_all_operations()
Perl
build_all_operations
बैच जॉब चलाना
सभी कार्रवाइयां जोड़ने के बाद, Google Ads API से बैच जॉब चलाने का अनुरोध किया जा सकता है. इसके लिए, अपलोड की गई कार्रवाइयों पर RunBatchJob को कॉल करें.
जॉब शुरू होने के बाद, RunBatchJob को कॉल करने पर, 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, # DEVELOPER_TOKEN, # 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/v19/${BATCH_JOB_RESOURCE_NAME}:run" \ --header "Content-Type: application/json" \ --header "developer-token: ${DEVELOPER_TOKEN}" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \ --data @- <<EOF {} EOF
लौटाया गया रिस्पॉन्स, लंबे समय तक चलने वाली Operation (एलआरओ) ऑब्जेक्ट होता है. एलआरओ में, आपके बैच जॉब के मेटाडेटा के साथ-साथ जॉब के स्टेटस की जानकारी भी होती है.
बैच जॉब पूरा होने तक, उसके स्टेटस को पोल करें
अगले चरण में, एलआरओ के GetOperation का इस्तेमाल करके, बैच जॉब की स्थिति को पोल करना होगा. ऐसा तब तक करना होगा, जब तक एलआरओ की done वैल्यू 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, # DEVELOPER_TOKEN, # 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 "developer-token: ${DEVELOPER_TOKEN}" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \
ज़्यादा समय तक चलने वाली किसी मौजूदा कार्रवाई की जानकारी पाना
अगर आपका ऐप्लिकेशन, किसी अलग प्रोसेस से बैच जॉब को पोल करता है या उसे रीस्टार्ट करने के बाद पोलिंग फिर से शुरू करनी है, तो GoogleAdsService.Search या GoogleAdsService.SearchStream का इस्तेमाल करके, ज़्यादा समय तक चलने वाली कार्रवाई का नाम वापस पाया जा सकता है:
batch_jobसंसाधन की क्वेरी करके, उसकाlong_running_operationफ़ील्ड पढ़ें:SELECT batch_job.id, batch_job.status, batch_job.long_running_operation FROM batch_job WHERE batch_job.id = BATCH_JOB_IDपुष्टि करें कि
batch_job.status,RUNNINGयाDONEहै (long_running_operationको सिर्फ़ तब भरा जाता है, जबRunBatchJobको कॉल किया जाता है. साथ ही, जॉब केPENDINGहोने पर, इसे सेट नहीं किया जाता है). इसके बाद, दिखाए गएBatchJobऑब्जेक्ट सेlong_running_operationस्ट्रिंग को पढ़ें.Operationकी जांच करने या पोलिंग फिर से शुरू करने के लिए, अपनी क्लाइंट लाइब्रेरी केOperationsClientपर, उस ऑपरेशन के नाम की स्ट्रिंग कोGetOperationको पास करें.
ऑपरेशन मेटाडेटा को समझना
ज़्यादा समय तक चलने वाली कार्रवाई को पोल करने पर, Operation रिस्पॉन्स में मौजूद metadata फ़ील्ड, बैच जॉब की प्रोग्रेस के बारे में ज़्यादा जानकारी देता है. इस फ़ील्ड में BatchJobMetadata ऑब्जेक्ट होता है. इसके मुख्य सबफ़ील्ड में ये शामिल हैं:
creation_date_time: वह टाइमस्टैंप जब बैच जॉब बनाया गया था.start_date_time: वह टाइमस्टैंप जब बैच जॉब शुरू हुआ.completion_date_time: बैच जॉब पूरा होने का टाइमस्टैंप (यह सिर्फ़ तब मौजूद होता है, जब जॉब पूरा हो गया हो).estimated_completion_ratio: जॉब के पूरे हो चुके हिस्से (0.0 से 1.0 तक) का अनुमान.operation_count: बैच जॉब में मौजूद कार्रवाइयों की कुल संख्या.executed_operation_count: अब तक किए गए ऑपरेशन की संख्या.execution_limit_seconds: बैच जॉब के रद्द होने से पहले, उसके चलने की अनुमानित ऊपरी सीमा. यह सीमा सेकंड में होती है.
इन फ़ील्ड, खास तौर पर estimated_completion_ratio और ऑपरेशन की संख्या से, आपको जॉब की प्रोग्रेस का आकलन करने, बचे हुए समय का अनुमान लगाने, और पोलिंग की फ़्रीक्वेंसी में बदलाव करने में मदद मिल सकती है. उदाहरण के लिए, estimated_completion_ratio कम होने पर, हो सकता है कि आप कम बार पोल करें. वहीं, 1.0 के आस-पास होने पर, ज़्यादा बार पोल करें.
बैच जॉब के सभी नतीजों की सूची बनाना
जब आपके सभी बैच जॉब पूरे हो जाएं, तो उनके नतीजे देखने के लिए ListBatchJobResults का इस्तेमाल करें. साथ ही, उनके स्टेटस और रिस्पॉन्स प्रिंट करें:
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}"
ListBatchJobResultsRequest में, page_size फ़ील्ड की डिफ़ॉल्ट वैल्यू 1000 होती है. ऐसा तब होता है, जब इस फ़ील्ड को शामिल नहीं किया जाता या इसकी वैल्यू 0 पर सेट होती है. page_size को 1000 से ज़्यादा या 0 से कम वैल्यू पर सेट करने से, BatchJobError.INVALID_PAGE_SIZE गड़बड़ी दिखती है.
हर BatchJobResult, operation_index का इस्तेमाल करके, उससे जुड़े ऑपरेशन की पहचान करता है. यह बैच जॉब में अपलोड किए गए MutateOperation का 0-आधारित इंडेक्स होता है.
- कार्रवाई पूरी हुई:
mutate_operation_responseमें जानकारी भरी गई है (इसकेresponseमें, बदले गए संसाधन काresource_nameशामिल है. साथ ही, अगरresponse_content_typeकोMUTABLE_RESOURCEपर सेट किया गया है, तो इसमें बदलाव किए जा सकने वाले सभी फ़ील्ड भी शामिल हैं) औरstatusको सेट नहीं किया गया है. - कार्रवाई पूरी नहीं हुई (या लागू नहीं हुई):
mutate_operation_responseसेट नहीं है औरstatusमें कार्रवाई के लिए गड़बड़ी की जानकारी शामिल है.
बैच जॉब को रद्द करना या हटाना
बैच जॉब को रोकने या खारिज करने का तरीका इस बात पर निर्भर करता है कि RunBatchJob को कॉल किया गया है या नहीं:
RunBatchJobको कॉल करने से पहले: जब तक जॉब काstatusPENDINGहै, तब तकBatchJobOperation.removeके साथMutateBatchJobको कॉल करके, बैच जॉब को खारिज किया जा सकता है. जॉब शुरू होने के बाद,removeको कॉल करने पर,BatchJobError.CAN_ONLY_REMOVE_PENDING_JOBगड़बड़ी का मैसेज दिखता है.RunBatchJobको कॉल करने के बाद: अगर कोई ज़्यादा समय तक चलने वाली कार्रवाई पहले से मौजूद है, तो ज़्यादा समय तक चलने वाली कार्रवाई परCancelOperationको कॉल करके, चालू बैच जॉब को रद्द किया जा सकता है. बैच जॉब में स्टैंडर्ड ऑपरेशन, आंशिक गड़बड़ी चालू होने पर लागू होते हैं. ऐटॉमिक सब-बैच में मौजूद ऑपरेशन एक साथ रोल बैक होते हैं. इसलिए, बैच जॉब को रद्द करने से, वे ऑपरेशन रोल बैक नहीं होते जो रद्द करने से पहले ही पूरे हो चुके हैं. जब रद्द किए गए (या रोके गए) जॉब काstatusDONEमें बदल जाता है, तबListBatchJobResultsको कॉल करने पर,BatchJobResult, एक्ज़ीक्यूट किए गए प्रीफ़िक्स (operation_index<executed_operation_count) के लिए एंट्री दिखाता है. इसमें हर एंट्री के लिए, एक्ज़ीक्यूशन के नतीजे शामिल होते हैं. इसके बाद,BatchJobResult, एक्ज़ीक्यूट नहीं किए गए सभी बचे हुए ऑपरेशन (operation_indexexecuted_operation_countसे लेकरoperation_count- 1तक) के लिए एंट्री दिखाता है. इसमेंmutate_operation_responseको सेट नहीं किया जाता है औरstatusमें जॉब-लेवल की गड़बड़ी की स्थिति (याInternalError.INTERNAL_ERROR) भरी जाती है.
गड़बड़ी ठीक करना
BatchJobService स्टैंडर्ड ऑपरेशन को आंशिक तौर पर पूरा होने की सुविधा के साथ लागू करता है. इसका मतलब है कि अगर एक ही जॉब में मौजूद अन्य ऑपरेशन पूरे नहीं होते हैं, तब भी पूरे हो चुके ऑपरेशन कमिट हो जाते हैं. हालांकि, ऐसा ऐटॉमिक सब-बैच में नहीं होता. साथ ही, यह कुछ समय के लिए होने वाली गड़बड़ियों की वजह से पूरे न हो पाने वाले ऑपरेशन को अपने-आप फिर से पूरा करने की कोशिश करता है. हालांकि, ऐसा नहीं है कि गड़बड़ी की सभी स्थितियों से बचा जा सकता है. पुष्टि करने के दौरान होने वाली गड़बड़ियों की वजह से पूरे नहीं हो पाए कामों को ठीक किया जा सकता है. इसके बाद, उन्हें नए बैच जॉब में फिर से सबमिट किया जा सकता है. इसी तरह, जब कोई बैच जॉब सभी कार्रवाइयां पूरी होने से पहले रद्द या बंद हो जाता है, तो आपके पास उन कार्रवाइयों को फिर से सबमिट करने का विकल्प होता है जो पूरी नहीं हुई हैं (operation_index
>= executed_operation_count). साथ ही, उन कार्रवाइयों को भी फिर से सबमिट किया जा सकता है जो पूरी नहीं हो पाई हैं.