Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questo documento fornisce istruzioni su come caricare una piccola quantità di dati in
Spanner nel formato di file CSV. Puoi caricare dati di esempio prima di eseguire una migrazione dei dati di produzione per testare schemi, query e applicazioni.
Per ottenere le autorizzazioni
necessarie per esportare i dati BigQuery in Spanner,
chiedi all'amministratore di concederti i
seguenti ruoli IAM nel progetto:
Esporta i dati da una tabella BigQuery:
Visualizzatore dati BigQuery (roles/bigquery.dataViewer)
Esegui un job di esportazione:
Utente BigQuery (roles/bigquery.user)
Scrivi dati in una tabella Spanner:
Utente database Spanner (roles/spanner.databaseUser)
Imposta un progetto predefinito in gcloud CLI utilizzando il seguente comando:
gcloudconfigsetprojectPROJECT_ID
Esporta i dati di origine nel formato di file CSV. Valuta la possibilità di utilizzare
pg_dump per
i database PostgreSQL o
mysqldump
per i database MySQL per convertire i dati di esempio nel formato di file CSV.
Carica i dati in BigQuery utilizzando i seguenti comandi bq:
Creare un set di dati BigQuery.
bqmkBQ_DATASET
Carica in batch i dati in una nuova tabella BigQuery.
Esporta i dati da BigQuery a Spanner utilizzando
il seguente comando:
bq--use_legacy_sql=false'EXPORT DATA OPTIONS( uri="https://spanner.googleapis.com/projects/PROJECT_ID/instances/SPANNER_INSTANCE/databases/SPANNER_DATABASE" format='CLOUD_SPANNER' spanner_options="""{ "table": "SPANNER_TABLE" }""" ) AS SELECT * FROM BQ_DATASET.BQ_TABLE;'
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-08-11 UTC."],[],[],null,["# Load sample data\n\nThis document provides instructions on how to load a small amount of data in\nthe CSV file format into Spanner. You can load sample data before\nperforming a production data migration to test schemas, queries, and applications.\n\nBefore you begin\n----------------\n\n1. [Install the Google Cloud CLI](/sdk/docs/install) or\n use [Cloud Shell](/shell/docs/launching-cloud-shell), which\n has all the necessary tools pre-installed.\n\n2.\n\n To get the permissions that\n you need to export BigQuery data to Spanner,\n\n ask your administrator to grant you the\n following IAM roles on your project:\n\n - Export data from a BigQuery table: BigQuery Data Viewer (`roles/bigquery.dataViewer`)\n - Run an export job: BigQuery User (`roles/bigquery.user`)\n - Write data to a Spanner table: Spanner Database User (`roles/spanner.databaseUser`)\n\n\n For more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\n You might also be able to get\n the required permissions through [custom\n roles](/iam/docs/creating-custom-roles) or other [predefined\n roles](/iam/docs/roles-overview#predefined).\n\nLoad sample data to Spanner\n---------------------------\n\nThe following instructions are performed using the\n[BigQuery reverse ETL](/bigquery/docs/export-to-spanner)\nworkflow and the [Google Cloud CLI](/sdk).\n\n1. Set a default project on the gcloud CLI using the following command:\n\n ```bash\n gcloud config set project PROJECT_ID\n ```\n2. Export the source data in the CSV file format. Consider using\n [`pg_dump`](https://www.postgresql.org/docs/current/app-pgdump.html) for\n PostgreSQL databases or\n [`mysqldump`](https://dev.mysql.com/doc/refman/8.4/en/mysqldump.html)\n for MySQL databases tools to convert your sample data into the CSV\n file format.\n\n | **Note:** If you are working with sample data that's not available in the CSV file format, then consider [batch loading](/bigquery/docs/batch-loading-data) the sample data to BigQuery.\n3. Load the data into BigQuery by using the following `bq` commands:\n\n 1. Create a BigQuery dataset.\n\n ```bash\n bq mk BQ_DATASET\n ```\n 2. Batch load the data into a new BigQuery table.\n\n ```bash\n bq load \\\n --source_format=CSV \\\n --autodetect \\\n --allow_quoted_newlines \\\n BQ_DATASET.BQ_TABLE /path/to/file\n ```\n\n Alternatively, you can batch load the data from a Cloud Storage file. \n\n ```bash\n bq load \\\n --source_format=CSV \\\n --autodetect \\\n --allow_quoted_newlines \\\n BQ_DATASET.BQ_TABLE gs://BUCKET/FILE\n ```\n4. Create a Spanner schema that matches the imported\n \u003cvar translate=\"no\"\u003eBQ_TABLE\u003c/var\u003e by using the following command:\n\n ```bash\n gcloud spanner databases ddl update SPANNER_DATABASE \\\n --instance=SPANNER_INSTANCE \\\n --ddl=\"CREATE TABLE \u003cvar translate=\"no\"\u003eSPANNER_TABLE\u003c/var\u003e ...\"\n ```\n\n For more information,\n see [Update Spanner schema](/spanner/docs/getting-started/gcloud#create-database).\n5. Export data from BigQuery to Spanner by using\n the following command:\n\n \u003cbr /\u003e\n\n ```bash\n bq --use_legacy_sql=false 'EXPORT DATA OPTIONS(\n uri=\"https://spanner.googleapis.com/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/instances/\u003cvar translate=\"no\"\u003eSPANNER_INSTANCE\u003c/var\u003e/databases/\u003cvar translate=\"no\"\u003eSPANNER_DATABASE\u003c/var\u003e\"\n format='CLOUD_SPANNER'\n spanner_options=\"\"\"{ \"table\": \"\u003cvar translate=\"no\"\u003eSPANNER_TABLE\u003c/var\u003e\" }\"\"\"\n ) AS\n SELECT *\n FROM \u003cvar translate=\"no\"\u003eBQ_DATASET\u003c/var\u003e.\u003cvar translate=\"no\"\u003eBQ_TABLE\u003c/var\u003e;'\n \n ```\n\n \u003cbr /\u003e\n\nWhat's next\n-----------\n\n- [Migration overview](/spanner/docs/migration-overview)"]]