启动数据回填,将历史数据加载到 BigQuery 中。如需了解可以回填的数据量,请参阅您的数据源对应的文档。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
Java
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
import com.google.cloud.bigquery.datatransfer.v1.ScheduleTransferRunsRequest;
import com.google.cloud.bigquery.datatransfer.v1.ScheduleTransferRunsResponse;
import com.google.protobuf.Timestamp;
import java.io.IOException;
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 =
Timestamp.newBuilder()
.setSeconds(instant.minus(5, ChronoUnit.DAYS).getEpochSecond())
.setNanos(instant.minus(5, ChronoUnit.DAYS).getNano())
.build();
Timestamp endTime =
Timestamp.newBuilder()
.setSeconds(instant.minus(2, ChronoUnit.DAYS).getEpochSecond())
.setNanos(instant.minus(2, ChronoUnit.DAYS).getNano())
.build();
scheduleBackFill(configId, startTime, endTime);
}
public static void scheduleBackFill(String configId, Timestamp startTime, Timestamp endTime)
throws IOException {
try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
ScheduleTransferRunsRequest request =
ScheduleTransferRunsRequest.newBuilder()
.setParent(configId)
.setStartTime(startTime)
.setEndTime(endTime)
.build();
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());
}
}
}
Python
import datetime
from google.cloud import bigquery_datatransfer
transfer_client = bigquery_datatransfer.DataTransferServiceClient()
transfer_config_name = "projects/1234/locations/us/transferConfigs/abcd"
now = datetime.datetime.now(datetime.timezone.utc)
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, start_time.day, tzinfo=datetime.timezone.utc
)
end_time = datetime.datetime(
end_time.year, end_time.month, end_time.day, 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: {run.name}")