Mencantumkan revisi skema untuk topik

Dokumen ini menunjukkan cara mencantumkan revisi skema untuk topik Pub/Sub.

Sebelum memulai

Peran dan izin yang diperlukan

Untuk mendapatkan izin yang diperlukan guna mencantumkan revisi skema dan mengelolanya, minta administrator untuk memberi Anda peran IAM Pub/Sub Editor (roles/pubsub.editor) di project Anda. Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.

Peran bawaan ini berisi izin yang diperlukan untuk mencantumkan revisi skema dan mengelolanya. Untuk melihat izin yang benar-benar diperlukan, luaskan bagian Izin yang diperlukan:

Izin yang diperlukan

Izin berikut diperlukan untuk mencantumkan revisi skema dan mengelolanya:

  • Buat skema: pubsub.schemas.create
  • Lampirkan skema ke topik: pubsub.schemas.attach
  • Meng-commit revisi skema: pubsub.schemas.commit
  • Menghapus skema atau revisi skema: pubsub.schemas.delete
  • Mendapatkan skema atau revisi skema: pubsub.schemas.get
  • Mencantumkan skema: pubsub.schemas.list
  • Mencantumkan revisi skema: pubsub.schemas.listRevisions
  • Mengembalikan skema: pubsub.schemas.rollback
  • Memvalidasi pesan: pubsub.schemas.validate
  • Mendapatkan kebijakan IAM untuk skema: pubsub.schemas.getIamPolicy
  • Konfigurasi kebijakan IAM untuk skema: pubsub.schemas.setIamPolicy

Anda mungkin juga bisa mendapatkan izin ini dengan peran khusus atau peran bawaan lainnya.

Anda dapat memberikan peran dan izin kepada entity utama seperti pengguna, grup, domain, atau akun layanan. Anda dapat membuat skema dalam satu project dan melampirkannya ke topik yang berada di project lain. Pastikan Anda memiliki izin yang diperlukan untuk setiap project.

Mencantumkan revisi skema

Anda dapat mencantumkan revisi skema dalam project Google Cloud menggunakan Google Cloud konsol, gcloud CLI, Pub/Sub API, atau Cloud Client Libraries.

Konsol

  1. Di konsol Google Cloud , buka halaman Pub/Sub schemas.

    Buka Skema

    Daftar skema akan ditampilkan.

  2. Klik nama skema yang ingin Anda lihat.

    Halaman Schema details untuk skema akan terbuka.

    Di bagian Revisi, Anda dapat melihat daftar revisi yang tersedia untuk skema.

gcloud

Untuk melihat revisi terbaru skema:

gcloud pubsub schemas list-revisions SCHEMA_ID

Gunakan perintah gcloud pubsub schemas list-revisions <var>SCHEMA_ID</var> --view=FULL untuk melihat definisi revisi skema.

REST

Untuk mencantumkan revisi skema untuk skema, kirim permintaan GET seperti berikut:

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

Jika berhasil, isi respons berisi objek JSON yang berisi semua revisi skema untuk skema.

C++

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan C++ di Panduan memulai: Menggunakan Library Klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi 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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di Panduan memulai: Menggunakan Library Klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Pub/Sub Go API.

import (
	"context"
	"fmt"
	"io"

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

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

	var schemas []*pubsubpb.Schema

	req := &pubsubpb.ListSchemaRevisionsRequest{
		Name: fmt.Sprintf("projects/%s/schemas/%s", projectID, schemaID),
		View: pubsubpb.SchemaView_FULL,
	}
	schemaIter := client.ListSchemaRevisions(ctx, req)
	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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Panduan memulai: Menggunakan Library Klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi 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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Panduan memulai: Menggunakan Library Klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi 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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai: Menggunakan Library Klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Node.js Pub/Sub.

/**
 * 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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai: Menggunakan Library Klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Node.js Pub/Sub.

/**
 * 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}.`);
}

Langkah berikutnya