List clusters

List all of the cluster names in an instance.

Code sample

C++

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

PHP

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

Ruby

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.