使用临时 ID
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
BatchJobService
的一项强大功能是支持使用临时 ID。
为此,您可以指定新资源的 resource_name
以使用负 ID。例如,假设您创建了一个广告系列,并将其资源名称指定为 customers/<YOUR_CUSTOMER_ID>/campaigns/-1
。在后续操作中创建广告组时,您可以使用该资源名称引用该广告组,并且您指定的 -1
将自动替换为所创建广告系列的实际 ID。
使用临时资源名称时,请注意以下事项:
- 临时资源名称只能在资源中定义后使用。在下例的操作列表中,广告组操作必须显示在广告系列操作之后。
- 系统无法跨作业或 mutate 请求记住临时资源名称。如需引用在之前的作业或变异请求中创建的资源,请使用其真实的资源名称。
- 对于单个作业或 mutate 请求,每个临时资源名称都必须使用具有唯一性的负数,即使它们来自不同的资源类型。如果在单个作业或 mutate 请求中重复使用临时 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"
...
}
}
},
]
广告组使用了新的临时 ID,这是因为我们无法重复利用广告系列已使用的 -1
。我们还会在创建广告组广告时引用此广告组。广告组本身会引用资源名称,而该资源名称是我们为该请求的早期操作中的广告系列确定的,而 ad_group_ad_operation
中的 resource_name
则不是必需的,因为没有进一步的操作会引用它。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):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."]]