列出列族

列出列族。

代码示例

C++

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

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

namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::StatusOr;
[](cbta::BigtableTableAdminClient admin, std::string const& project_id,
   std::string const& instance_id, std::string const& table_id) {
  std::string table_name = cbt::TableName(project_id, instance_id, table_id);

  google::bigtable::admin::v2::GetTableRequest r;
  r.set_name(table_name);
  r.set_view(google::bigtable::admin::v2::Table::FULL);

  StatusOr<google::bigtable::admin::v2::Table> schema =
      admin.GetTable(std::move(r));

  if (!schema) throw std::move(schema).status();
  for (auto const& kv : schema->column_families()) {
    std::string const& column_family_name = kv.first;
    google::bigtable::admin::v2::ColumnFamily const& family = kv.second;
    std::cout << "Column family name: " << column_family_name << "\n"
              << "Column family metadata: " << family.DebugString() << "\n";
  }
}

Java

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

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

// Lists all families in the table with GC rules.
try {
  Table table = adminClient.getTable(tableId);
  Collection<ColumnFamily> columnFamilies = table.getColumnFamilies();
  for (ColumnFamily columnFamily : columnFamilies) {
    System.out.printf(
        "Column family: %s%nGC Rule: %s%n",
        columnFamily.getId(), columnFamily.getGCRule().toString());
  }
} catch (NotFoundException e) {
  System.err.println(
      "Failed to list column families from a non-existent table: " + e.getMessage());
}

Node.js

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

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

// List all families in the table with GC rules
const [families] = await table.getFamilies();
// Print ID, GC Rule for each column family
families.forEach(family => {
  const metadata = JSON.stringify(family.metadata);
  console.log(`Column family: ${family.id}, Metadata: ${metadata}`);
  /* Sample output:
      Column family: projects/{{projectId}}/instances/my-instance/tables/my-table/columnFamilies/cf4,
      Metadata: {"gcRule":{"intersection":{"rules":[{"maxAge":{"seconds":"432000","nanos":0},"rule":"maxAge"},{"maxNumVersions":2,"rule":"maxNumVersions"}]},"rule":"intersection"}}
  */
});

PHP

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

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

use Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient;
use Google\Cloud\Bigtable\Admin\V2\GetTableRequest;

/**
 * List column families of a table
 *
 * @param string $projectId The Google Cloud project ID
 * @param string $instanceId The ID of the Bigtable instance
 * @param string $tableId The ID of the table for which the families need to be displayed
 */
function list_column_families(
    string $projectId,
    string $instanceId,
    string $tableId
): void {
    $tableAdminClient = new BigtableTableAdminClient();

    $tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
    $getTableRequest = (new GetTableRequest())
        ->setName($tableName);

    $table = $tableAdminClient->getTable($getTableRequest);
    $columnFamilies = $table->getColumnFamilies()->getIterator();

    foreach ($columnFamilies as $k => $columnFamily) {
        printf('Column Family: %s' . PHP_EOL, $k);
        print('GC Rule:' . PHP_EOL);
        printf('%s' . PHP_EOL, $columnFamily->serializeToJsonString());
    }
}

Python

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

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

print("Printing Column Family and GC Rule for all column families...")
column_families = table.list_column_families()
for column_family_name, gc_rule in sorted(column_families.items()):
    print("Column Family:", column_family_name)
    print("GC Rule:")
    print(gc_rule.to_pb())
    # Sample output:
    #         Column Family: cf4
    #         GC Rule:
    #         gc_rule {
    #           intersection {
    #             rules {
    #               max_age {
    #                 seconds: 432000
    #               }
    #             }
    #             rules {
    #               max_num_versions: 2
    #             }
    #           }
    #         }

Ruby

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

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

table = bigtable.table(
  instance_id,
  table_id,
  view:           :FULL,
  perform_lookup: true
)
table.column_families.each do |name, family|
  puts "Column family name: #{name}"
  puts "GC Rule:"
  p family.gc_rule
end

后续步骤

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