输出精简版主题的名称、保留配置和分区配置。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Go
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import (
"context"
"fmt"
"io"
"cloud.google.com/go/pubsublite"
)
func getTopic(w io.Writer, projectID, region, location, topicID string) error {
// projectID := "my-project-id"
// region := "us-central1"
// NOTE: location can be either a region ("us-central1") or a zone ("us-central1-a")
// For a list of valid locations, see https://cloud.google.com/pubsub/lite/docs/locations.
// location := "us-central1"
// topicID := "my-topic"
ctx := context.Background()
client, err := pubsublite.NewAdminClient(ctx, region)
if err != nil {
return fmt.Errorf("pubsublite.NewAdminClient: %w", err)
}
defer client.Close()
topicPath := fmt.Sprintf("projects/%s/locations/%s/topics/%s", projectID, location, topicID)
topic, err := client.Topic(ctx, topicPath)
if err != nil {
return fmt.Errorf("client.Topic got err: %w", err)
}
fmt.Fprintf(w, "Got topic: %#v\n", *topic)
return nil
}
Java
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsublite.AdminClient;
import com.google.cloud.pubsublite.AdminClientSettings;
import com.google.cloud.pubsublite.CloudRegion;
import com.google.cloud.pubsublite.CloudRegionOrZone;
import com.google.cloud.pubsublite.CloudZone;
import com.google.cloud.pubsublite.ProjectNumber;
import com.google.cloud.pubsublite.TopicName;
import com.google.cloud.pubsublite.TopicPath;
import com.google.cloud.pubsublite.proto.Topic;
import java.util.concurrent.ExecutionException;
public class GetTopicExample {
public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String cloudRegion = "your-cloud-region";
char zoneId = 'b';
// Choose an existing topic.
String topicId = "your-topic-id";
long projectNumber = Long.parseLong("123456789");
// True if using a regional location. False if using a zonal location.
// https://cloud.google.com/pubsub/lite/docs/topics
boolean regional = true;
getTopicExample(cloudRegion, zoneId, projectNumber, topicId, regional);
}
public static void getTopicExample(
String cloudRegion, char zoneId, long projectNumber, String topicId, boolean regional)
throws Exception {
CloudRegionOrZone location;
if (regional) {
location = CloudRegionOrZone.of(CloudRegion.of(cloudRegion));
} else {
location = CloudRegionOrZone.of(CloudZone.of(CloudRegion.of(cloudRegion), zoneId));
}
TopicPath topicPath =
TopicPath.newBuilder()
.setProject(ProjectNumber.of(projectNumber))
.setLocation(location)
.setName(TopicName.of(topicId))
.build();
AdminClientSettings adminClientSettings =
AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();
try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
Topic topic = adminClient.getTopic(topicPath).get();
long numPartitions = adminClient.getTopicPartitionCount(topicPath).get();
System.out.println(topic.getAllFields() + "\nhas " + numPartitions + " partition(s).");
} catch (ExecutionException e) {
try {
throw e.getCause();
} catch (NotFoundException notFound) {
System.out.println("This topic is not found.");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
Python
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath
# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# zone_id = "a"
# topic_id = "your-topic-id"
# regional = True
location = None
if regional:
# A region.
location = CloudRegion(cloud_region)
else:
# A zone.
location = CloudZone(CloudRegion(cloud_region), zone_id)
topic_path = TopicPath(project_number, location, topic_id)
client = AdminClient(cloud_region)
try:
response = client.get_topic(topic_path)
num_partitions = client.get_topic_partition_count(topic_path)
print(f"{response.name} has {num_partitions} partition(s).")
except NotFound:
print(f"{topic_path} not found.")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。