Manage clustered tables
After you create a clustered table in BigQuery, you can control who can access it, review its properties, and change its clustering columns as your query patterns evolve. This document describes how to control access to clustered tables, get information about them, list the clustered tables in a dataset, and modify the clustering specification.
To learn about clustered tables, see Introduction to clustered tables. To create one, see Create clustered tables.
Before you begin
To get information about tables, you must have the
bigquery.tables.get permission. The following predefined IAM
roles include bigquery.tables.get permissions:
roles/bigquery.metadataViewerroles/bigquery.dataViewerroles/bigquery.dataOwnerroles/bigquery.dataEditorroles/bigquery.admin
In addition, if a user has the bigquery.datasets.create permission, when that
user creates a dataset, they are granted bigquery.dataOwner access to it.
bigquery.dataOwner access gives the user the ability to get information about
tables in a dataset.
For more information about IAM roles and permissions in BigQuery, see Predefined roles and permissions.
Control access to clustered tables
To configure access to tables and views, you can grant a predefined or custom IAM role to a principal on one of the following resources:
- A Resource Manager resource, such as a project, folder, or organization. All tables and views in the resource inherit the roles granted on the resource. For example, if you grant a role to a principal on a project, that principal has permissions that apply to all datasets, tables, and views in the project. For more information about Resource Manager resources, see About resource hierarchy.
- A dataset. All tables and views in the dataset inherit the roles granted on the dataset.
- A table or view. The roles granted on the table or view apply only to that specific table or view.
For more information about configuring access to BigQuery resources, see Control access to resources with IAM.
You can also restrict data access within tables, by using the following methods:
Get information about clustered tables
Select one of the following options:
Console
In the Google Cloud console, go to the Explorer pane.
Expand your project and dataset, and then click the table that you want to view.
Click the Details tab.
The table's details are displayed, including the clustering columns.
SQL
For clustered tables, you can query the CLUSTERING_ORDINAL_POSITION column
in the INFORMATION_SCHEMA.COLUMNS view
to find the 1-indexed offset of the column within the table's clustering
columns:
In the Google Cloud console, go to the BigQuery page.
In the query editor, enter the following statement:
CREATE TABLE mydataset.data (column1 INT64, column2 INT64) CLUSTER BY column1, column2; SELECT column_name, clustering_ordinal_position FROM mydataset.INFORMATION_SCHEMA.COLUMNS;
Click Run.
For more information about how to run queries, see Run an interactive query.
The clustering ordinal position is 1 for column1 and 2 for column2.
More table metadata is available through the TABLES, TABLE_OPTIONS,
COLUMNS, and COLUMN_FIELD_PATH views in INFORMATION_SCHEMA.
bq
Issue the bq show command to display all table information. Use the
--schema flag to display only table schema information. The --format
flag can be used to control the output.
If you are getting information about a table in a project other than
your default project, add the project ID to the dataset in the following
format: project_id:dataset.
bq show \ --schema \ --format=prettyjson \ PROJECT_ID:DATASET.TABLE
Replace the following:
PROJECT_ID: your project IDDATASET: the name of the datasetTABLE: the name of the table
Examples:
Enter the following command to display all information about
myclusteredtable in mydataset. mydataset in your default project.
bq show --format=prettyjson mydataset.myclusteredtable
The output should look like the following:
{
"clustering": {
"fields": [
"customer_id"
]
},
...
}
API
Call the bigquery.tables.get
method and provide any relevant parameters.
List clustered tables in a dataset
The permissions required to list clustered tables and the steps to list them are the same as for standard tables. For more information, see Listing tables in a dataset.
Modify the clustering specification
You can change or remove a table's clustering specifications, or change the set of clustered columns in a clustered table. This method of updating the clustering column set is useful for tables that use continuous streaming inserts because those tables cannot be easily swapped by other methods.
Follow these steps to apply a new clustering specification to unpartitioned or partitioned tables.
In the bq tool, update the clustering specification of your table to match the new clustering:
bq update --clustering_fields=CLUSTER_COLUMN DATASET.ORIGINAL_TABLE
Replace the following:
CLUSTER_COLUMN: the column you are clustering on—for example,mycolumnDATASET: the name of the dataset containing the table—for example,mydatasetORIGINAL_TABLE: the name of your original table—for example,mytable
You can also call the
tables.updateortables.patchAPI method to modify the clustering specification.To cluster all rows according to the new clustering specification, run the following
UPDATEstatement:UPDATE DATASET.ORIGINAL_TABLE SET CLUSTER_COLUMN=CLUSTER_COLUMN WHERE true
What's next
- For information about querying clustered tables, see Query clustered tables.
- For an overview of partitioned table support in BigQuery, see Introduction to partitioned tables.
- To learn how to create partitioned tables, see Create partitioned tables.