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

Commit d2eb675

Browse files
author
Praful Makani
authored
docs(samples): create and delete scheduled query (#225)
1 parent 87d834e commit d2eb675

File tree

7 files changed

+370
-1
lines changed

7 files changed

+370
-1
lines changed

samples/install-without-bom/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
<version>1.0.1version>
4646
<scope>testscope>
4747
dependency>
48+
<dependency>
49+
<groupId>com.google.cloudgroupId>
50+
<artifactId>google-cloud-bigqueryartifactId>
51+
<version>1.116.3version>
52+
<scope>testscope>
53+
dependency>
4854
dependencies>
4955

5056

samples/snapshot/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
<version>1.0.1version>
4545
<scope>testscope>
4646
dependency>
47+
<dependency>
48+
<groupId>com.google.cloudgroupId>
49+
<artifactId>google-cloud-bigqueryartifactId>
50+
<version>1.116.3version>
51+
<scope>testscope>
52+
dependency>
4753
dependencies>
4854

4955
@@ -80,4 +86,4 @@
8086
plugin>
8187
plugins>
8288
build>
83-
project>
89+
project>

samples/snippets/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@
5757
<version>1.0.1version>
5858
<scope>testscope>
5959
dependency>
60+
<dependency>
61+
<groupId>com.google.cloudgroupId>
62+
<artifactId>google-cloud-bigqueryartifactId>
63+
<version>1.116.3version>
64+
<scope>testscope>
65+
dependency>
6066
dependencies>
6167
project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_scheduled_query]
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+
28+
import java.io.IOException;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
32+
// Sample to create a scheduled query
33+
public class CreateScheduledQuery {
34+
35+
public static void runCreateScheduledQuery() {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "MY_PROJECT_ID";
38+
String datasetId = "MY_DATASET_ID";
39+
String query =
40+
"SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time, @run_date as intended_run_date, 17 as some_integer";
41+
Map<String, Value> params = new HashMap<>();
42+
params.put("query", Value.newBuilder().setStringValue(query).build());
43+
params.put(
44+
"destination_table_name_template",
45+
Value.newBuilder().setStringValue("my_destination_table_{run_date}").build());
46+
params.put("write_disposition", Value.newBuilder().setStringValue("WRITE_TRUNCATE").build());
47+
params.put("partitioning_field", Value.newBuilder().build());
48+
TransferConfig transferConfig =
49+
TransferConfig.newBuilder()
50+
.setDestinationDatasetId(datasetId)
51+
.setDisplayName("Your Scheduled Query Name")
52+
.setDataSourceId("scheduled_query")
53+
.setParams(Struct.newBuilder().putAllFields(params).build())
54+
.setSchedule("every 24 hours")
55+
.build();
56+
createScheduledQuery(projectId, transferConfig);
57+
}
58+
59+
public static void createScheduledQuery(String projectId, TransferConfig transferConfig) {
60+
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
61+
ProjectName parent = ProjectName.of(projectId);
62+
CreateTransferConfigRequest request =
63+
CreateTransferConfigRequest.newBuilder()
64+
.setParent(parent.toString())
65+
.setTransferConfig(transferConfig)
66+
.build();
67+
TransferConfig config = dataTransferServiceClient.createTransferConfig(request);
68+
System.out.print("Scheduled query created successfully." + config.getName());
69+
} catch (IOException | ApiException ex) {
70+
System.out.print("Scheduled query was not created." + ex.toString());
71+
}
72+
}
73+
}
74+
// [END bigquerydatatransfer_create_scheduled_query]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_delete_scheduled_query]
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.DeleteTransferConfigRequest;
23+
24+
import java.io.IOException;
25+
26+
// Sample to delete a scheduled query
27+
public class DeleteScheduledQuery {
28+
29+
public static void runDeleteScheduledQuery() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
// i.e projects/{project_id}/transferConfigs/{config_id}` or
32+
// `projects/{project_id}/locations/{location_id}/transferConfigs/{config_id}`
33+
String name = "MY_CONFIG_ID";
34+
deleteScheduledQuery(name);
35+
}
36+
37+
public static void deleteScheduledQuery(String name) {
38+
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
39+
DeleteTransferConfigRequest request =
40+
DeleteTransferConfigRequest.newBuilder().setName(name).build();
41+
dataTransferServiceClient.deleteTransferConfig(request);
42+
System.out.print("Scheduled query deleted successfully.");
43+
} catch (IOException | ApiException ex) {
44+
System.out.print("Scheduled query was not deleted." + ex.toString());
45+
}
46+
}
47+
}
48+
// [END bigquerydatatransfer_delete_scheduled_query]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryOptions;
21+
import com.google.cloud.bigquery.DatasetInfo;
22+
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
23+
import com.google.cloud.bigquery.datatransfer.v1.TransferState;
24+
import com.google.protobuf.Struct;
25+
import com.google.protobuf.Value;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
import java.io.ByteArrayOutputStream;
32+
import java.io.PrintStream;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
import java.util.UUID;
36+
37+
import static com.google.common.truth.Truth.assertThat;
38+
import static junit.framework.TestCase.assertNotNull;
39+
40+
public class CreateScheduledQueryIT {
41+
42+
private BigQuery bigquery;
43+
private ByteArrayOutputStream bout;
44+
private String name;
45+
private String displayName;
46+
private String datasetName;
47+
private PrintStream out;
48+
49+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
51+
private static String requireEnvVar(String varName) {
52+
String value = System.getenv(varName);
53+
assertNotNull(
54+
"Environment variable " + varName + " is required to perform these tests.",
55+
System.getenv(varName));
56+
return value;
57+
}
58+
59+
@BeforeClass
60+
public static void checkRequirements() {
61+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
62+
}
63+
64+
@Before
65+
public void setUp() {
66+
displayName = "MY_SCHEDULE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
67+
datasetName = "MY_DATASET_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
68+
// create a temporary dataset
69+
bigquery = BigQueryOptions.getDefaultInstance().getService();
70+
bigquery.create(DatasetInfo.of(datasetName));
71+
bout = new ByteArrayOutputStream();
72+
out = new PrintStream(bout);
73+
System.setOut(out);
74+
}
75+
76+
@After
77+
public void tearDown() {
78+
// Clean up
79+
DeleteScheduledQuery.deleteScheduledQuery(name);
80+
// delete a temporary dataset
81+
bigquery.delete(datasetName, BigQuery.DatasetDeleteOption.deleteContents());
82+
System.setOut(null);
83+
}
84+
85+
@Test
86+
public void testCreateScheduledQuery() {
87+
String query =
88+
"SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time, @run_date as intended_run_date, 17 as some_integer";
89+
String destinationTableName =
90+
"MY_DESTINATION_TABLE_" + UUID.randomUUID().toString().substring(0, 8) + "_{run_date}";
91+
Map<String, Value> params = new HashMap<>();
92+
params.put("query", Value.newBuilder().setStringValue(query).build());
93+
params.put(
94+
"destination_table_name_template",
95+
Value.newBuilder().setStringValue(destinationTableName).build());
96+
params.put("write_disposition", Value.newBuilder().setStringValue("WRITE_TRUNCATE").build());
97+
params.put("partitioning_field", Value.newBuilder().setStringValue("").build());
98+
TransferConfig transferConfig =
99+
TransferConfig.newBuilder()
100+
.setDestinationDatasetId(datasetName)
101+
.setDisplayName(displayName)
102+
.setDataSourceId("scheduled_query")
103+
.setParams(Struct.newBuilder().putAllFields(params).build())
104+
.setSchedule("every 24 hours")
105+
.setState(TransferState.CANCELLED)
106+
.build();
107+
CreateScheduledQuery.createScheduledQuery(PROJECT_ID, transferConfig);
108+
String result = bout.toString();
109+
name = result.substring(result.indexOf(".") + 1);
110+
assertThat(result).contains("Scheduled query created successfully.");
111+
}
112+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryOptions;
21+
import com.google.cloud.bigquery.DatasetInfo;
22+
import com.google.cloud.bigquery.datatransfer.v1.TransferConfig;
23+
import com.google.protobuf.Struct;
24+
import com.google.protobuf.Value;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.PrintStream;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
import java.util.UUID;
35+
36+
import static com.google.common.truth.Truth.assertThat;
37+
import static junit.framework.TestCase.assertNotNull;
38+
39+
public class DeleteScheduledQueryIT {
40+
41+
private BigQuery bigquery;
42+
private ByteArrayOutputStream bout;
43+
private String name;
44+
private String displayName;
45+
private String datasetName;
46+
private PrintStream out;
47+
48+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
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("GOOGLE_CLOUD_PROJECT");
61+
}
62+
63+
@Before
64+
public void setUp() {
65+
bout = new ByteArrayOutputStream();
66+
out = new PrintStream(bout);
67+
System.setOut(out);
68+
69+
displayName = "MY_SCHEDULE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
70+
datasetName = "MY_DATASET_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
71+
// create a temporary dataset
72+
bigquery = BigQueryOptions.getDefaultInstance().getService();
73+
bigquery.create(DatasetInfo.of(datasetName));
74+
75+
// create a scheduled query
76+
String query =
77+
"SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time, @run_date as intended_run_date, 17 as some_integer";
78+
String destinationTableName =
79+
"MY_DESTINATION_TABLE_" + UUID.randomUUID().toString().substring(0, 8) + "_{run_date}";
80+
Map<String, Value> params = new HashMap<>();
81+
params.put("query", Value.newBuilder().setStringValue(query).build());
82+
params.put(
83+
"destination_table_name_template",
84+
Value.newBuilder().setStringValue(destinationTableName).build());
85+
params.put("write_disposition", Value.newBuilder().setStringValue("WRITE_TRUNCATE").build());
86+
params.put("partitioning_field", Value.newBuilder().setStringValue("").build());
87+
TransferConfig transferConfig =
88+
TransferConfig.newBuilder()
89+
.setDestinationDatasetId(datasetName)
90+
.setDisplayName(displayName)
91+
.setDataSourceId("scheduled_query")
92+
.setParams(Struct.newBuilder().putAllFields(params).build())
93+
.setSchedule("every 24 hours")
94+
.build();
95+
CreateScheduledQuery.createScheduledQuery(PROJECT_ID, transferConfig);
96+
String result = bout.toString();
97+
name = result.substring(result.indexOf(".") + 1);
98+
99+
bout = new ByteArrayOutputStream();
100+
out = new PrintStream(bout);
101+
System.setOut(out);
102+
}
103+
104+
@After
105+
public void tearDown() {
106+
// delete a temporary dataset
107+
bigquery.delete(datasetName, BigQuery.DatasetDeleteOption.deleteContents());
108+
System.setOut(null);
109+
}
110+
111+
@Test
112+
public void testDeleteScheduledQuery() {
113+
// delete scheduled query that was just created
114+
DeleteScheduledQuery.deleteScheduledQuery(name);
115+
assertThat(bout.toString()).contains("Scheduled query deleted successfully.");
116+
}
117+
}

0 commit comments

Comments
 (0)