使用Core Reporting Google API v4(Java)显示超过10000行 [英] Displaying more than 10000 rows using Core Reporting Google API v4 ( Java)

查看:161
本文介绍了使用Core Reporting Google API v4(Java)显示超过10000行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Core Reporting API v4 。对于给定的Dimensions&组合,我能够捕获最多10,000条记录。指标。
我的问题是,如果我的查询可以产生超过10,000个搜索结果,那么我该如何获取所有这些记录?我已经浏览了文档,发现在单个请求中,我们无法通过设置ReportRequest对象的属性来访问超过10,000条记录。

I'm fetching Google Analytics data using Core Reporting API v4. I'm able to capture at most 10,000 records for a given combination of Dimensions & Metrics. My question is that if my query can produce more than 10,000 search results then how can I fetch all those records? I have gone through the documentation and found that in a single request we can't access more than 10,000 records by setting the properties of ReportRequest object.

ReportRequest request = new ReportRequest()
    .setDateRanges(Arrays.asList(dateRange)) 
    .setViewId(VIEW_ID)
    .setDimensions(Arrays.asList(dimension))
    .setMetrics(Arrays.asList(metric))
    .setPageSize(10000); 

我们如何在一次运行中启用多个请求,具体取决于搜索结果的数量获得。

How can we enable multiple requests in a single run depending upon the number of search-results that can be obtained.

例如:如果我的查询可以返回35,000条记录,那么应该有4条内部管理的请求(10,000,10,000,10,000和3,500)。

For example : If my query can return 35,000 records then there should be 4 requests (10,000,10,000, 10,000 & 3,500) managed internally.

请仔细研究并为我提供一些指导。在此先感谢。

Please look into this and facilitate me some guidance. Thanks in Advance.

推荐答案

这是一个稳定且经过广泛测试的Java解决方案。它是一个递归解决方案,存储每10000个结果批处理(如果有的话)并回忆自己,直到找到null nextToken。在此特定解决方案中,每10000个结果批处理保存到csv中,然后执行递归调用!注意,这个函数第一次从外面的某个地方调用时,nextPageToken也是null!专注于递归理论和空值检查!

Here's a stable and extensively tested solution in Java. It is a recursive solution that stores every 10000 results batch (if any) and recalls itself until finds a null nextToken. In this specific solution every 10000 results batch is saved into a csv and then a recursive call is performed! Note that the first time this function called from somewhere outside, the nextPageToken is also null!! Focus on the recursive rationale and the null value check!

private static int getComplexReport(AnalyticsReporting service,int 
reportIndex,java.lang.String startDate,String endDate,ArrayList<String>
metricNames,ArrayList<String> dimensionNames,String pageToken)    
throws IOException

ReportRequest req = createComplexRequest(startDate,endDate,metricNames,dimensionNames,pageToken);

ArrayList<ReportRequest> requests = new ArrayList<>();
requests.add(req);

// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest()
    .setReportRequests(requests);

// Call the batchGet method.
GetReportsResponse response = service.reports().batchGet(getReport).execute();
      //printResponse(response);


saveBatchToCsvFile("dummy_"+startDate+"_"+endDate+"_"+Integer.toString(reportIndex)+".csv",startDate+"_"+endDate,response,metricNames,dimensionNames);
String nextToken = response.getReports().get(0).getNextPageToken();
//System.out.println(nextToken);
if(nextToken!=null)
    return getComplexReport(service,reportIndex+1,"2016-06-21","2016-06-21",metricNames,dimensionNames,nextToken);

return reportIndex; 
} 

这篇关于使用Core Reporting Google API v4(Java)显示超过10000行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆