Créer une table à l'aide d'un modèle
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Créez une table à l'aide des propriétés d'une table (schéma, partitionnement, clustering) pour créer une table vide avec la même configuration.
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","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)."]]