|
| 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] |
0 commit comments