Get table properties
Stay organized with collections
Save and categorize content based on your preferences.
Retrieve the properties of a table for a given table ID.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to retrieve properties of a table in BigQuery using various client libraries like Go, Java, Node.js, PHP, and Python.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples showcase how to fetch table metadata such as schema, description, and the number of rows by providing the project ID, dataset ID, and table ID.\u003c/p\u003e\n"],["\u003cp\u003eEach example includes instructions on setting up authentication using Application Default Credentials and refers to the relevant BigQuery API documentation for the specific language.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code snippets illustrate the process of initializing a BigQuery client and using it to access and print table information.\u003c/p\u003e\n"],["\u003cp\u003eThe provided content gives a link for more information and where to find other code samples within the google cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# Get table properties\n\nRetrieve the properties of a table for a given table ID.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create and use tables](/bigquery/docs/tables)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Go API\nreference documentation](https://godoc.org/cloud.google.com/go/bigquery).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/bigquery\"\n )\n\n // printTableInfo demonstrates fetching metadata from a table and printing some basic information\n // to an io.Writer.\n func printTableInfo(w io.Writer, projectID, datasetID, tableID string) error {\n \t// projectID := \"my-project-id\"\n \t// datasetID := \"mydataset\"\n \t// tableID := \"mytable\"\n \tctx := context.Background()\n \tclient, err := bigquery.NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"bigquery.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tmeta, err := client.Dataset(datasetID).Table(tableID).Metadata(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \t// Print basic information about the table.\n \tfmt.Fprintf(w, \"Schema has %d top-level fields\\n\", len(meta.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Schema))\n \tfmt.Fprintf(w, \"Description: %s\\n\", meta.Description)\n \tfmt.Fprintf(w, \"Rows in managed storage: %d\\n\", meta.NumRows)\n \treturn nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-biglake/latest/com.google.cloud.bigquery.biglake.v1.Table.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.TableId.html;\n\n public class GetTable {\n\n public static void main(String[] args) {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"bigquery_public_data\";\n String datasetName = \"samples\";\n String tableName = \"shakespeare\";\n getTable(projectId, datasetName, tableName);\n }\n\n public static void getTable(String projectId, String datasetName, String tableName) {\n try {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigquery = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.getDefaultInstance().getService();\n\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.TableId.html tableId = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.TableId.html.of(projectId, datasetName, tableName);\n https://cloud.google.com/java/docs/reference/google-cloud-biglake/latest/com.google.cloud.bigquery.biglake.v1.Table.html table = bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_getTable_com_google_cloud_bigquery_TableId_com_google_cloud_bigquery_BigQuery_TableOption____(tableId);\n System.out.println(\"Table info: \" + table.getDescription());\n } catch (https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html e) {\n System.out.println(\"Table not retrieved. \\n\" + e.toString());\n }\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Node.js API\nreference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n // Import the Google Cloud client library\n const {BigQuery} = require('https://cloud.google.com/nodejs/docs/reference/bigquery/latest/overview.html');\n const bigquery = new https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html();\n\n async function getTable() {\n // Retrieves table named \"my_table\" in \"my_dataset\".\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample\n */\n // const datasetId = \"my_dataset\";\n // const tableId = \"my_table\";\n\n // Retrieve table reference\n const dataset = bigquery.dataset(datasetId);\n const [table] = await dataset.table(tableId).get();\n\n console.log('Table:');\n console.log(table.metadata.tableReference);\n }\n getTable();\n\n### PHP\n\n\nBefore trying this sample, follow the PHP setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery PHP API\nreference documentation](/php/docs/reference/cloud-bigquery/latest/BigQueryClient).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n use Google\\Cloud\\BigQuery\\BigQueryClient;\n\n /** Uncomment and populate these variables in your code */\n //$projectId = 'The Google project ID';\n //$datasetId = 'The BigQuery dataset ID';\n //$tableId = 'The BigQuery table ID';\n\n $bigQuery = new BigQueryClient([\n 'projectId' =\u003e $projectId,\n ]);\n $dataset = $bigQuery-\u003edataset($datasetId);\n $table = $dataset-\u003etable($tableId);\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Python API\nreference documentation](/python/docs/reference/bigquery/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n\n # Construct a BigQuery client object.\n client = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html()\n\n # TODO(developer): Set table_id to the ID of the model to fetch.\n # table_id = 'your-project.your_dataset.your_table'\n\n table = client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_get_table(table_id) # Make an API request.\n\n # View table properties\n print(\n \"Got table '{}.{}.{}'.\".format(table.project, table.dataset_id, table.table_id)\n )\n print(\"Table schema: {}\".format(table.schema))\n print(\"Table description: {}\".format(table.description))\n print(\"Table has {} rows\".format(table.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.table.Table.html#google_cloud_bigquery_table_Table_num_rows))\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]