删除 Pub/Sub 精简版预留
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Go
import (
"context"
"fmt"
"io"
"cloud.google.com/go/pubsublite"
)
func deleteReservation(w io.Writer, projectID, region, reservationID string) error {
// projectID := "my-project-id"
// region := "us-central1"
// reservationID := "my-reservation"
ctx := context.Background()
client, err := pubsublite.NewAdminClient(ctx, region)
if err != nil {
return fmt.Errorf("pubsublite.NewAdminClient: %v", err)
}
defer client.Close()
reservationPath := fmt.Sprintf("projects/%s/locations/%s/reservations/%s", projectID, region, reservationID)
err = client.DeleteReservation(ctx, reservationPath)
if err != nil {
return fmt.Errorf("client.DeleteReservation got err: %v", err)
}
fmt.Fprint(w, "Deleted reservation")
return nil
}
Java
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.ProjectNumber;
import com.google.cloud.pubsublite.ReservationName;
import com.google.cloud.pubsublite.ReservationPath;
import java.util.concurrent.ExecutionException;
public class DeleteReservationExample {
public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
long projectNumber = Long.parseLong("123456789");
String cloudRegion = "your-cloud-region";
String reservationId = "your-reservation-id";
deleteReservationExample(projectNumber, cloudRegion, reservationId);
}
public static void deleteReservationExample(
long projectNumber, String cloudRegion, String reservationId) throws Exception {
ReservationPath reservationPath =
ReservationPath.newBuilder()
.setProject(ProjectNumber.of(projectNumber))
.setLocation(CloudRegion.of(cloudRegion))
.setName(ReservationName.of(reservationId))
.build();
AdminClientSettings adminClientSettings =
AdminClientSettings.newBuilder().setRegion(CloudRegion.of(cloudRegion)).build();
// If a reservation has topics attached, you must delete the topics before deleting
// the reservation.
try (AdminClient adminClient = AdminClient.create(adminClientSettings)) {
adminClient.deleteReservation(reservationPath).get();
System.out.println(reservationPath + " deleted successfully.");
} catch (ExecutionException e) {
try {
throw e.getCause();
} catch (NotFoundException notFound) {
System.out.println("This reservation is not found.");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
Python
from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import CloudRegion, ReservationPath
# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# reservation_id = "your-reservation-id"
cloud_region = CloudRegion(cloud_region)
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)
client = AdminClient(cloud_region)
try:
client.delete_reservation(reservation_path)
print(f"{reservation_path} deleted successfully.")
except NotFound:
print(f"{reservation_path} not found.")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。