전송 구성을 만들어 프로젝트, 위치 또는 둘 다에서 데이터 세트의 모든 테이블을 복사합니다.
코드 샘플
자바
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest;
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
import com.google.cloud.bigquery.datatransfer.v1.ProjectName;
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
// Sample to copy dataset from another gcp project
public class CopyDataset {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
final String destinationProjectId = "MY_DESTINATION_PROJECT_ID";
final String destinationDatasetId = "MY_DESTINATION_DATASET_ID";
final String sourceProjectId = "MY_SOURCE_PROJECT_ID";
final String sourceDatasetId = "MY_SOURCE_DATASET_ID";
Map<String, Value> params = new HashMap<>();
params.put("source_project_id", Value.newBuilder().setStringValue(sourceProjectId).build());
params.put("source_dataset_id", Value.newBuilder().setStringValue(sourceDatasetId).build());
TransferConfig transferConfig =
TransferConfig.newBuilder()
.setDestinationDatasetId(destinationDatasetId)
.setDisplayName("Your Dataset Copy Name")
.setDataSourceId("cross_region_copy")
.setParams(Struct.newBuilder().putAllFields(params).build())
.setSchedule("every 24 hours")
.build();
copyDataset(destinationProjectId, transferConfig);
}
public static void copyDataset(String projectId, TransferConfig transferConfig)
throws IOException {
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
ProjectName parent = ProjectName.of(projectId);
CreateTransferConfigRequest request =
CreateTransferConfigRequest.newBuilder()
.setParent(parent.toString())
.setTransferConfig(transferConfig)
.build();
TransferConfig config = dataTransferServiceClient.createTransferConfig(request);
System.out.println("Copy dataset created successfully :" + config.getName());
} catch (ApiException ex) {
System.out.print("Copy dataset was not created." + ex.toString());
}
}
}
Python
from google.cloud import bigquery_datatransfer
transfer_client = bigquery_datatransfer.DataTransferServiceClient()
destination_project_id = "my-destination-project"
destination_dataset_id = "my_destination_dataset"
source_project_id = "my-source-project"
source_dataset_id = "my_source_dataset"
transfer_config = bigquery_datatransfer.TransferConfig(
destination_dataset_id=destination_dataset_id,
display_name="Your Dataset Copy Name",
data_source_id="cross_region_copy",
params={
"source_project_id": source_project_id,
"source_dataset_id": source_dataset_id,
},
schedule="every 24 hours",
)
transfer_config = transfer_client.create_transfer_config(
parent=transfer_client.common_project_path(destination_project_id),
transfer_config=transfer_config,
)
print(f"Created transfer config: {transfer_config.name}")
다음 단계
다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.