将资产清单导出到 BigQuery。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Go
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
// Sample asset-quickstart exports assets to given bigquery table.
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
asset "cloud.google.com/go/asset/apiv1"
"cloud.google.com/go/asset/apiv1/assetpb"
)
func main() {
ctx := context.Background()
projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
client, err := asset.NewClient(ctx)
if err != nil {
log.Fatalf("asset.NewClient: %v", err)
}
defer client.Close()
datasetID := strings.Replace(fmt.Sprintf("%s-for-assets", projectID), "-", "_", -1)
dataset := fmt.Sprintf("projects/%s/datasets/%s", projectID, datasetID)
req := &assetpb.ExportAssetsRequest{
Parent: fmt.Sprintf("projects/%s", projectID),
OutputConfig: &assetpb.OutputConfig{
Destination: &assetpb.OutputConfig_BigqueryDestination{
BigqueryDestination: &assetpb.BigQueryDestination{
Dataset: dataset,
Table: "test",
Force: true,
},
},
},
}
op, err := client.ExportAssets(ctx, req)
if err != nil {
log.Fatalf("ExportAssets: %v", err)
}
resp, err := op.Wait(ctx)
if err != nil {
log.Fatalf("Wait: %v", err)
}
fmt.Print(resp)
}
Java
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
// Imports the Google Cloud client library
import com.google.cloud.ServiceOptions;
import com.google.cloud.asset.v1.AssetServiceClient;
import com.google.cloud.asset.v1.BigQueryDestination;
import com.google.cloud.asset.v1.ContentType;
import com.google.cloud.asset.v1.ExportAssetsRequest;
import com.google.cloud.asset.v1.ExportAssetsRequest.Builder;
import com.google.cloud.asset.v1.ExportAssetsResponse;
import com.google.cloud.asset.v1.OutputConfig;
import com.google.cloud.asset.v1.PartitionSpec;
import com.google.cloud.asset.v1.ProjectName;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
public class ExportAssetsBigqueryExample {
// Use the default project Id.
private static final String projectId = ServiceOptions.getDefaultProjectId();
/**
* Export assets to BigQuery for a project.
* @param bigqueryDataset which dataset the results will be exported to
* @param bigqueryTable which table the results will be exported to
* @param contentType determines the schema for the table
* @param assetTypes a list of asset types to export. if empty, export all.
* @param isPerType separate BigQuery tables for each resource type
*/
public static void exportBigQuery(String bigqueryDataset, String bigqueryTable,
ContentType contentType, String[] assetTypes, boolean isPerType)
throws IOException, IllegalArgumentException, InterruptedException, ExecutionException {
try (AssetServiceClient client = AssetServiceClient.create()) {
ProjectName parent = ProjectName.of(projectId);
OutputConfig outputConfig;
// Outputs to per-type BigQuery table.
if (isPerType) {
outputConfig =
OutputConfig.newBuilder()
.setBigqueryDestination(
BigQueryDestination.newBuilder()
.setDataset(bigqueryDataset)
.setTable(bigqueryTable)
.setForce(true)
.setSeparateTablesPerAssetType(true)
.setPartitionSpec(
PartitionSpec.newBuilder()
.setPartitionKey(PartitionSpec.PartitionKey.READ_TIME)
.build())
.build())
.build();
} else {
outputConfig =
OutputConfig.newBuilder()
.setBigqueryDestination(
BigQueryDestination.newBuilder()
.setDataset(bigqueryDataset)
.setTable(bigqueryTable)
.setForce(true)
.build())
.build();
}
Builder exportAssetsRequestBuilder = ExportAssetsRequest.newBuilder()
.setParent(parent.toString()).setContentType(contentType).setOutputConfig(outputConfig);
if (assetTypes.length > 0) {
exportAssetsRequestBuilder.addAllAssetTypes(Arrays.asList(assetTypes));
}
ExportAssetsRequest request = exportAssetsRequestBuilder.build();
ExportAssetsResponse response = client.exportAssetsAsync(request).get();
System.out.println(response);
}
}
}
Node.js
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const dataSet = 'projects/project_id/datasets/dataset_id';
// const table = 'mytable';
const {AssetServiceClient} = require('@google-cloud/asset');
const client = new AssetServiceClient();
async function exportAssetsBigquery() {
const projectId = await client.getProjectId();
const projectResource = client.projectPath(projectId);
const dataset = dataSet;
const request = {
parent: projectResource,
outputConfig: {
bigqueryDestination: {
dataset: `projects/${projectId}/${dataset}`,
table: table,
force: true,
},
},
};
// Handle the operation using the promise pattern.
const [operation] = await client.exportAssets(request);
// Operation#promise starts polling for the completion of the operation.
const [result] = await operation.promise();
// Do things with with the response.
console.log(result);
}
exportAssetsBigquery();
Python
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
from google.cloud import asset_v1
# TODO project_id = 'Your Google Cloud Project ID'
# TODO dataset = 'Your BigQuery dataset path'
# TODO table = 'Your BigQuery table name'
# TODO content_type ="Content type to export"
client = asset_v1.AssetServiceClient()
parent = f"projects/{project_id}"
output_config = asset_v1.OutputConfig()
output_config.bigquery_destination.dataset = dataset
output_config.bigquery_destination.table = table
output_config.bigquery_destination.force = True
response = client.export_assets(
request={
"parent": parent,
"content_type": content_type,
"output_config": output_config,
}
)
print(response.result())
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。