Skip to content

Commit 720b5ac

Browse files
author
Praful Makani
authored
docs(samples): add update dataset partition expiration (#612)
1 parent db9eef7 commit 720b5ac

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.bigquery;
18+
19+
// [START bigquery_update_dataset_partition_expiration]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Dataset;
24+
import java.util.concurrent.TimeUnit;
25+
26+
// Sample to update partition expiration on a dataset.
27+
public class UpdateDatasetPartitionExpiration {
28+
29+
public static void runUpdateDatasetPartitionExpiration() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
// Set the default partition expiration (applies to new tables, only) in
33+
// milliseconds. This example sets the default expiration to 90 days.
34+
Long newExpiration = TimeUnit.MILLISECONDS.convert(90, TimeUnit.DAYS);
35+
updateDatasetPartitionExpiration(datasetName, newExpiration);
36+
}
37+
38+
public static void updateDatasetPartitionExpiration(String datasetName, Long newExpiration) {
39+
try {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
44+
Dataset dataset = bigquery.getDataset(datasetName);
45+
bigquery.update(dataset.toBuilder().setDefaultPartitionExpirationMs(newExpiration).build());
46+
System.out.println(
47+
"Dataset default partition expiration updated successfully to " + newExpiration);
48+
} catch (BigQueryException e) {
49+
System.out.println("Dataset partition expiration was not updated \n" + e.toString());
50+
}
51+
}
52+
}
53+
// [END bigquery_update_dataset_partition_expiration]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.concurrent.TimeUnit;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class UpdateDatasetPartitionExpirationIT {
32+
33+
private String datasetName;
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
private static final String GOOGLE_CLOUD_PROJECT = requireEnvVar("GOOGLE_CLOUD_PROJECT");
38+
39+
private static String requireEnvVar(String varName) {
40+
String value = System.getenv(varName);
41+
assertNotNull(
42+
"Environment variable " + varName + " is required to perform these tests.",
43+
System.getenv(varName));
44+
return value;
45+
}
46+
47+
@BeforeClass
48+
public static void checkRequirements() {
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
datasetName = RemoteBigQueryHelper.generateDatasetName();
58+
// Create a dataset in order to modify its partition expiration
59+
CreateDataset.createDataset(datasetName);
60+
61+
bout = new ByteArrayOutputStream();
62+
out = new PrintStream(bout);
63+
System.setOut(out);
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
// Clean up
69+
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
70+
System.setOut(null);
71+
}
72+
73+
@Test
74+
public void testUpdateDatasetPartitionExpiration() {
75+
Long newExpiration = TimeUnit.MILLISECONDS.convert(90, TimeUnit.DAYS);
76+
UpdateDatasetPartitionExpiration.updateDatasetPartitionExpiration(datasetName, newExpiration);
77+
assertThat(bout.toString())
78+
.contains("Dataset default partition expiration updated successfully");
79+
}
80+
}

0 commit comments

Comments
 (0)