使用暫時 ID
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
BatchJobService
的強大功能在於支援使用暫時 ID。
如要這麼做,請指定新資源的 resource_name
,使用負數 ID。舉例來說,假設您建立廣告活動,並將其資源名稱指定為 customers/<YOUR_CUSTOMER_ID>/campaigns/-1
。您在後續作業中建立廣告群組時,可以透過該資源名稱參照廣告群組,系統會自動將您指定的 -1
替換為所建立廣告活動的實際 ID。
使用臨時資源名稱時,請注意下列事項:
- 臨時資源名稱只能在資源中定義後使用。在下列範例中,廣告群組作業必須出現在作業清單中的廣告活動作業之後。
- 系統不會在工作或變更要求中記住臨時資源名稱。
如要參照先前作業或變更要求中建立的資源,請使用該資源的實際名稱。
- 如果是單一工作或變動要求,即使臨時資源名稱來自不同資源類型,也必須使用不重複的負數。如果單一工作或變動要求重複使用臨時 ID,系統會傳回錯誤。
範例
假設您想在單一 API 要求中新增廣告活動、廣告群組和廣告,您會為要求建立類似下列結構的結構:
mutate_operations: [
{
campaign_operation: {
create: {
resource_name: "customers/<YOUR_CUSTOMER_ID>/campaigns/-1",
...
}
}
},
{
ad_group_operation: {
create: {
resource_name: "customers/<YOUR_CUSTOMER_ID>/adGroups/-2",
campaign: "customers/<YOUR_CUSTOMER_ID>/campaigns/-1"
...
}
}
},
{
ad_group_ad_operation: {
create: {
ad_group: "customers/<YOUR_CUSTOMER_ID>/adGroups/-2"
...
}
}
},
]
由於我們無法重複使用廣告活動的 -1
,因此廣告群組會使用新的臨時 ID。建立廣告群組廣告時,我們也會參考這個廣告群組。廣告群組本身會參照我們在要求中較早作業為廣告活動建立的資源名稱,而 resource_name
則不需要,因為沒有其他作業會參照該名稱。ad_group_ad_operation
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[null,null,["上次更新時間:2025-08-27 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eBatchJobService\u003c/code\u003e allows the use of temporary IDs (negative numbers) for new resources within a single job, enabling you to reference them in subsequent operations within the same request.\u003c/p\u003e\n"],["\u003cp\u003eTemporary resource names must be unique within a single job or mutate request and are not remembered across different jobs or requests; use actual resource names for referencing resources created earlier.\u003c/p\u003e\n"],["\u003cp\u003eResources with temporary IDs must be defined before being referenced by other operations within the same request, ensuring the order of operations reflects dependencies.\u003c/p\u003e\n"]]],[],null,["# Using temporary IDs\n\nA powerful feature of [`BatchJobService`](/google-ads/api/reference/rpc/v21/BatchJobService) is that\nit supports the use of temporary IDs.\n\nYou can do this by specifying the new resource's `resource_name` to use a\nnegative ID. For example, suppose you create a campaign and specify its resource name\nas `customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1`. When you create the\nad group in a later operation, you can then reference it by that resource name\nand the `-1` you specified will be replaced by the actual ID of the created\ncampaign automatically.\n\nHere are some things to keep in mind when using temporary resource names:\n\n- A temporary resource name can only be used after it's been defined in a resource. In the following example, the ad group operation would have to appear after the campaign operation in the list of operations.\n- Temporary resource names are not remembered across jobs or mutate requests. To reference a resource created in a previous job or mutate request, use its actual resource name.\n- For a single job or mutate request, each temporary resource name must use a unique negative number, even if they are from different resource types. If a temporary ID is reused in a single job or mutate request, then an error is returned.\n\n### Example\n\nSuppose you want to add a campaign, an ad group, and an ad in a single API\nrequest. You would create a structure for your request analogous to the\nfollowing: \n\n mutate_operations: [\n {\n campaign_operation: {\n create: {\n resource_name: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1\",\n ...\n }\n }\n },\n {\n ad_group_operation: {\n create: {\n resource_name: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/adGroups/-2\",\n campaign: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1\"\n ...\n }\n }\n },\n {\n ad_group_ad_operation: {\n create: {\n ad_group: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/adGroups/-2\"\n ...\n }\n }\n },\n ]\n\nA new temporary ID is used for the ad group, since we can't reuse the `-1` that\nwe used for the campaign. We also reference this ad group when creating an ad\ngroup ad. The ad group itself references the resource name we established for\nthe campaign in an earlier operation in the request, while `resource_name` in\n`ad_group_ad_operation` is not necessary since no further operation is\nreferencing it."]]