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

Commit 96eb331

Browse files
author
Praful Makani
authored
docs(samples): add schedule backfill (#390)
1 parent 37097f8 commit 96eb331

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_schedule_backfill]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
22+
import com.google.cloud.bigquery.datatransfer.v1.ScheduleOptions;
23+
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
24+
import com.google.cloud.bigquery.datatransfer.v1.UpdateTransferConfigRequest;
25+
import com.google.common.collect.ImmutableList;
26+
import com.google.protobuf.FieldMask;
27+
import com.google.protobuf.Timestamp;
28+
import com.google.protobuf.util.FieldMaskUtil;
29+
import java.io.IOException;
30+
import org.threeten.bp.Clock;
31+
import org.threeten.bp.Instant;
32+
import org.threeten.bp.temporal.ChronoUnit;
33+
34+
// Sample to update schedule back fill for transfer config
35+
public class ScheduleBackFill {
36+
37+
public static void main(String[] args) throws IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String configId = "MY_CONFIG_ID";
40+
Clock clock = Clock.systemDefaultZone();
41+
Instant instant = clock.instant();
42+
Timestamp startDate =
43+
Timestamp.newBuilder()
44+
.setSeconds(instant.getEpochSecond())
45+
.setNanos(instant.getNano())
46+
.build();
47+
Timestamp endDate =
48+
Timestamp.newBuilder()
49+
.setSeconds(instant.plus(10, ChronoUnit.DAYS).getEpochSecond())
50+
.setNanos(instant.plus(10, ChronoUnit.DAYS).getNano())
51+
.build();
52+
TransferConfig transferConfig =
53+
TransferConfig.newBuilder()
54+
.setName(configId)
55+
.setScheduleOptions(
56+
ScheduleOptions.newBuilder().setStartTime(startDate).setEndTime(endDate).build())
57+
.build();
58+
FieldMask updateMask = FieldMaskUtil.fromStringList(ImmutableList.of("start_time", "end_time"));
59+
scheduleBackFill(transferConfig, updateMask);
60+
}
61+
62+
public static void scheduleBackFill(TransferConfig transferConfig, FieldMask updateMask)
63+
throws IOException {
64+
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
65+
UpdateTransferConfigRequest request =
66+
UpdateTransferConfigRequest.newBuilder()
67+
.setTransferConfig(transferConfig)
68+
.setUpdateMask(updateMask)
69+
.build();
70+
TransferConfig updateConfig = dataTransferServiceClient.updateTransferConfig(request);
71+
System.out.println(
72+
"Schedule backfill updated successfully :" + updateConfig.getDisplayName());
73+
} catch (ApiException ex) {
74+
System.out.print("Schedule backfill was not updated." + ex.toString());
75+
}
76+
}
77+
}
78+
// [END bigquerydatatransfer_schedule_backfill]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.datatransfer.v1.ScheduleOptions;
23+
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
24+
import com.google.common.collect.ImmutableList;
25+
import com.google.protobuf.FieldMask;
26+
import com.google.protobuf.Timestamp;
27+
import com.google.protobuf.util.FieldMaskUtil;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.PrintStream;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
33+
import org.junit.After;
34+
import org.junit.Before;
35+
import org.junit.BeforeClass;
36+
import org.junit.Test;
37+
import org.threeten.bp.Clock;
38+
import org.threeten.bp.Instant;
39+
import org.threeten.bp.temporal.ChronoUnit;
40+
41+
public class ScheduleBackFillIT {
42+
43+
private static final Logger LOG = Logger.getLogger(ScheduleBackFillIT.class.getName());
44+
private ByteArrayOutputStream bout;
45+
private PrintStream out;
46+
private PrintStream originalPrintStream;
47+
48+
private static final String CONFIG_NAME = requireEnvVar("DTS_TRANSFER_CONFIG_NAME");
49+
50+
private static String requireEnvVar(String varName) {
51+
String value = System.getenv(varName);
52+
assertNotNull(
53+
"Environment variable " + varName + " is required to perform these tests.",
54+
System.getenv(varName));
55+
return value;
56+
}
57+
58+
@BeforeClass
59+
public static void checkRequirements() {
60+
requireEnvVar("DTS_TRANSFER_CONFIG_NAME");
61+
}
62+
63+
@Before
64+
public void setUp() {
65+
bout = new ByteArrayOutputStream();
66+
out = new PrintStream(bout);
67+
originalPrintStream = System.out;
68+
System.setOut(out);
69+
}
70+
71+
@After
72+
public void tearDown() {
73+
// restores print statements in the original method
74+
System.out.flush();
75+
System.setOut(originalPrintStream);
76+
LOG.log(Level.INFO, bout.toString());
77+
}
78+
79+
@Test
80+
public void testScheduleBackFill() throws IOException {
81+
Clock clock = Clock.systemDefaultZone();
82+
Instant instant = clock.instant();
83+
Timestamp startDate =
84+
Timestamp.newBuilder()
85+
.setSeconds(instant.getEpochSecond())
86+
.setNanos(instant.getNano())
87+
.build();
88+
Timestamp endDate =
89+
Timestamp.newBuilder()
90+
.setSeconds(instant.plus(10, ChronoUnit.DAYS).getEpochSecond())
91+
.setNanos(instant.plus(10, ChronoUnit.DAYS).getNano())
92+
.build();
93+
TransferConfig transferConfig =
94+
TransferConfig.newBuilder()
95+
.setName(CONFIG_NAME)
96+
.setScheduleOptions(
97+
ScheduleOptions.newBuilder().setStartTime(startDate).setEndTime(endDate).build())
98+
.build();
99+
FieldMask updateMask = FieldMaskUtil.fromStringList(ImmutableList.of("start_time", "end_time"));
100+
ScheduleBackFill.scheduleBackFill(transferConfig, updateMask);
101+
assertThat(bout.toString()).contains("Schedule backfill updated successfully :");
102+
}
103+
}

0 commit comments

Comments
 (0)