列出集群

列出实例中的所有集群名称。

代码示例

C++

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::StatusOr;
[](cbta::BigtableInstanceAdminClient instance_admin,
   std::string const& project_id, std::string const& instance_id) {
  std::string instance_name = cbt::InstanceName(project_id, instance_id);
  StatusOr<google::bigtable::admin::v2::ListClustersResponse> clusters =
      instance_admin.ListClusters(instance_name);
  if (!clusters) throw std::move(clusters).status();
  std::cout << "Cluster Name List\n";
  for (auto const& cluster : clusters->clusters()) {
    std::cout << "Cluster Name:" << cluster.name() << "\n";
  }
  if (!clusters->failed_locations().empty()) {
    std::cout << "The Cloud Bigtable service reports that the following "
                 "locations are temporarily unavailable and no information "
                 "about clusters in these locations can be obtained:\n";
    for (auto const& failed_location : clusters->failed_locations()) {
      std::cout << failed_location << "\n";
    }
  }
}
namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::StatusOr;
[](cbta::BigtableInstanceAdminClient instance_admin,
   std::string const& project_id) {
  std::string instance_name = cbt::InstanceName(project_id, "-");
  StatusOr<google::bigtable::admin::v2::ListClustersResponse> clusters =
      instance_admin.ListClusters(instance_name);
  if (!clusters) throw std::move(clusters).status();
  std::cout << "Cluster Name List\n";
  for (auto const& cluster : clusters->clusters()) {
    std::cout << "Cluster Name:" << cluster.name() << "\n";
  }
  if (!clusters->failed_locations().empty()) {
    std::cout << "The Cloud Bigtable service reports that the following "
                 "locations are temporarily unavailable and no information "
                 "about clusters in these locations can be obtained:\n";
    for (auto const& failed_location : clusters->failed_locations()) {
      std::cout << failed_location << "\n";
    }
  }
}

Java

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

try {
  List<Cluster> clusters = adminClient.listClusters(instanceId);
  for (Cluster cluster : clusters) {
    System.out.println(cluster.getId());
  }
} catch (NotFoundException e) {
  System.err.println("Failed to list clusters from a non-existent instance: " + e.getMessage());
}

Node.js

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

const instance3 = bigtable.instance(instanceID);
const [clusters] = await instance3.getClusters();
clusters.forEach(cluster => {
  console.log(cluster.id);
});

PHP

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\ListClustersRequest;

/**
 * List clusters of an instance
 *
 * @param string $projectId The Google Cloud project ID
 * @param string $instanceId The ID of the Bigtable instance
 */
function list_instance_clusters(
    string $projectId,
    string $instanceId
): void {
    $instanceAdminClient = new BigtableInstanceAdminClient();

    $projectName = $instanceAdminClient->projectName($projectId);
    $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);

    printf('Listing Clusters:' . PHP_EOL);
    $listClustersRequest = (new ListClustersRequest())
        ->setParent($instanceName);
    $getClusters = $instanceAdminClient->listClusters($listClustersRequest)->getClusters();
    $clusters = $getClusters->getIterator();

    foreach ($clusters as $cluster) {
        print($cluster->getName() . PHP_EOL);
    }
}

Python

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

print("\nListing clusters...")
for cluster in instance.list_clusters()[0]:
    print(cluster.cluster_id)

Ruby

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

# instance_id = "my-instance"
bigtable.instance(instance_id).clusters.all do |cluster|
  puts "Cluster: #{cluster.cluster_id}"
end

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器