Skip to content
Permalink
Browse files
docs(samples): add update dataset partition expiration (#612)
  • Loading branch information
Praful Makani committed Jul 28, 2020
1 parent db9eef7 commit 720b5acfe2f9f46eebb18b6e5d0db881b0ad7e2a
@@ -0,0 +1,53 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

// [START bigquery_update_dataset_partition_expiration]
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import java.util.concurrent.TimeUnit;

// Sample to update partition expiration on a dataset.
public class UpdateDatasetPartitionExpiration {

public static void runUpdateDatasetPartitionExpiration() {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "MY_DATASET_NAME";
// Set the default partition expiration (applies to new tables, only) in
// milliseconds. This example sets the default expiration to 90 days.
Long newExpiration = TimeUnit.MILLISECONDS.convert(90, TimeUnit.DAYS);
updateDatasetPartitionExpiration(datasetName, newExpiration);
}

public static void updateDatasetPartitionExpiration(String datasetName, Long newExpiration) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

Dataset dataset = bigquery.getDataset(datasetName);
bigquery.update(dataset.toBuilder().setDefaultPartitionExpirationMs(newExpiration).build());
System.out.println(
"Dataset default partition expiration updated successfully to " + newExpiration);
} catch (BigQueryException e) {
System.out.println("Dataset partition expiration was not updated \n" + e.toString());
}
}
}
// [END bigquery_update_dataset_partition_expiration]
@@ -0,0 +1,80 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class UpdateDatasetPartitionExpirationIT {

private String datasetName;
private ByteArrayOutputStream bout;
private PrintStream out;

private static final String GOOGLE_CLOUD_PROJECT = requireEnvVar("GOOGLE_CLOUD_PROJECT");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
assertNotNull(
"Environment variable " + varName + " is required to perform these tests.",
System.getenv(varName));
return value;
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
datasetName = RemoteBigQueryHelper.generateDatasetName();
// Create a dataset in order to modify its partition expiration
CreateDataset.createDataset(datasetName);

bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
// Clean up
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
System.setOut(null);
}

@Test
public void testUpdateDatasetPartitionExpiration() {
Long newExpiration = TimeUnit.MILLISECONDS.convert(90, TimeUnit.DAYS);
UpdateDatasetPartitionExpiration.updateDatasetPartitionExpiration(datasetName, newExpiration);
assertThat(bout.toString())
.contains("Dataset default partition expiration updated successfully");
}
}

0 comments on commit 720b5ac

Please sign in to comment.