Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit 6c88965

Browse files
author
Praful Makani
authored
docs(samples): add create redshift transfer config (#507)
* docs(samples): add create redshift transfer config * docs(samples): update code
1 parent 6d064d4 commit 6c88965

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquerydatatransfer;
18+
19+
// [START bigquerydatatransfer_create_redshift_transfer]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest;
22+
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
23+
import com.google.cloud.bigquery.datatransfer.v1.ProjectName;
24+
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
25+
import com.google.protobuf.Struct;
26+
import com.google.protobuf.Value;
27+
import java.io.IOException;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
// Sample to create redshift transfer config
32+
public class CreateRedshiftTransfer {
33+
34+
public static void main(String[] args) throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
final String projectId = "MY_PROJECT_ID";
37+
String datasetId = "MY_DATASET_ID";
38+
String datasetRegion = "US";
39+
String jdbcUrl = "MY_JDBC_URL_CONNECTION_REDSHIFT";
40+
String dbUserName = "MY_USERNAME";
41+
String dbPassword = "MY_PASSWORD";
42+
String accessKeyId = "MY_AWS_ACCESS_KEY_ID";
43+
String secretAccessId = "MY_AWS_SECRET_ACCESS_ID";
44+
String s3Bucket = "MY_S3_BUCKET_URI";
45+
String redShiftSchema = "MY_REDSHIFT_SCHEMA";
46+
String tableNamePatterns = "*";
47+
String vpcAndReserveIpRange = "MY_VPC_AND_IP_RANGE";
48+
Map<String, Value> params = new HashMap<>();
49+
params.put("jdbc_url", Value.newBuilder().setStringValue(jdbcUrl).build());
50+
params.put("database_username", Value.newBuilder().setStringValue(dbUserName).build());
51+
params.put("database_password", Value.newBuilder().setStringValue(dbPassword).build());
52+
params.put("access_key_id", Value.newBuilder().setStringValue(accessKeyId).build());
53+
params.put("secret_access_key", Value.newBuilder().setStringValue(secretAccessId).build());
54+
params.put("s3_bucket", Value.newBuilder().setStringValue(s3Bucket).build());
55+
params.put("redshift_schema", Value.newBuilder().setStringValue(redShiftSchema).build());
56+
params.put("table_name_patterns", Value.newBuilder().setStringValue(tableNamePatterns).build());
57+
params.put(
58+
"migration_infra_cidr", Value.newBuilder().setStringValue(vpcAndReserveIpRange).build());
59+
TransferConfig transferConfig =
60+
TransferConfig.newBuilder()
61+
.setDestinationDatasetId(datasetId)
62+
.setDatasetRegion(datasetRegion)
63+
.setDisplayName("Your Redshift Config Name")
64+
.setDataSourceId("redshift")
65+
.setParams(Struct.newBuilder().putAllFields(params).build())
66+
.setSchedule("every 24 hours")
67+
.build();
68+
createRedshiftTransfer(projectId, transferConfig);
69+
}
70+
71+
public static void createRedshiftTransfer(String projectId, TransferConfig transferConfig)
72+
throws IOException {
73+
try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
74+
ProjectName parent = ProjectName.of(projectId);
75+
CreateTransferConfigRequest request =
76+
CreateTransferConfigRequest.newBuilder()
77+
.setParent(parent.toString())
78+
.setTransferConfig(transferConfig)
79+
.build();
80+
TransferConfig config = client.createTransferConfig(request);
81+
System.out.println("Cloud redshift transfer created successfully :" + config.getName());
82+
} catch (ApiException ex) {
83+
System.out.print("Cloud redshift transfer was not created." + ex.toString());
84+
}
85+
}
86+
}
87+
// [END bigquerydatatransfer_create_redshift_transfer]

0 commit comments

Comments
 (0)