スキーマの詳細を表示する

このドキュメントでは、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 コンソール、gcloud CLI、Pub/Sub API、または Cloud クライアント ライブラリを使用して取得できます。結果は、最新のリビジョン ID の詳細を返します。

Console

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

    スキーマに移動

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

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

gcloud

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

gcloud pubsub schemas describe SCHEMA_NAME

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

gcloud pubsub schemas describe SCHEMA_ID@REVISION_ID

ここで

  • REVISION_ID は、ロールバックするリビジョンです。

REST

スキーマの最新リビジョンの詳細を取得するには、次のような GET リクエストを送信します。

GET https://pubsub.googleapis.com/v1/projects/PROJECT_ID/schemas/SCHEMA_ID

たとえば、スキーマ ID が schema-inventory のスキーマの場合は、次のリクエストを送信します。https://pubsub.googleapis.com/v1/projects/PROJECT_ID/schemas/schema-inventory

スキーマの特定のリビジョンの詳細を取得するには、次のような GET リクエストを送信します。

GET https://pubsub.googleapis.com/v1/projects/PROJECT_ID/schemas/SCHEMA_ID@REVISION_ID

たとえば、スキーマ ID が schema-inventory でリビジョン ID が fa567a3e のスキーマの場合は、次のリクエストを送信します。https://pubsub.googleapis.com/v1/projects/PROJECT_ID/schemas/schema-inventory@fa567a3e

ここで

  • PROJECT_ID はプロジェクト ID です。
  • SCHEMA_ID は、スキーマ ID です。
  • REVISION_ID は、特定のスキーマのリビジョンの ID です。

成功した場合、レスポンスの本文にはスキーマクラスのインスタンスが含まれます。

C++

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

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::GetSchemaRequest request;
  request.set_name(
      pubsub::Schema(project_id, schema_id_with_revision).FullName());
  request.set_view(google::pubsub::v1::FULL);
  auto schema = client.GetSchema(request);
  if (!schema) throw std::move(schema).status();

  std::cout << "The schema revision exists and its metadata is:"
            << "\n"
            << schema->DebugString() << "\n";
}

C#

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


using Google.Cloud.PubSub.V1;

public class GetSchemaSample
{
    public Schema GetSchema(string projectId, string schemaId)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        GetSchemaRequest request = new GetSchemaRequest
        {
            SchemaName = SchemaName.FromProjectSchema(projectId, schemaId),
            View = SchemaView.Full
        };

        return schemaService.GetSchema(request);
    }
}

Go

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

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/pubsub"
)

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

	s, err := client.Schema(ctx, schemaID, pubsub.SchemaViewFull)
	if err != nil {
		return fmt.Errorf("client.Schema revision: %w", err)
	}
	fmt.Fprintf(w, "Got schema revision: %#v\n", s)
	return nil
}

Java

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


import com.google.api.gax.rpc.NotFoundException;
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 GetSchemaRevisionExample {

  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-schema-revision]";
    getSchemaRevisionExample(projectId, schemaId);
  }

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

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

      Schema schema = schemaServiceClient.getSchema(schemaName);

      System.out.println("Got a schema:\n" + schema);

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

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 getSchema(schemaNameOrId) {
  const schema = pubSubClient.schema(schemaNameOrId);
  const info = await schema.get();
  const fullName = await schema.getName();
  console.log(`Schema ${fullName} info: ${JSON.stringify(info, null, 4)}.`);
}

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 getSchema(schemaNameOrId: string) {
  const schema = pubSubClient.schema(schemaNameOrId);
  const info = await schema.get();
  const fullName = await schema.getName();
  console.log(`Schema ${fullName} info: ${JSON.stringify(info, null, 4)}.`);
}

PHP

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

use Google\Cloud\PubSub\PubSubClient;

/**
 * Get a schema.
 *
 * @param string $projectId
 * @param string $schemaId
 */
function get_schema($projectId, $schemaId)
{
    $pubsub = new PubSubClient([
        'projectId' => $projectId,
    ]);

    $schema = $pubsub->schema($schemaId);
    $schema->info();

    printf('Schema %s retrieved', $schema->name());
}

Python

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

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"
# schema_revision_id = "your-schema-revision-id"

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

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

Ruby

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

# schema_id = "your-schema-id"

pubsub = Google::Cloud::Pubsub.new

schema = pubsub.schema schema_id

puts "Schema #{schema.name} retrieved."

次のステップ