Get dataset properties Stay organized with collections Save and categorize content based on your preferences.
Retrieve the properties of a dataset.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Go
Before trying this sample, follow theGo setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryGo API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
import("context""fmt""io""cloud.google.com/go/bigquery""google.golang.org/api/iterator")// printDatasetInfo demonstrates fetching dataset metadata and printing some of it to an io.Writer.funcprintDatasetInfo(wio.Writer,projectID,datasetIDstring)error{// projectID := "my-project-id"// datasetID := "mydataset"ctx:=context.Background()client,err:=bigquery.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("bigquery.NewClient: %w",err)}deferclient.Close()meta,err:=client.Dataset(datasetID).Metadata(ctx)iferr!=nil{returnerr}fmt.Fprintf(w,"Dataset ID: %s\n",datasetID)fmt.Fprintf(w,"Description: %s\n",meta.Description)fmt.Fprintln(w,"Labels:")fork,v:=rangemeta.Labels{fmt.Fprintf(w,"\t%s: %s",k,v)}fmt.Fprintln(w,"Tables:")it:=client.Dataset(datasetID).Tables(ctx)cnt:=0for{t,err:=it.Next()iferr==iterator.Done{break}cnt++fmt.Fprintf(w,"\t%s\n",t.TableID)}ifcnt==0{fmt.Fprintln(w,"\tThis dataset does not contain any tables.")}returnnil}Java
Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
importcom.google.api.gax.paging.Page;importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQuery.TableListOption;importcom.google.cloud.bigquery.BigQueryException;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.cloud.bigquery.Dataset;importcom.google.cloud.bigquery.DatasetId;importcom.google.cloud.bigquery.Table;publicclassGetDatasetInfo{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringprojectId="MY_PROJECT_ID";StringdatasetName="MY_DATASET_NAME";getDatasetInfo(projectId,datasetName);}publicstaticvoidgetDatasetInfo(StringprojectId,StringdatasetName){try{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.BigQuerybigquery=BigQueryOptions.getDefaultInstance().getService();DatasetIddatasetId=DatasetId.of(projectId,datasetName);Datasetdataset=bigquery.getDataset(datasetId);// View dataset propertiesStringdescription=dataset.getDescription();System.out.println(description);// View tables in the dataset// For more information on listing tables see:// https://javadoc.io/static/com.google.cloud/google-cloud-bigquery/0.22.0-beta/com/google/cloud/bigquery/BigQuery.htmlPage<Table>tables=bigquery.listTables(datasetName,TableListOption.pageSize(100));tables.iterateAll().forEach(table->System.out.print(table.getTableId().getTable()+"\n"));System.out.println("Dataset info retrieved successfully.");}catch(BigQueryExceptione){System.out.println("Dataset info not retrieved. \n"+e.toString());}}}Node.js
Before trying this sample, follow theNode.js setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryNode.js API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
// Import the Google Cloud client libraryconst{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();asyncfunctiongetDataset(){// Retrieves dataset named "my_dataset"./** * TODO(developer): Uncomment the following lines before running the sample */// const datasetId = "my_dataset";// Retrieve dataset referenceconst[dataset]=awaitbigquery.dataset(datasetId).get();console.log('Dataset:');console.log(dataset.metadata.datasetReference);}getDataset();Python
Before trying this sample, follow thePython setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPython API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
fromgoogle.cloudimportbigquery# Construct a BigQuery client object.client=bigquery.Client()# TODO(developer): Set dataset_id to the ID of the dataset to fetch.# dataset_id = 'your-project.your_dataset'dataset=client.get_dataset(dataset_id)# Make an API request.full_dataset_id="{}.{}".format(dataset.project,dataset.dataset_id)friendly_name=dataset.friendly_nameprint("Got dataset '{}' with friendly_name '{}'.".format(full_dataset_id,friendly_name))# View dataset properties.print("Description:{}".format(dataset.description))print("Labels:")labels=dataset.labelsiflabels:forlabel,valueinlabels.items():print("\t{}:{}".format(label,value))else:print("\tDataset has no labels defined.")# View tables in dataset.print("Tables:")tables=list(client.list_tables(dataset))# Make an API request(s).iftables:fortableintables:print("\t{}".format(table.table_id))else:print("\tThis dataset does not contain any tables.")What's next
To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.