列出分类

列出现有分类。

代码示例

Go

在试用此示例之前,请按照 Data Catalog 快速入门:使用客户端库中的 Go 设置说明进行操作。 如需了解详情,请参阅 Data Catalog Go API 参考文档

如需向 Data Catalog 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	datacatalog "cloud.google.com/go/datacatalog/apiv1beta1"
	"cloud.google.com/go/datacatalog/apiv1beta1/datacatalogpb"
	"google.golang.org/api/iterator"
)

// listTaxonomies prints information about the taxonomies contained within a specific
// project and location.
func listTaxonomies(w io.Writer, projectID, location string) error {
	// projectID := "my-project-id"
	// location := "us"
	ctx := context.Background()
	policyClient, err := datacatalog.NewPolicyTagManagerClient(ctx)
	if err != nil {
		return fmt.Errorf("datacatalog.NewPolicyTagManagerClient: %w", err)
	}
	defer policyClient.Close()

	req := &datacatalogpb.ListTaxonomiesRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
	}
	it := policyClient.ListTaxonomies(ctx, req)
	fmt.Fprintf(w, "listing taxonomies in project %s and location %s\n", projectID, location)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListTaxonomies iteration error: %w", err)
		}

		fmt.Fprintf(w, "\t- %s: %s\n", resp.Name, resp.DisplayName)
	}
	return nil
}

Node.js

在试用此示例之前,请按照 Data Catalog 快速入门:使用客户端库中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 Data Catalog Node.js API 参考文档

如需向 Data Catalog 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

// Import the Google Cloud client library.
const {DataCatalogClient, PolicyTagManagerClient} =
  require('@google-cloud/datacatalog').v1;
const dataCatalog = new DataCatalogClient();
const policyTagManager = new PolicyTagManagerClient();

async function listTaxonomies() {
  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // const projectId = 'my_project'; // Google Cloud Platform project
  // const location = 'us';

  // Parent project location format is `projects/${projectId}/locations/${location}`
  const parent = dataCatalog.locationPath(projectId, location);

  const request = {
    parent: parent,
  };

  try {
    const [taxonomies] = await policyTagManager.listTaxonomies(request);
    console.log('Taxonomies:');
    taxonomies.forEach(taxonomy => {
      console.log(taxonomy.name);
    });
  } catch (e) {
    console.error(e);
    process.exitCode = 1;
  }
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器