Répertorier les familles de colonnes

Répertorier les familles de colonnes.

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::TableAdmin admin, std::string const& table_id) {
  StatusOr<google::bigtable::admin::v2::Table> schema =
      admin.GetTable(table_id, cbt::TableAdmin::FULL);

  if (!schema) throw std::runtime_error(schema.status().message());
  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

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

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

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\BigtableTableAdminClient;

/**
 * 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);

    $table = $tableAdminClient->getTable($tableName);
    $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

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

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

Étape suivante

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