Skip to content

Commit baf20c4

Browse files
bq_update_dataset_expiration sample (GoogleCloudPlatform#1783)
* bq_update_dataset_expiration sample * update sample according to comments
1 parent b891b3d commit baf20c4

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019 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_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+
public class UpdateDatasetExpiration {
27+
28+
public static void runUpdateDatasetExpiration() {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String datasetName = "my-dataset-name";
31+
updateDatasetExpiration(datasetName);
32+
}
33+
34+
public static void updateDatasetExpiration(String datasetName) {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests.
37+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38+
39+
Dataset dataset = bigquery.getDataset(datasetName);
40+
41+
// Update dataset expiration to one day
42+
Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
43+
try {
44+
bigquery.update(dataset.toBuilder().setDefaultTableLifetime(newExpiration).build());
45+
System.out.println("Dataset description updated successfully to " + newExpiration);
46+
} catch (BigQueryException e) {
47+
System.out.println("Dataset expiration was not updated \n" + e.toString());
48+
}
49+
}
50+
}
51+
// [END bigquery_update_dataset_expiration]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2019 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+
21+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class UpdateDatasetExpirationIT {
29+
private ByteArrayOutputStream bout;
30+
private PrintStream out;
31+
32+
@Before
33+
public void setUp() throws Exception {
34+
bout = new ByteArrayOutputStream();
35+
out = new PrintStream(bout);
36+
System.setOut(out);
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
System.setOut(null);
42+
}
43+
44+
@Test
45+
public void updateDatasetExpiration() {
46+
String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
47+
// Create a dataset in order to modify its expiration
48+
CreateDataset.createDataset(generatedDatasetName);
49+
50+
// Modify dataset's expiration
51+
UpdateDatasetExpiration.updateDatasetExpiration(generatedDatasetName);
52+
assertThat(bout.toString()).contains("Dataset description updated successfully");
53+
}
54+
}

0 commit comments

Comments
 (0)