Skip to content

ReportDownloader utilities

Josh Radcliff edited this page Mar 10, 2023 · 11 revisions

Overview

The client library includes a ReportDownloader utility for Ad Manager. This page describes the options the utility offers for processing report results.

For examples of how to construct and issue report requests, see the following folders:

Note: Several of the examples below make use of Guava I/O utilities.

Download report contents as a string

Use the approach below to download a report's contents as a string with embedded newlines. With this approach, you will hold the entire contents of the report in memory, so this is not the best option for large reports. Instead, consider downloading to a file or processing the report contents as a stream.

Ad Manager

ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(exportFormat);
options.setUseGzipCompression(true);

String reportContents = reportDownloader.
    getReportAsCharSource(options).read();

Download report contents to a file

Use the approach below to download a report's contents to a file.

Ad Manager

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));

Process the report contents as a stream

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.

Ad Manager

import java.io.BufferedReader;
...

ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(exportFormat);
options.setUseGzipCompression(true);

BufferedReader contentsReader = null;
try {
  contentsReader = reportDownloader.getReportAsCharSource(options)
      .openBufferedStream();

  String line;
  while ((line = contentsReader.readLine()) != null) {
    // Process a single line of report contents...
  }
} finally {
  if (contentsReader != null) {
    contentsReader.close();
  }
}

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.