Spanner to Cloud Storage Avro template

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 Spanner database that you want to export.
  • databaseId: The database ID of the Spanner database that you want to export.
  • outputDir: The Cloud Storage path to export Avro files to. The export job creates a new directory under this path that contains the exported files. For example, gs://your-bucket/your-path.

Optional parameters

  • avroTempDirectory: The Cloud Storage path where temporary Avro files are written.
  • 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.
  • snapshotTime: The timestamp that corresponds to the version of the Spanner database that you want to read. The timestamp must be specified by using RFC 3339 UTC Zulu format. The timestamp must be in the past, and maximum timestamp staleness applies. For example, 1990-12-31T23:59:60Z. Defaults to empty.
  • spannerProjectId: The ID of the Google Cloud project that contains the Spanner database that you want to read data from.
  • shouldExportTimestampAsLogicalType: If true, timestamps are exported as a long type with timestamp-micros logical type. By default, this parameter is set to false and timestamps are exported as ISO-8601 strings at nanosecond precision.
  • tableNames: A comma-separated list of tables specifying the subset of the Spanner database to export. If you set this parameter, you must either include all of the related tables (parent tables and foreign key referenced tables) or set the shouldExportRelatedTables parameter to true.If the table is in named schema, please use fully qualified name. For example: sch1.foo in which sch1 is the schema name and foo is the table name. Defaults to empty.
  • shouldExportRelatedTables: Whether to include related tables. This parameter is used in conjunction with the tableNames parameter. Defaults to: false.
  • spannerPriority: The request priority for Spanner calls. Possible values are HIGH, MEDIUM, and LOW. The default value is MEDIUM.
  • dataBoostEnabled: Set to true to use the compute resources of Spanner Data Boost to run the job with near-zero impact on Spanner OLTP workflows. When set to true, you also need the spanner.databases.useDataBoost IAM permission. For more information, see the Data Boost overview (https://cloud.google.com/spanner/docs/databoost/databoost-overview). Defaults to: false.

Run the template

Console

  1. Go to the Dataflow Create job from template page.
  2. Go to Create job from template
  3. 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 ID
    • SPANNER_DATABASE_NAME: your Spanner database's name
  4. 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.

  5. From the Dataflow template drop-down menu, select the Cloud Spanner to Avro Files on Cloud Storage template.
  6. In the provided parameter fields, enter your parameter values.
  7. 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 choice

    For 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 use

    You can use the following values:

  • 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 ID
  • DATABASE_ID: your Spanner database ID
  • GCS_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 job
  • JOB_NAME: a unique job name of your choice

    For 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 use

    You can use the following values:

  • 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 ID
  • DATABASE_ID: your Spanner database ID
  • GCS_DIRECTORY: the Cloud Storage path that the Avro files are exported to

What's next