Create a table using a template
Stay organized with collections
Save and categorize content based on your preferences.
Create a table using the properties of one table (schema, partitioning, clustering) to create a new empty table with the same configuration.
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 Go code demonstrates how to create a new empty table in BigQuery that mirrors the schema, partitioning, and clustering configuration of an existing source table.\u003c/p\u003e\n"],["\u003cp\u003eThe code utilizes the \u003ccode\u003ebigquery\u003c/code\u003e package in Go to fetch metadata from a source table, including its schema, clustering, and partitioning properties.\u003c/p\u003e\n"],["\u003cp\u003eThe code constructs new table metadata for the destination table, incorporating the relevant properties copied from the source table's metadata.\u003c/p\u003e\n"],["\u003cp\u003eThe example then creates a new table using the properties that were defined, including the description, schema, clustering, and partitioning.\u003c/p\u003e\n"]]],[],null,["# Create a table using a template\n\nCreate a table using the properties of one table (schema, partitioning, clustering) to create a new empty table with the same configuration.\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\n \t\"cloud.google.com/go/bigquery\"\n )\n\n // createTableFromTemplateTable demonstrates how to use the properties of one\n // table (schema, partitioning, clustering) to create a new empty table with\n // the same configuration.\n func createTableFromTemplateTable(srcProjectID, srcDatasetID, srcTableID, dstProjectID, dstDatasetID, dstTableID string) error {\n \t// srcProjectID := \"bigquery-public-data\"\n \t// srcDatasetID := \"samples\"\n \t// srcTableID := \"shakespeare\"\n \t// dstProjectID := \"my-project-id\"\n \t// dstDatasetID := \"mydataset\"\n \t// dstTableID := \"mytable\"\n \tctx := context.Background()\n\n \t// We'll construct the client based on the destination project.\n \tclient, err := bigquery.NewClient(ctx, dstProjectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"bigquery.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tsrcTableRef := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Client_DatasetInProject(srcProjectID, srcDatasetID).Table(srcTableID)\n\n \tsrcMeta, err := srcTableRef.Metadata(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to get source table metadata: %w\", err)\n \t}\n\n \tdstTableRef := client.Dataset(dstDatasetID).Table(dstTableID)\n\n \t// We'll use some (but not all) of the metadata from the source table\n \t// to define the destination table. Other properties to consider include\n \t// attributes like expiration policy and managed encryption settings.\n \tdstMeta := &bigquery.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_TableMetadata{\n \t\tDescription: fmt.Sprintf(\"table structure copied from %s.%s.%s\",\n \t\t\tsrcTableRef.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Job_ProjectID, srcTableRef.DatasetID, srcTableRef.TableID),\n \t\tSchema: srcMeta.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Schema,\n \t\tClustering: srcMeta.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Clustering,\n \t\t// A table will only have one partitioning configuration, the others will be empty/nil.\n \t\tTimePartitioning: srcMeta.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_TimePartitioning,\n \t\tRangePartitioning: srcMeta.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_RangePartitioning,\n \t\t// Retain the same enforcement of partition filtering as well.\n \t\tRequirePartitionFilter: srcMeta.RequirePartitionFilter,\n \t}\n\n \tif err := dstTableRef.Create(ctx, dstMeta); err != nil {\n \t\treturn err\n \t}\n\n \treturn nil\n }\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)."]]