Schedule a backfill run

Initiate a data backfill to load historical data into BigQuery. For information about how much data is available for backfill, see the documentation for your data source.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


import org.threeten.bp.Clock;
import org.threeten.bp.Instant;
import org.threeten.bp.temporal.ChronoUnit;

// Sample to run schedule back fill for transfer config
public class ScheduleBackFill {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String configId = "MY_CONFIG_ID";
    Clock clock = Clock.systemDefaultZone();
    Instant instant = clock.instant();
    Timestamp startTime =
            .setSeconds(instant.minus(5, ChronoUnit.DAYS).getEpochSecond())
            .setNanos(instant.minus(5, ChronoUnit.DAYS).getNano())
    Timestamp endTime =
            .setSeconds(instant.minus(2, ChronoUnit.DAYS).getEpochSecond())
            .setNanos(instant.minus(2, ChronoUnit.DAYS).getNano())
    scheduleBackFill(configId, startTime, endTime);

  public static void scheduleBackFill(String configId, Timestamp startTime, Timestamp endTime)
      throws IOException {
    try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
      ScheduleTransferRunsRequest request =
      ScheduleTransferRunsResponse response = client.scheduleTransferRuns(request);
      System.out.println("Schedule backfill run successfully :" + response.getRunsCount());
    } catch (ApiException ex) {
      System.out.print("Schedule backfill was not run." + ex.toString());


import datetime

from import bigquery_datatransfer

transfer_client = bigquery_datatransfer.DataTransferServiceClient()

transfer_config_name = "projects/1234/locations/us/transferConfigs/abcd"
now =
start_time = now - datetime.timedelta(days=5)
end_time = now - datetime.timedelta(days=2)

# Some data sources, such as scheduled_query only support daily run.
# Truncate start_time and end_time to midnight time (00:00AM UTC).
start_time = datetime.datetime(
    start_time.year, start_time.month,, tzinfo=datetime.timezone.utc
end_time = datetime.datetime(
    end_time.year, end_time.month,, tzinfo=datetime.timezone.utc

response = transfer_client.schedule_transfer_runs(
    parent=transfer_config_name, start_time=start_time, end_time=end_time,

print("Started transfer runs:")
for run in response.runs:
    print(f"backfill: {run.run_time} run: {}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser