List tables

List all tables in a Cloud Bigtable instance.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::StreamRange;
[](cbta::BigtableTableAdminClient admin, std::string const& project_id,
   std::string const& instance_id) {
  std::string instance_name = cbt::InstanceName(project_id, instance_id);

  google::bigtable::admin::v2::ListTablesRequest r;
  r.set_parent(instance_name);
  r.set_view(google::bigtable::admin::v2::Table::NAME_ONLY);

  StreamRange<google::bigtable::admin::v2::Table> tables =
      admin.ListTables(std::move(r));
  for (auto& table : tables) {
    if (!table) throw std::move(table).status();
    std::cout << table->name() << "\n";
  }
}

C#

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Lists tables in intance.
// Initialize request argument(s).
ListTablesRequest request = new ListTablesRequest
{
    ParentAsInstanceName = s_instanceName
};
try
{
    // Make the request.
    PagedEnumerable<ListTablesResponse, Table> response = bigtableTableAdminClient.ListTables(request);

}
catch (Exception ex)
{
    Console.WriteLine($"Error listing tables {ex.Message}");
}

Java

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Lists tables in the current instance.
try {
  List<String> tableIds = adminClient.listTables();
  for (String tableId : tableIds) {
    System.out.println(tableId);
  }
} catch (NotFoundException e) {
  System.err.println("Failed to list tables from a non-existent instance: " + e.getMessage());
}

Node.js

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// List tables in current project
const [tables] = await instance.getTables();
tables.forEach(table => {
  console.log(table.id);
});

PHP

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient;
use Google\Cloud\Bigtable\Admin\V2\ListTablesRequest;

/**
 * List tables in an instance
 *
 * @param string $projectId The Google Cloud project ID
 * @param string $instanceId The ID of the Bigtable instance
 */
function list_tables(
    string $projectId,
    string $instanceId
): void {
    $instanceAdminClient = new BigtableInstanceAdminClient();
    $tableAdminClient = new BigtableTableAdminClient();

    $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);

    printf('Listing Tables:' . PHP_EOL);
    $listTablesRequest = (new ListTablesRequest())
        ->setParent($instanceName);
    $tables = $tableAdminClient->listTables($listTablesRequest)->iterateAllElements();
    $tables = iterator_to_array($tables);
    if (empty($tables)) {
        print('No table exists.' . PHP_EOL);
        return;
    }
    foreach ($tables as $table) {
        print($table->getName() . PHP_EOL);
    }
}

Python

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

tables = instance.list_tables()
print("Listing tables in current project...")
if tables != []:
    for tbl in tables:
        print(tbl.table_id)
else:
    print("No table exists in current project...")

Ruby

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

# instance_id = "my-instance"
bigtable.tables(instance_id).all.each do |t|
  puts "Table: #{t.name}"
end

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.