The Spanner to Avro Files on Cloud Storage template is a batch pipeline that exports a whole Spanner database to Cloud Storage in Avro format. Exporting a Spanner database creates a folder in the bucket you select. The folder contains:
- A
spanner-export.json
file. - A
TableName-manifest.json
file for each table in the database you exported. - One or more
TableName.avro-#####-of-#####
files.
For example, exporting a database with two tables, Singers
and Albums
,
creates the following file set:
Albums-manifest.json
Albums.avro-00000-of-00002
Albums.avro-00001-of-00002
Singers-manifest.json
Singers.avro-00000-of-00003
Singers.avro-00001-of-00003
Singers.avro-00002-of-00003
spanner-export.json
Pipeline requirements
- The Spanner database must exist.
- The output Cloud Storage bucket must exist.
- In addition to the Identity and Access Management (IAM) roles necessary to run Dataflow jobs, you must also have the appropriate IAM roles for reading your Spanner data and writing to your Cloud Storage bucket.
Template parameters
Required parameters
- instanceId : The instance ID of the Cloud Spanner database that you want to export.
- databaseId : The database ID of the Cloud Spanner database that you want to export.
- outputDir : The Cloud Storage path where the Avro files should be exported to. A new directory will be created under this path that contains the export. (Example: gs://your-bucket/your-path).
Optional parameters
- avroTempDirectory : The Cloud Storage path where the temporary Avro files can be created. Ex: gs://your-bucket/your-path.
- spannerHost : The Cloud Spanner endpoint to call in the template. Only used for testing. (Example: https://batch-spanner.googleapis.com). Defaults to: https://batch-spanner.googleapis.com.
- snapshotTime : Specifies the snapshot time as RFC 3339 format in UTC time without the timezone offset(always ends in 'Z'). Timestamp must be in the past and Maximum timestamp staleness applies. See https://cloud.google.com/spanner/docs/timestamp-bounds#maximum_timestamp_staleness (Example: 1990-12-31T23:59:59Z). Defaults to empty.
- spannerProjectId : The project ID of the Cloud Spanner instance.
- shouldExportTimestampAsLogicalType : If true, Timestamps are exported as timestamp-micros type. Timestamps are exported as ISO8601 strings at nanosecond precision by default.
- tableNames : If provided, only this comma separated list of tables are exported. Ancestor tables and tables that are referenced via foreign keys are required. If not explicitly listed, the
shouldExportRelatedTables
flag must be set for a successful export. Defaults to empty. - shouldExportRelatedTables : Used in conjunction with
tableNames
. If true, add related tables necessary for the export, such as interleaved parent tables and foreign keys tables. IftableNames
is specified but doesn't include related tables, this option must be set to true for a successful export. Defaults to: false. - spannerPriority : The request priority for Cloud Spanner calls. The value must be one of: [HIGH,MEDIUM,LOW].
- dataBoostEnabled : Use Spanner on-demand compute so the export job will run on independent compute resources and have no impact to current Spanner workloads. This will incur additional charges in Spanner. Defaults to: false.
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-export-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 Cloud Spanner to Avro Files on Cloud Storage 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/Cloud_Spanner_to_GCS_Avro \ --region REGION_NAME \ --staging-location GCS_STAGING_LOCATION \ --parameters \ instanceId=INSTANCE_ID,\ databaseId=DATABASE_ID,\ outputDir=GCS_DIRECTORY
Replace the following:
JOB_NAME
: a unique job name of your choiceFor the job to show in the Spanner portion of the Google Cloud console, the job name must match the format
cloud-spanner-export-INSTANCE_ID-DATABASE_ID
.VERSION
: 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
GCS_STAGING_LOCATION
: the path for writing temporary files; for example,gs://mybucket/temp
INSTANCE_ID
: your Spanner instance IDDATABASE_ID
: your Spanner database IDGCS_DIRECTORY
: the Cloud Storage path that the Avro files are exported to
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/Cloud_Spanner_to_GCS_Avro { "jobName": "JOB_NAME", "parameters": { "instanceId": "INSTANCE_ID", "databaseId": "DATABASE_ID", "outputDir": "gs://GCS_DIRECTORY" } }
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 choiceFor the job to show in the Spanner portion of the Google Cloud console, the job name must match the format
cloud-spanner-export-INSTANCE_ID-DATABASE_ID
.VERSION
: 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
GCS_STAGING_LOCATION
: the path for writing temporary files; for example,gs://mybucket/temp
INSTANCE_ID
: your Spanner instance IDDATABASE_ID
: your Spanner database IDGCS_DIRECTORY
: the Cloud Storage path that the Avro files are exported to
What's next
- Learn about Dataflow templates.
- See the list of Google-provided templates.