查询具有客户管理的加密密钥的表。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Go
尝试此示例之前,请按照《BigQuery 快速入门:使用客户端库》中的 Go 设置说明进行操作。如需了解详情,请参阅 BigQuery Go API 参考文档。
import (
"context"
"fmt"
"io"
"cloud.google.com/go/bigquery"
"google.golang.org/api/iterator"
)
// queryWithDestinationCMEK demonstrates saving query results to a destination table and protecting those results
// by specifying a customer managed encryption key.
func queryWithDestinationCMEK(w io.Writer, projectID, dstDatasetID, dstTableID string) error {
// projectID := "my-project-id"
// datasetID := "mydataset"
// tableID := "mytable"
ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
return fmt.Errorf("bigquery.NewClient: %v", err)
}
defer client.Close()
q := client.Query("SELECT 17 as my_col")
q.Location = "US" // Location must match the dataset(s) referenced in query.
q.QueryConfig.Dst = client.Dataset(dstDatasetID).Table(dstTableID)
q.DestinationEncryptionConfig = &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/test",
}
// Run the query and process the returned row iterator.
it, err := q.Read(ctx)
if err != nil {
return fmt.Errorf("query.Read(): %v", err)
}
for {
var row []bigquery.Value
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
return err
}
fmt.Fprintln(w, row)
}
return nil
}
Java
试用此示例之前,请按照《BigQuery 快速入门:使用客户端库》中的 Java 设置说明进行操作。如需了解详情,请参阅 BigQuery Java API 参考文档。
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.EncryptionConfiguration;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
// Sample to query on destination table with encryption key
public class QueryDestinationTableCmek {
public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
String kmsKeyName = "MY_KMS_KEY_NAME";
String query =
String.format("SELECT stringField, booleanField FROM %s.%s", datasetName, tableName);
EncryptionConfiguration encryption =
EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build();
queryDestinationTableCmek(query, encryption);
}
public static void queryDestinationTableCmek(String query, EncryptionConfiguration encryption) {
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();
QueryJobConfiguration config =
QueryJobConfiguration.newBuilder(query)
// Set the encryption key to use for the destination.
.setDestinationEncryptionConfiguration(encryption)
.build();
TableResult results = bigquery.query(config);
results
.iterateAll()
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
System.out.println("Query performed successfully with encryption key.");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Query not performed \n" + e.toString());
}
}
}
Python
尝试此示例之前,请按照《BigQuery 快速入门:使用客户端库》中的 Python 设置说明进行操作。如需了解详情,请参阅 BigQuery Python API 参考文档。
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
# TODO(developer): Set table_id to the ID of the destination table.
# table_id = "your-project.your_dataset.your_table_name"
# Set the encryption key to use for the destination.
# TODO(developer): Replace this key with a key you have created in KMS.
# kms_key_name = "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}".format(
# your-project, location, your-ring, your-key
# )
job_config = bigquery.QueryJobConfig(
destination=table_id,
destination_encryption_configuration=bigquery.EncryptionConfiguration(
kms_key_name=kms_key_name
),
)
# Start the query, passing in the extra configuration.
query_job = client.query(
"SELECT 17 AS my_col;", job_config=job_config
) # Make an API request.
query_job.result() # Wait for the job to complete.
table = client.get_table(table_id) # Make an API request.
if table.encryption_configuration.kms_key_name == kms_key_name:
print("The destination table is written using the encryption configuration")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。