The Cloud Storage Avro files to Spanner template is a batch pipeline that reads Avro files exported from Spanner stored in Cloud Storage and imports them to a Spanner database.
Pipeline requirements
- The target Spanner database must exist and must be empty.
- You must have read permissions for the Cloud Storage bucket and write permissions for the target Spanner database.
- The input Cloud Storage path must exist, and it must include a
spanner-export.json
file that contains a JSON description of files to import. - If the source Avro file doesn't contain a primary key, you must create an empty Spanner table with a primary key before you run the template. This step isn't required if the Avro file defines the primary key.
Template parameters
Required parameters
- instanceId: The instance ID of the Spanner database.
- databaseId: The database ID of the Spanner database.
- inputDir: The Cloud Storage path where the Avro files are imported from.
Optional parameters
- spannerHost: The Cloud Spanner endpoint to call in the template. Only used for testing. For example,
https://batch-spanner.googleapis.com
. Defaults to: https://batch-spanner.googleapis.com. - waitForIndexes: If
true
, the pipeline waits for indexes to be created. Iffalse
, the job might complete while indexes are still being created in the background. The default value isfalse
. - waitForForeignKeys: If
true
, the pipeline waits for foreign keys to be created. Iffalse
, the job might complete while foreign keys are still being created in the background. The default value isfalse
. - waitForChangeStreams: If
true
, the pipeline waits for change streams to be created. Iffalse
, the job might complete while change streams are still being created in the background. The default value istrue
. - waitForSequences: By default, the import pipeline is blocked on sequence creation. If
false
, the import pipeline might complete with sequences still being created in the background. - earlyIndexCreateFlag: Specifies whether early index creation is enabled. If the template runs a large number of DDL statements, it's more efficient to create indexes before loading data. Therefore, the default behavior is to create the indexes first when the number of DDL statements exceeds a threshold. To disable this feature, set
earlyIndexCreateFlag
tofalse
. The default value istrue
. - spannerProjectId: The ID of the Google Cloud project that contains the Spanner database. If not set, the default Google Cloud project is used.
- ddlCreationTimeoutInMinutes: The timeout in minutes for DDL statements performed by the template. The default value is 30 minutes.
- spannerPriority: The request priority for Spanner calls. Possible values are
HIGH
,MEDIUM
, andLOW
. The default value isMEDIUM
.
Run the template
Console
- Go to the Dataflow Create job from template page. Go to Create job from template
- In the Job name field, enter a unique job name.
For the job to show up in the Spanner Instances page of the Google Cloud console, the job name must match the following format:
cloud-spanner-import-SPANNER_INSTANCE_ID-SPANNER_DATABASE_NAME
Replace the following:
SPANNER_INSTANCE_ID
: your Spanner instance's IDSPANNER_DATABASE_NAME
: your Spanner database's name
- Optional: For Regional endpoint, select a value from the drop-down menu. The default
region is
us-central1
.For a list of regions where you can run a Dataflow job, see Dataflow locations.
- From the Dataflow template drop-down menu, select the Avro Files on Cloud Storage to Cloud Spanner template.
- In the provided parameter fields, enter your parameter values.
- Click Run job.
gcloud
In your shell or terminal, run the template:
gcloud dataflow jobs run JOB_NAME \ --gcs-location gs://dataflow-templates-REGION_NAME/VERSION/GCS_Avro_to_Cloud_Spanner \ --region REGION_NAME \ --staging-location GCS_STAGING_LOCATION \ --parameters \ instanceId=INSTANCE_ID,\ databaseId=DATABASE_ID,\ inputDir=GCS_DIRECTORY
Replace the following:
JOB_NAME
: a unique job name of your choiceVERSION
: the version of the template that you want to useYou can use the following values:
latest
to use the latest version of the template, which is available in the non-dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/latest/- the version name, like
2023-09-12-00_RC00
, to use a specific version of the template, which can be found nested in the respective dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/
REGION_NAME
: the region where you want to deploy your Dataflow job—for example,us-central1
INSTANCE_ID
: the ID of the Spanner instance that contains the databaseDATABASE_ID
: the ID of the Spanner database to import toGCS_DIRECTORY
: the Cloud Storage path where the Avro files are imported from, for example,gs://mybucket/somefolder
API
To run the template using the REST API, send an HTTP POST request. For more information on the
API and its authorization scopes, see
projects.templates.launch
.
POST https://dataflow.googleapis.com/v1b3/projects/PROJECT_ID/locations/LOCATION/templates:launch?gcsPath=gs://dataflow-templates-LOCATION/VERSION/GCS_Avro_to_Cloud_Spanner { "jobName": "JOB_NAME", "parameters": { "instanceId": "INSTANCE_ID", "databaseId": "DATABASE_ID", "inputDir": "gs://GCS_DIRECTORY" }, "environment": { "machineType": "n1-standard-2" } }
Replace the following:
PROJECT_ID
: the Google Cloud project ID where you want to run the Dataflow jobJOB_NAME
: a unique job name of your choiceVERSION
: the version of the template that you want to useYou can use the following values:
latest
to use the latest version of the template, which is available in the non-dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/latest/- the version name, like
2023-09-12-00_RC00
, to use a specific version of the template, which can be found nested in the respective dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/
LOCATION
: the region where you want to deploy your Dataflow job—for example,us-central1
INSTANCE_ID
: the ID of the Spanner instance that contains the databaseDATABASE_ID
: the ID of the Spanner database to import toGCS_DIRECTORY
: the Cloud Storage path where the Avro files are imported from, for example,gs://mybucket/somefolder
What's next
- Learn about Dataflow templates.
- See the list of Google-provided templates.