Crear una tabla con una plantilla
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Crea una tabla con las propiedades de una tabla (esquema, partición, agrupamiento en clústeres) para crear una tabla vacía nueva con la misma configuración.
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]