Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Ce document explique comment charger une petite quantité de données au format de fichier CSV dans Spanner. Vous pouvez charger des exemples de données avant d'effectuer une migration de données de production pour tester les schémas, les requêtes et les applications.
Pour obtenir les autorisations nécessaires à l'exportation des données BigQuery vers Spanner, demandez à votre administrateur de vous accorder les rôles IAM suivants sur votre projet :
Pour exporter des données depuis une table BigQuery :
Lecteur de données BigQuery (roles/bigquery.dataViewer)
Pour exécuter un job d'exportation :
Utilisateur BigQuery (roles/bigquery.user)
Écrire des données dans une table Spanner :
Utilisateur de base de données Spanner (roles/spanner.databaseUser)
Définissez un projet par défaut dans gcloud CLI à l'aide de la commande suivante :
gcloudconfigsetprojectPROJECT_ID
Exportez les données sources au format de fichier CSV. Envisagez d'utiliser pg_dump pour les bases de données PostgreSQL ou mysqldump pour les bases de données MySQL afin de convertir vos exemples de données au format de fichier CSV.
Chargez les données dans BigQuery à l'aide des commandes bq suivantes :
Créez un ensemble de données BigQuery.
bqmkBQ_DATASET
Chargez les données par lot dans une nouvelle table BigQuery.
Exportez les données de BigQuery vers Spanner à l'aide de la commande suivante :
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;'
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.
Dernière mise à jour le 2025/09/05 (UTC).
[[["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"]],["Dernière mise à jour le 2025/09/05 (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)"]]