Répertorier les clusters

Répertorier tous les noms de cluster dans une instance.

Exemple de code

C++

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes Bigtable.

namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::InstanceAdmin instance_admin, std::string const& instance_id) {
  StatusOr<cbt::ClusterList> clusters =
      instance_admin.ListClusters(instance_id);
  if (!clusters) throw std::runtime_error(clusters.status().message());
  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;
using ::google::cloud::StatusOr;
[](cbt::InstanceAdmin instance_admin) {
  StatusOr<cbt::ClusterList> clusters = instance_admin.ListClusters();
  if (!clusters) throw std::runtime_error(clusters.status().message());
  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

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes 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

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes Bigtable.

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

PHP

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes Bigtable.

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

/**
 * 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);
    $getClusters = $instanceAdminClient->listClusters($instanceName)->getClusters();
    $clusters = $getClusters->getIterator();

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

Python

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes Bigtable.

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

Ruby

Pour savoir comment installer et utiliser la bibliothèque cliente pour Bigtable, consultez la section Bibliothèques clientes Bigtable.

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

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.