批量
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
有时,您无法从单个报告中获取所需的所有信息,例如当您的维度和指标不兼容时,或者您希望以不同的方式汇总数据时。
在此类情况下,您需要生成多个报告。
此外,批处理还可以减少请求数量,也是提高应用性能的有效方式。
发出多个独立请求的问题
虽然这是一个简单的实施解决方案,但在短时间内发出多个 API 请求可能会导致您遇到配额限制。
更好的解决方案是完全避免并行请求,而改为创建批量请求。
报告批量处理
在由用户驱动的应用示例中,第一种方法是将尽可能多的独立请求合并为一个批量请求。
报告 1:8 月份排名前 10 的国家/地区
报告 2:这 10 个国家/地区在 7 月份的值(进行对比)
报告 3:8 月份排名前 10 的渠道
报告 4:这 10 个渠道在 7 月份的值(进行对比)
在上述示例中,报告 1 和报告 3 是独立的,但无法合并为一份报告,因为它们提供的数据细分方式不同。不过,可以将它们一起批量处理。
只有在获得第一批结果后,才能生成报告 2 和报告 4,因为我们需要在请求中设置相应的过滤器。一旦第一批结果到达,这些作业可以作为第二批运行。
另一个方法是将所有请求合并到一个批次中,那就是直接返回 7 月的所有值(而不仅仅是前 10 个值),还可能返回 8 月的所有值。这可能是一个合理的选择,具体取决于相关 AdSense 帐号的大小以及您的应用可用的内存大小。
具体做法
批处理在每个客户端库中的处理方式不同,因此您应查看其文档以了解详情。以下是多个客户端库中相关文档的链接:
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eBatching requests improves app performance by reducing the number of API calls and mitigating quota limitations.\u003c/p\u003e\n"],["\u003cp\u003eIndependent reports, like those with incompatible dimensions or metrics, can be combined into batch requests.\u003c/p\u003e\n"],["\u003cp\u003eBatching can involve grouping related reports or retrieving broader data sets to minimize the total number of batches.\u003c/p\u003e\n"],["\u003cp\u003eSpecific batching implementations vary by client library (.NET, PHP, Python, Java, Ruby, etc.).\u003c/p\u003e\n"],["\u003cp\u003eFor handling very large reports or incompatible dimensions and metrics, refer to the provided resources for further guidance.\u003c/p\u003e\n"]]],["When single reports can't provide all necessary data due to incompatible dimensions or different aggregation needs, multiple reports are required. Batching multiple API requests improves app performance and reduces quota limitations, avoiding parallel requests. Independent requests, like retrieving top countries and channels, can be combined into a single batch. Subsequent requests for comparison data can be batched afterward, once the initial batch results are available. Client libraries offer varied methods for batching, requiring consultation of their respective documentation.\n"],null,["# Batch\n\nSometimes it's not possible to get all the information you need out of a single report, such as when you have incompatible dimensions and metrics, or when you're looking to aggregate data in different ways.\n\nIn those situations, you'll need to run multiple reports.\n\nAlso, batching is an effective way to improve your app's performance, reducing the number of requests.\n\nThe problem with making multiple independent requests\n-----------------------------------------------------\n\nWhile a simple solution to implement, making multiple API requests in a short amount of time may cause you to run into quota limitations.\n\nA better solution is to avoid the parallel requests altogether, and instead create batch requests.\n\nReport batching\n---------------\n\nIn the example of a user-driven application, a first approach would be to combine as many independent requests as possible into a single batch request.\n\n\n**Report 1:** Top 10 countries for August \n\n**Report 2:** Values for same 10 countries in July, for comparison \n\n**Report 3:** Top 10 channels for August \n\n**Report 4:** Values for same 10 channels in July, for comparison\n\nIn the above example, reports 1 and 3 are independent, but cannot be combined into a single report, since they offer different breakdowns of the data. They can, however, be batched together.\n\nReports 2 and 4 can only be run after obtaining the results of the first batch, since we need to set up an appropriate filter in the request. These can be run as a second batch, once the results from the first arrive.\n\nA different option, which would allow all requests to be combined into a single batch, would be to simply return all values for July (instead of just the top 10), and potentially August as well. This may be a reasonable option depending on the size of the AdSense account in question and the amount of memory available to your application.\n\nHow it's done\n-------------\n\nBatching is handled differently in each client library, so you should check its documentation in order to find out the details. Here are some links to the relevant documentation in several client libraries:\n\n- [.NET](https://developers.google.com/api-client-library/dotnet/guide/batch)\n- [PHP](https://developers.google.com/api-client-library/php/guide/batch)\n- [Python](https://developers.google.com/api-client-library/python/guide/batch)\n- [Java](https://code.google.com/p/google-api-java-client/wiki/Batch)\n- [Ruby](https://developers.google.com/api-client-library/ruby/guide/batch)\n\n\u003cbr /\u003e\n\nNext steps\n----------\n\n- [Running Large Reports](/adsense/management/reporting/large_reports)\n- [Incompatible Dimensions and Metrics](/adsense/management/reporting/dimensions_metrics#incompatible)"]]