获取实例信息

本页面介绍了如何使用 Bigtable 客户端库以编程方式列出项目中的所有实例并获取实例详细信息,或者使用 Google Cloud 控制台、Google Cloud CLI 或 cbt CLI 手动获取实例详细信息:

列出实例

如需获取实例列表,请执行以下操作:

控制台

  1. 在 Google Cloud 控制台中打开 Bigtable 实例列表。

    打开实例列表

    实例页面会显示实例列表。

gcloud

使用 bigtable instances list 命令查看实例列表:

  gcloud bigtable instances list

cbt

使用 listinstances 命令查看实例列表:

  cbt listinstances

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

获取实例详情

如需获取实例的详细信息,请执行以下操作:

控制台

  1. 在 Google Cloud 控制台中打开 Bigtable 实例列表。

    打开实例列表

  2. 点击要查看其详细信息的实例。

    实例概览页面会显示有关实例的详细信息。

gcloud

使用 bigtable instances describe 命令查看实例的详细信息:

    gcloud bigtable instances describe INSTANCE_ID

替换以下内容:

  • INSTANCE_ID:实例的永久性标识符。

cbt

使用 listclusters 命令查看实例的视图详细信息:

    cbt listclusters INSTANCE_ID

替换以下内容:

  • INSTANCE_ID:实例的永久性标识符。

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::Instance> instance =
      instance_admin.GetInstance(instance_name);
  if (!instance) throw std::move(instance).status();
  std::cout << "GetInstance details : " << instance->DebugString() << "\n";
}

C#

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

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

// Initialize request argument(s).
GetInstanceRequest request = new GetInstanceRequest
{
    InstanceName = new InstanceName(projectId, instanceId)
};
try
{
    // Make Request.
    Console.WriteLine("Waiting for operation to complete...");
    Instance respond = bigtableInstanceAdminClient.GetInstance(request);
}
catch (Exception ex)
{
    Console.WriteLine($"Exception retreiving {instanceId} instance");
    Console.WriteLine(ex.Message);
}

Java

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

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

Instance instance = null;
try {
  instance = adminClient.getInstance(instanceId);
  System.out.println("Instance ID: " + instance.getId());
  System.out.println("Display Name: " + instance.getDisplayName());
  System.out.print("Labels: ");
  Map<String, String> labels = instance.getLabels();
  for (String key : labels.keySet()) {
    System.out.printf("%s - %s", key, labels.get(key));
  }
  System.out.println("\nState: " + instance.getState());
  System.out.println("Type: " + instance.getType());
} catch (NotFoundException e) {
  System.err.println("Failed to get non-existent instance: " + e.getMessage());
}

Node.js

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

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

const [instances2] = await bigtable.instance(instanceID).get();
console.log(`Instance ID: ${instances2.id}`);
console.log(`Instance Meta: ${JSON.stringify(instances2.metadata)}`);

PHP

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

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

use Google\ApiCore\ApiException;
use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\GetInstanceRequest;
use Google\Cloud\Bigtable\Admin\V2\Instance\State;
use Google\Cloud\Bigtable\Admin\V2\Instance\Type;

/**
 * Get a Bigtable instance
 *
 * @param string $projectId The Google Cloud project ID
 * @param string $instanceId The ID of the Bigtable instance
 */
function get_instance(
    string $projectId,
    string $instanceId
): void {
    $instanceAdminClient = new BigtableInstanceAdminClient();
    $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);

    printf('Fetching the Instance %s' . PHP_EOL, $instanceId);
    try {
        $getInstanceRequest = (new GetInstanceRequest())
            ->setName($instanceName);
        $instance = $instanceAdminClient->getInstance($getInstanceRequest);
    } catch (ApiException $e) {
        if ($e->getStatus() === 'NOT_FOUND') {
            printf('Instance %s does not exists.' . PHP_EOL, $instanceId);
            return;
        }
        throw $e;
    }

    printf('Printing Details:' . PHP_EOL);

    // Fetch some commonly used metadata
    printf('Name: ' . $instance->getName() . PHP_EOL);
    printf('Display Name: ' . $instance->getDisplayName() . PHP_EOL);
    printf('State: ' . State::name($instance->getState()) . PHP_EOL);
    printf('Type: ' . Type::name($instance->getType()) . PHP_EOL);
    printf('Labels: ' . PHP_EOL);

    $labels = $instance->getLabels();

    // Labels are an object of the MapField class which implement the IteratorAggregate, Countable
    // and ArrayAccess interfaces so you can do the following:
    printf("\tNum of Labels: " . $labels->count() . PHP_EOL);
    printf("\tLabel with a key(dev-label): " . ($labels['dev-label'] ?? 'N/A') . PHP_EOL);

    // we can even loop over all the labels
    foreach ($labels as $key => $val) {
        printf("\t$key: $val" . PHP_EOL);
    }
}

Python

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

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

print(
    "\nName of instance: {}\nLabels: {}".format(
        instance.display_name, instance.labels
    )
)

Ruby

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

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

# instance_id = "my-instance"
instance = bigtable.instance instance_id
puts "Get Instance id: #{instance.instance_id}"

后续步骤