列出实例

列出当前项目中的实例。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

C++

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

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

namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::Project;
using ::google::cloud::StatusOr;
[](cbta::BigtableInstanceAdminClient instance_admin,
   std::string const& project_id) {
  std::string project_name = Project(project_id).FullName();
  StatusOr<google::bigtable::admin::v2::ListInstancesResponse> instances =
      instance_admin.ListInstances(project_name);
  if (!instances) throw std::move(instances).status();
  for (auto const& instance : instances->instances()) {
    std::cout << instance.name() << "\n";
  }
  if (!instances->failed_locations().empty()) {
    std::cout << "The Cloud Bigtable service reports that the following "
                 "locations are temporarily unavailable and no information "
                 "about instances in these locations can be obtained:\n";
    for (auto const& failed_location : instances->failed_locations()) {
      std::cout << failed_location << "\n";
    }
  }
}

C#

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

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

// Lists instances in the project.
// Initialize request argument(s).
ListInstancesRequest listInstancesRequest = new ListInstancesRequest
{
    ParentAsProjectName = new ProjectName(projectId)
};
try
{
    // Make a request.
    Console.WriteLine("Waiting for operation to complete...");
    ListInstancesResponse instances = bigtableInstanceAdminClient.ListInstances(listInstancesRequest);
}
catch (Exception ex)
{
    Console.WriteLine($"Exception while requesting information about instances in {projectId} project");
    Console.WriteLine(ex.Message);
    return -1;
}
Console.WriteLine(new string('-', 50));

Java

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

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

try {
  List<Instance> instances = adminClient.listInstances();
  for (Instance instance : instances) {
    System.out.println(instance.getId());
  }
} catch (PartialListInstancesException e) {
  System.err.println("Failed to list instances: " + e.getMessage());
  System.err.println("The following zones are unavailable: " + e.getUnavailableZones());
  System.err.println("But the following instances are reachable: " + e.getInstances());
}

Node.js

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

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

const [instances] = await bigtable.getInstances();
instances.forEach(instance => {
  console.log(instance.id);
});

PHP

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

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

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

/**
 * List Bigtable instances in a project
 *
 * @param string $projectId The Google Cloud project ID
 */
function list_instance(string $projectId): void
{
    $instanceAdminClient = new BigtableInstanceAdminClient();

    $projectName = $instanceAdminClient->projectName($projectId);

    printf('Listing Instances:' . PHP_EOL);
    $listInstancesRequest = (new ListInstancesRequest())
        ->setParent($projectName);

    $getInstances = $instanceAdminClient->listInstances($listInstancesRequest)->getInstances();
    $instances = $getInstances->getIterator();

    foreach ($instances as $instance) {
        print($instance->getName() . PHP_EOL);
    }
}

Python

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

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

print("\nListing instances:")
for instance_local in client.list_instances()[0]:
    print(instance_local.instance_id)

Ruby

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

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

bigtable.instances.all do |instance|
  puts "Instance: #{instance.instance_id}"
end

后续步骤

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