トピックのスキーマ リビジョンを一覧表示する

このドキュメントでは、Pub/Sub トピックのスキーマのリビジョンを一覧表示する方法について説明します。

準備

必要なロールと権限

スキーマのリビジョンを一覧表示して管理するために必要な権限を取得するには、管理者にPub/Sub 編集者 roles/pubsub.editor )プロジェクトに対する IAM ロールを付与するよう依頼してください。ロールの付与の詳細については、アクセス権の管理をご覧ください。

この事前定義ロールには、スキーマ リビジョンを一覧表示して管理するために必要な権限が含まれています。必要とされる正確な権限については、「必要な権限」セクションを開いてご確認ください。

必要な権限

スキーマ リビジョンを一覧表示して管理するには、次の権限が必要です。

  • スキーマを作成します: pubsub.schemas.create
  • スキーマをトピックに添付します: pubsub.schemas.attach
  • スキーマのリビジョンを commit します: pubsub.schemas.commit
  • スキーマまたはスキーマ リビジョンを削除します: pubsub.schemas.delete
  • スキーマまたはスキーマのリビジョンを取得します: pubsub.schemas.get
  • スキーマを一覧表示します: pubsub.schemas.list
  • スキーマのリビジョンを一覧表示します: pubsub.schemas.listRevisions
  • スキーマをロールバックします: pubsub.schemas.rollback
  • メッセージを検証します: pubsub.schemas.validate
  • スキーマの IAM ポリシーを取得します: pubsub.schemas.getIamPolicy
  • スキーマの IAM ポリシーを構成します: pubsub.schemas.setIamPolicy

カスタムロールや他の事前定義ロールを使用して、これらの権限を取得することもできます。

ユーザー、グループ、ドメイン、サービス アカウントなどのプリンシパルにロールと権限を付与できます。あるプロジェクトにスキーマを作成し、別のプロジェクトにあるトピックにアタッチできます。プロジェクトごとに必要な権限があることを確認します。

スキーマのリビジョンを一覧表示する

Google Cloud プロジェクトのスキーマのリビジョンを一覧表示するには、Google Cloud コンソール、gcloud CLI、Pub/Sub API、Cloud クライアント ライブラリを使用します。

Console

  1. Google Cloud コンソールで、[Pub/Sub スキーマ] ページに移動します。

    スキーマに移動

    スキーマのリストが表示されます。

  2. 表示するスキーマの名前をクリックします。

    スキーマの [スキーマの詳細] ページが開きます。

    [リビジョン] セクションに、スキーマで使用可能なリビジョンのリストが表示されます。

gcloud

スキーマの最新リビジョンを表示するには:

gcloud pubsub schemas list-revisions SCHEMA_ID

gcloud pubsub schemas list-revisions <var>SCHEMA_ID</var> --view=FULL コマンドを使用して、スキーマ リビジョンの定義を確認します。

REST

スキーマのスキーマ リビジョンを一覧表示するには、次のような GET リクエストを送信します。

GET https://pubsub.googleapis.com/v1/projects/SCHEMA_NAME:listRevisions

成功した場合、レスポンスの本文にはスキーマのすべてのスキーマ リビジョンを含む JSON オブジェクトが含まれます。

C++

このサンプルを試す前に、クイックスタート: クライアント ライブラリの使用の C++ の設定手順を実施してください。詳細については、Pub/Sub C++ API のリファレンス ドキュメントをご覧ください。

namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
   std::string const& schema_id) {
  auto const parent = pubsub::Schema(project_id, schema_id).FullName();
  for (auto& s : client.ListSchemaRevisions(parent)) {
    if (!s) throw std::move(s).status();
    std::cout << "Schema revision: " << s->DebugString() << "\n";
  }
}

Go

このサンプルを試す前に、クイックスタート: クライアント ライブラリの使用の Go の設定手順を実施してください。詳細については、Pub/Sub Go API のリファレンス ドキュメントをご覧ください。

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsub"
	"google.golang.org/api/iterator"
)

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

	var schemas []*pubsub.SchemaConfig

	schemaIter := client.ListSchemaRevisions(ctx, schemaID, pubsub.SchemaViewFull)
	for {
		sc, err := schemaIter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("schemaIter.Next: %w", err)
		}
		fmt.Fprintf(w, "Got schema revision: %#v\n", sc)
		schemas = append(schemas, sc)
	}

	fmt.Fprintf(w, "Got %d schema revisions", len(schemas))
	return schemas, nil
}

Java

このサンプルを試す前に、クイックスタート: クライアント ライブラリの使用の Java の設定手順を実施してください。詳細については、Pub/Sub Java API のリファレンス ドキュメントをご覧ください。

import com.google.cloud.pubsub.v1.SchemaServiceClient;
import com.google.pubsub.v1.Schema;
import com.google.pubsub.v1.SchemaName;
import java.io.IOException;

public class ListSchemaRevisionsExample {
  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";

    listSchemaRevisionsExample(projectId, schemaId);
  }

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

    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
      for (Schema schema : schemaServiceClient.listSchemaRevisions(schemaName).iterateAll()) {
        System.out.println(schema);
      }
      System.out.println("Listed schema revisions.");
    }
  }
}

Python

このサンプルを試す前に、クイックスタート: クライアント ライブラリの使用の Python の設定手順を実施してください。詳細については、Pub/Sub Python API のリファレンス ドキュメントをご覧ください。

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"

schema_client = SchemaServiceClient()
schema_path = schema_client.schema_path(project_id, schema_id)

for schema in schema_client.list_schema_revisions(request={"name": schema_path}):
    print(schema)

print("Listed schema revisions.")

Node.js

このサンプルを試す前に、クイックスタート: クライアント ライブラリの使用の Node.js の設定手順を実施してください。詳細については、Pub/Sub Node.js API のリファレンス ドキュメントをご覧ください。

/**
 * TODO(developer): Uncomment this variable before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_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 listSchemaRevisions(schemaNameOrId) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to list the schema revisions.
  const schemaClient = await pubSubClient.getSchemaClient();
  const [results] = await schemaClient.listSchemaRevisions({
    name,
  });
  for (const rev of results) {
    console.log(rev.revisionId, rev.revisionCreateTime);
  }
  console.log(`Listed revisions of schema ${name}.`);
}

Node.js

このサンプルを試す前に、クイックスタート: クライアント ライブラリの使用の Node.js の設定手順を実施してください。詳細については、Pub/Sub Node.js API のリファレンス ドキュメントをご覧ください。

/**
 * TODO(developer): Uncomment this variable before running the sample.
 */
// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_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 listSchemaRevisions(schemaNameOrId: string) {
  // Get the fully qualified schema name.
  const schema = pubSubClient.schema(schemaNameOrId);
  const name = await schema.getName();

  // Use the gapic client to list the schema revisions.
  const schemaClient = await pubSubClient.getSchemaClient();
  const [results] = await schemaClient.listSchemaRevisions({
    name,
  });
  for (const rev of results) {
    console.log(rev.revisionId, rev.revisionCreateTime);
  }
  console.log(`Listed revisions of schema ${name}.`);
}

次のステップ