Filtering resources using labels
To filter resources using labels, you can do one of the following:
- Use the search bar in the Google Cloud console.
- Create a filter specification for use in the API, bq command-line tool, or client libraries.
Limitations
- The API, bq command-line tool, and client libraries support filtering only for datasets.
- You cannot filter jobs by label in any of the BigQuery tools.
Before you begin
Grant Identity and Access Management (IAM) roles that give users the necessary permissions to perform each task in this document.
Required permissions
To filter resources using labels, you must be able to retrieve resource metadata. To filter resources using labels, you need the following IAM permissions:
bigquery.datasets.get
(lets you filter datasets)bigquery.tables.get
(lets you filter tables and views)
Each of the following predefined IAM roles includes the permissions that you need in order to filter datasets:
roles/bigquery.user
roles/bigquery.metadataViewer
roles/bigquery.dataViewer
roles/bigquery.dataOwner
roles/bigquery.dataEditor
roles/bigquery.admin
Each of the following predefined IAM roles includes the permissions that you need in order to filter tables and views:
roles/bigquery.metadataViewer
roles/bigquery.dataViewer
roles/bigquery.dataOwner
roles/bigquery.dataEditor
roles/bigquery.admin
Additionally, if you have the bigquery.datasets.create
permission, you can filter the resources that you create.
For more information on IAM roles and permissions in BigQuery, see Predefined roles and permissions.
Filtering resources in the Google Cloud console
To generate a filtered list of resources, use the Google Cloud console:
In the Google Cloud console, go to the Explorer pane.
In the search bar, enter the
key
orkey:value
pair. Your results include any partial matches.For example, to show only datasets with the label
department:shipping
, you can enterdepartment
ordepartment:shipping
.
Filtering datasets in the API or bq command-line tool
Currently, the API, bq command-line tool, and client libraries support filtering only for datasets.
To filter datasets by using the API, bq tool, or client libraries, create a filter specification and use the specification:
- As the parameter for the
--filter
flag in the bq tool - As the value for the
filter
property in the API'sdatasets.list
method
Limitations on filter specifications
Filter specifications have the following limitations:
- Only the
AND
logical operator is supported. Space-separated comparisons are treated as having implicitAND
operators. - The only field currently eligible for filtering is
labels.key
wherekey
is the name of a label. - The filter can include up to ten expressions.
- Filtering is case-sensitive.
- Currently, the API, bq command-line tool, and client libraries support filtering only for datasets.
Filter specification examples
A filter specification uses the following syntax:
"field[:value][ field[:value]]..."
Replace the following:
field
is expressed aslabels.key
where key is a label key.value
is an optional label value.
The following examples show how to generate filter expressions.
To list resources that have a department:shipping
label, use the following
filter specification:
labels.department:shipping
To list resources using multiple labels, separate the key:value
pairs with a
space. The space is treated as a logical AND
operator. For example, to list
datasets with the department:shipping
label and the location:usa
label,
use the following filter specification:
labels.department:shipping labels.location:usa
You can filter on the presence of a key alone, rather than matching against a
key:value pair. The following filter specification lists all datasets
labeled department
regardless of the value.
labels.department
An equivalent filter specification uses an asterisk to represent all possible
values associated with the department
key.
labels.department:*
You can also use tags in a filter specification. For example, to list resources
with the department:shipping
label and test_data
tag, use the following
filter specification:
labels.department:shipping labels.test_data
Filtering datasets in the bq command-line tool and the API
To filter datasets by using the API, bq command-line tool, or client libraries:
bq
Issue the bq ls
command with the --filter
flag. If you are listing
datasets in a project other than your default project, specify the
--project_id
flag.
bq ls \ --filter "filter_specification" \ --project_id project_id
Replace the following:
filter_specification
is a valid filter specification.project_id
is your project ID.
Examples:
Enter the following command to list datasets in your default project that
have a department:shipping
label:
bq ls --filter "labels.department:shipping"
Enter the following command to list datasets in your default project that
have a department:shipping
label and a test_data
tag.
bq ls --filter "labels.department:shipping labels.test_data"
Enter the following command to list datasets in myotherproject
that have
a department:shipping
label:
bq ls --filter "labels.department:shipping" --project_id myotherproject
The output for each of these commands returns a list of datasets like the following.
+-----------+ | datasetId | +-----------+ | mydataset | | mydataset2| +-----------+
API
Call the datasets.list
API method and provide the filter specification using the filter
property.
Go
Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Go API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Java
Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Node.js
Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Python
Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
What's next
- Learn how to add labels to BigQuery resources.
- Learn how to view labels on BigQuery resources.
- Learn how to update labels on BigQuery resources.
- Learn how to delete labels on BigQuery resources.
- Read about Using labels in the Resource Manager documentation.