從結構定義中刪除修訂版本

刪除結構定義的修訂版本

深入探索

如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:

程式碼範例

C++

在試用這個範例之前,請先按照C++Pub/Sub 快速入門導覽課程:使用用戶端程式庫」中的操作說明進行設定。 詳情請參閱 Pub/Sub C++ API 參考說明文件

如要驗證 Pub/Sub,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
   std::string const& schema_id, std::string const& revision_id) {
  std::string const schema_id_with_revision = schema_id + "@" + revision_id;

  google::pubsub::v1::DeleteSchemaRevisionRequest request;
  request.set_name(
      pubsub::Schema(project_id, schema_id_with_revision).FullName());
  auto schema = client.DeleteSchemaRevision(request);
  if (!schema) throw std::move(schema).status();

  std::cout << "Deleted schema. Its metadata is:"
            << "\n"
            << schema->DebugString() << "\n";
}

C#

在試用這個範例之前,請先按照C#Pub/Sub 快速入門導覽課程:使用用戶端程式庫」中的操作說明進行設定。 詳情請參閱 Pub/Sub C# API 參考說明文件

如要驗證 Pub/Sub,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。


using Google.Cloud.PubSub.V1;

public class DeleteSchemaRevisionSample
{
    public void DeleteSchemaRevision(string projectId, string schemaId, string revisionId)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        DeleteSchemaRevisionRequest request = new DeleteSchemaRevisionRequest
        {
            SchemaName = SchemaName.FromProjectSchema(projectId, schemaId + "@" + revisionId)
        };
        schemaService.DeleteSchemaRevision(request);
    }
}

Go

在試用這個範例之前,請先按照GoPub/Sub 快速入門導覽課程:使用用戶端程式庫」中的操作說明進行設定。 詳情請參閱 Pub/Sub Go API 參考說明文件

如要驗證 Pub/Sub,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

import (
	"context"
	"fmt"
	"io"

	pubsub "cloud.google.com/go/pubsub/v2/apiv1"
	"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)

func deleteSchemaRevision(w io.Writer, projectID, schemaID, revisionID string) error {
	// projectID := "my-project-id"
	// schemaID := "my-schema-id"
	// revisionID := "my-revision-id"
	ctx := context.Background()
	client, err := pubsub.NewSchemaClient(ctx)
	if err != nil {
		return fmt.Errorf("pubsub.NewSchemaClient: %w", err)
	}
	defer client.Close()

	req := &pubsubpb.DeleteSchemaRevisionRequest{
		Name: fmt.Sprintf("projects/%s/schemas/%s@%s", projectID, schemaID, revisionID),
	}
	if _, err := client.DeleteSchemaRevision(ctx, req); err != nil {
		return fmt.Errorf("client.DeleteSchemaRevision: %w", err)
	}
	fmt.Fprintf(w, "Deleted a schema revision: %s@%s", schemaID, revisionID)
	return nil
}

Java

在試用這個範例之前,請先按照JavaPub/Sub 快速入門導覽課程:使用用戶端程式庫」中的操作說明進行設定。 詳情請參閱 Pub/Sub Java API 參考說明文件

如要驗證 Pub/Sub,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。


import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsub.v1.SchemaServiceClient;
import com.google.pubsub.v1.DeleteSchemaRevisionRequest;
import com.google.pubsub.v1.SchemaName;
import java.io.IOException;

public class DeleteSchemaRevisionExample {

  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String schemaId = "your-schema-id@your-revision-id";

    deleteSchemaRevisionExample(projectId, schemaId);
  }

  public static void deleteSchemaRevisionExample(String projectId, String schemaId)
      throws IOException {
    SchemaName schemaName = SchemaName.of(projectId, schemaId);

    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {

      DeleteSchemaRevisionRequest request =
          DeleteSchemaRevisionRequest.newBuilder().setName(schemaName.toString()).build();

      schemaServiceClient.deleteSchemaRevision(request);

      System.out.println("Deleted a schema revision:" + schemaName);

    } catch (NotFoundException e) {
      System.out.println(schemaName + "not found.");
    }
  }
}

Node.js

在試用這個範例之前,請先按照Node.jsPub/Sub 快速入門導覽課程:使用用戶端程式庫」中的操作說明進行設定。 詳情請參閱 Pub/Sub Node.js API 參考說明文件

如要驗證 Pub/Sub,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';
// const revisionId = 'YOUR_REVISION_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function deleteSchemaRevision(schemaNameOrId, revisionId) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to delete the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.deleteSchemaRevision({
    name: `${name}@${revisionId}`,
  });

  console.log(`Schema ${name} revision ${revisionId} deleted.`);
}

Node.js

在嘗試這個範例之前,請先按照使用用戶端程式庫的 Pub/Sub 快速入門導覽課程中的 Node.js 設定說明進行操作。詳情請參閱 Pub/Sub Node.js API 參考說明文件

如要驗證 Pub/Sub,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';
// const revisionId = 'YOUR_REVISION_ID';

// Imports the Google Cloud client library
import {PubSub} from '@google-cloud/pubsub';

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function deleteSchemaRevision(
  schemaNameOrId: string,
  revisionId: string,
) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to delete the schema revision.
  const schemaClient = await pubSubClient.getSchemaClient();
  await schemaClient.deleteSchemaRevision({
    name: `${name}@${revisionId}`,
  });

  console.log(`Schema ${name} revision ${revisionId} deleted.`);
}

Python

在試用這個範例之前,請先按照PythonPub/Sub 快速入門導覽課程:使用用戶端程式庫」中的操作說明進行設定。 詳情請參閱 Pub/Sub Python API 參考說明文件

如要驗證 Pub/Sub,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

from google.api_core.exceptions import NotFound
from google.cloud.pubsub import SchemaServiceClient

# TODO(developer): Replace these variables before running the sample.
# project_id = "your-project-id"
# schema_id = "your-schema-id"
# revision_id = "your-revision-id"

schema_client = SchemaServiceClient()
schema_path = schema_client.schema_path(project_id, schema_id + "@" + revision_id)

try:
    schema_client.delete_schema_revision(request={"name": schema_path})
    print(f"Deleted a schema revision:\n{schema_path}")
except NotFound:
    print(f"{schema_id} not found.")

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器