-
Notifications
You must be signed in to change notification settings - Fork 360
ReportDownloader utilities
The client library includes ReportDownloader
utilities for AdWords and DFP. This page describes the options each utility offers for processing report results.
For examples of how to construct and issue report requests, see the following folders:
- AdWords: examples/adwords_axis/src/main/java/adwords/axis/v201509/reporting
- DFP: examples/dfp_axis/src/main/java/dfp/axis/v201508/reportservice
Note: Several of the examples below make use of Guava I/O utilities.
Use the approach below to download a report's contents as a string with embedded newlines.
ReportDownloadResponse reportDownloadResponse = reportDownloader.downloadReport(...);
String reportContents = reportDownloadResponse.getAsString();
ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(exportFormat);
options.setUseGzipCompression(true);
String reportContents = reportDownloader.getReportAsCharSource(options).read();
Use the approach below to download a report's contents to a file.
ReportDownloadResponse reportDownloadResponse = reportDownloader.downloadReport(...);
File file = File.createTempFile("report", ".csv");
reportDownloadResponse.saveToFile(file.getPath());
import com.google.io.Files;
import com.google.io.Resources;
...
File file = File.createTempFile("report", ".csv.gz");
ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(exportFormat);
options.setUseGzipCompression(true);
URL url = reportDownloader.getDownloadUrl(options);
// Use the Resources and Files utilities in com.google.common.io to copy
// the report contents to the file.
Resources.asByteSource(url).copyTo(Files.asByteSink(file));
Use the approach below to process a report's contents as an InputStream
. This is useful if the report output is extremely large and you do not want to hold the entire contents in memory.
The examples show how to convert the report output into a CharSource
and then process the contents one line at a time.
import com.google.api.client.util.Charsets;
import com.google.io.ByteSource;
import com.google.io.CharSource;
...
final ReportDownloadResponse reportDownloadResponse = reportDownloader.downloadReport(...);
CharSource charSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return response.getInputStream();
}
}.asCharSource(Charsets.UTF_8);
for (String line : charSource.readLines()) {
// Process a single line of report contents...
}
The example above will work for text formats such as CSV
, TSV
, and XML
. If you request one of the gzipped formats such as GZIPPED_CSV
or GZIPPED_XML
, you should wrap the stream returned by response.getInputStream()
in a java.util.zip.GZIPInputStream
.
CharSource charSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new GZIPInputStream(response.getInputStream());
}
}.asCharSource(Charsets.UTF_8);
If you simply want to read the contents as a stream of bytes, you can just use the InputStream
returned by response.getInputStream()
directly.
import com.google.io.CharSource;
...
ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(exportFormat);
options.setUseGzipCompression(true);
CharSource charSource = reportDownloader.getReportAsCharSource(options);
for (String line : charSource.readLines()) {
// Process a single line of report contents...
}
If you simply want to read the contents as a stream of bytes, you can just use the InputStream
returned by reportDownloader.getDownloadUrl().openStream()
directly.