Update a table Stay organized with collections Save and categorize content based on your preferences.
Update a table with customer-managed encryption keys.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Go
Before trying this sample, follow theGo setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryGo API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
import("context""fmt""cloud.google.com/go/bigquery")// updateTableChangeCMEK demonstrates how to change the customer managed encryption key that protects a table.funcupdateTableChangeCMEK(projectID,datasetID,tableIDstring)error{// projectID := "my-project-id"// datasetID := "mydatasetid"// tableID := "mytableid"ctx:=context.Background()client,err:=bigquery.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("bigquery.NewClient: %w",err)}deferclient.Close()tableRef:=client.Dataset(datasetID).Table(tableID)meta,err:=tableRef.Metadata(ctx)iferr!=nil{returnerr}update:=bigquery.TableMetadataToUpdate{EncryptionConfig:&bigquery.EncryptionConfig{// TODO: Replace this key with a key you have created in Cloud KMS.KMSKeyName:"projects/cloud-samples-tests/locations/us-central1/keyRings/test/cryptoKeys/otherkey",},}if_,err:=tableRef.Update(ctx,update,meta.ETag);err!=nil{returnerr}returnnil}Java
Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQueryException;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.cloud.bigquery.EncryptionConfiguration;importcom.google.cloud.bigquery.Table;importcom.google.cloud.bigquery.TableId;// Sample to update a cmek tablepublicclassUpdateTableCmek{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringdatasetName="MY_DATASET_NAME";StringtableName="MY_TABLE_NAME";StringkmsKeyName="MY_KEY_NAME";// Set a new encryption key to use for the destination.// i.e. projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{cryptoKey}EncryptionConfigurationencryption=EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build();updateTableCmek(datasetName,tableName,encryption);}publicstaticvoidupdateTableCmek(StringdatasetName,StringtableName,EncryptionConfigurationencryption){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.BigQuerybigquery=BigQueryOptions.getDefaultInstance().getService();Tabletable=bigquery.getTable(TableId.of(datasetName,tableName));bigquery.update(table.toBuilder().setEncryptionConfiguration(encryption).build());System.out.println("Table cmek updated successfully");}catch(BigQueryExceptione){System.out.println("Table cmek was not updated. \n"+e.toString());}}}Python
Before trying this sample, follow thePython setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPython API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
# from google.cloud import bigquery# client = bigquery.Client()asserttable.encryption_configuration.kms_key_name==original_kms_key_name# Set a new encryption key to use for the destination.# TODO: Replace this key with a key you have created in KMS.updated_kms_key_name=("projects/cloud-samples-tests/locations/us/keyRings/test/cryptoKeys/otherkey")table.encryption_configuration=bigquery.EncryptionConfiguration(kms_key_name=updated_kms_key_name)table=client.update_table(table,["encryption_configuration"])# API requestasserttable.encryption_configuration.kms_key_name==updated_kms_key_nameassertoriginal_kms_key_name!=updated_kms_key_nameWhat's next
To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.