JDBC to Cloud Spanner template

Use the Managed Service for Apache Spark JDBC to Spanner template to extract data from JDBC databases to Spanner.

This template supports the following databases as input:

Use the template

Run the template using the gcloud CLI or Managed Service for Apache Spark API.

gcloud

Before using any of the command data below, make the following replacements:

  • PROJECT_ID: Required. Your Google Cloud project ID listed in the IAM Settings.
  • REGION: Required. Compute Engine region.
  • TEMPLATE_VERSION: Required. Specify latest for the latest template version, or the date of a specific version, for example, 2023-03-17_v0.1.0-beta (visit gs://templates-binaries or run gcloud storage ls gs://templates-binaries to list available template versions).
  • SUBNET: Optional. If a subnet is not specified, the subnet in the specified REGION in the default network is selected.

    Example: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME

  • JDBC_CONNECTOR_CLOUD_STORAGE_PATH: Required. The full Cloud Storage path, including the filename, where the JDBC connector jar is stored. You can use the following commands to download JDBC connectors for uploading to Cloud Storage:
  • The following variables are used to construct the required JDBC_CONNECTION_URL:

    Execute the following command:

    Linux, macOS, or Cloud Shell

    gcloud dataproc batches submit spark \
        --class=com.google.cloud.dataproc.templates.main.DataProcTemplate \
        --version="1.2" \
        --project="PROJECT_ID" \
        --region="REGION" \
        --jars="gs://templates-binaries/TEMPLATE_VERSION/java/templates.jar,JDBC_CONNECTOR_CLOUD_STORAGE_PATH" \
        --subnet="SUBNET" \
        --kms-key="KMS_KEY" \
        --service-account="SERVICE_ACCOUNT" \
        --properties="PROPERTY=PROPERTY_VALUE" \
        --labels="LABEL=LABEL_VALUE" \
        -- --template=JDBCTOSPANNER \
        --templateProperty log.level="LOG_LEVEL" \
        --templateProperty project.id="PROJECT_ID" \
        --templateProperty jdbctospanner.jdbc.url="JDBC_CONNECTION_URL" \
        --templateProperty jdbctospanner.jdbc.driver.class.name="DRIVER" \
        --templateProperty jdbctospanner.jdbc.fetchsize="FETCHSIZE" \
        --templateProperty jdbctospanner.jdbc.sessioninitstatement="JDBC_SESSION_INIT" \
        --templateProperty jdbctospanner.sql="QUERY" \
        --templateProperty jdbctospanner.sql.file="QUERY_FILE" \
        --templateProperty jdbctospanner.sql.numPartitions="NUM_PARTITIONS" \
        --templateProperty jdbctospanner.sql.partitionColumn="INPUT_PARTITION_COLUMN" \
        --templateProperty jdbctospanner.sql.lowerBound="LOWERBOUND" \
        --templateProperty jdbctospanner.sql.upperBound="UPPERBOUND" \
        --templateProperty jdbctospanner.output.instance="INSTANCE" \
        --templateProperty jdbctospanner.output.database="SPANNER_DATABASE" \
        --templateProperty jdbctospanner.output.table="TABLE" \
        --templateProperty jdbctospanner.output.saveMode="MODE" \
        --templateProperty jdbctospanner.output.primaryKey="PRIMARY_KEY" \
        --templateProperty jdbctospanner.output.batch.size="BATCHSIZE" \
        --templateProperty jdbctospanner.temp.table="TEMPVIEW" \
        --templateProperty jdbctospanner.temp.query="SQL_QUERY" \
        --templateProperty spanner.jdbc.dialect="SPANNER_JDBC_DIALECT" 

    Windows (PowerShell)

    gcloud dataproc batches submit spark `
        --class=com.google.cloud.dataproc.templates.main.DataProcTemplate `
        --version="1.2" `
        --project="PROJECT_ID" `
        --region="REGION" `
        --jars="gs://templates-binaries/TEMPLATE_VERSION/java/templates.jar,JDBC_CONNECTOR_CLOUD_STORAGE_PATH" `
        --subnet="SUBNET" `
        --kms-key="KMS_KEY" `
        --service-account="SERVICE_ACCOUNT" `
        --properties="PROPERTY=PROPERTY_VALUE" `
        --labels="LABEL=LABEL_VALUE" `
        -- --template=JDBCTOSPANNER `
        --templateProperty log.level="LOG_LEVEL" `
        --templateProperty project.id="PROJECT_ID" `
        --templateProperty jdbctospanner.jdbc.url="JDBC_CONNECTION_URL" `
        --templateProperty jdbctospanner.jdbc.driver.class.name="DRIVER" `
        --templateProperty jdbctospanner.jdbc.fetchsize="FETCHSIZE" `
        --templateProperty jdbctospanner.jdbc.sessioninitstatement="JDBC_SESSION_INIT" `
        --templateProperty jdbctospanner.sql="QUERY" `
        --templateProperty jdbctospanner.sql.file="QUERY_FILE" `
        --templateProperty jdbctospanner.sql.numPartitions="NUM_PARTITIONS" `
        --templateProperty jdbctospanner.sql.partitionColumn="INPUT_PARTITION_COLUMN" `
        --templateProperty jdbctospanner.sql.lowerBound="LOWERBOUND" `
        --templateProperty jdbctospanner.sql.upperBound="UPPERBOUND" `
        --templateProperty jdbctospanner.output.instance="INSTANCE" `
        --templateProperty jdbctospanner.output.database="SPANNER_DATABASE" `
        --templateProperty jdbctospanner.output.table="TABLE" `
        --templateProperty jdbctospanner.output.saveMode="MODE" `
        --templateProperty jdbctospanner.output.primaryKey="PRIMARY_KEY" `
        --templateProperty jdbctospanner.output.batch.size="BATCHSIZE" `
        --templateProperty jdbctospanner.temp.table="TEMPVIEW" `
        --templateProperty jdbctospanner.temp.query="SQL_QUERY" `
        --templateProperty spanner.jdbc.dialect="SPANNER_JDBC_DIALECT" 

    Windows (cmd.exe)

    gcloud dataproc batches submit spark ^
        --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ^
        --version="1.2" ^
        --project="PROJECT_ID" ^
        --region="REGION" ^
        --jars="gs://templates-binaries/TEMPLATE_VERSION/java/templates.jar,JDBC_CONNECTOR_CLOUD_STORAGE_PATH" ^
        --subnet="SUBNET" ^
        --kms-key="KMS_KEY" ^
        --service-account="SERVICE_ACCOUNT" ^
        --properties="PROPERTY=PROPERTY_VALUE" ^
        --labels="LABEL=LABEL_VALUE" ^
        -- --template=JDBCTOSPANNER ^
        --templateProperty log.level="LOG_LEVEL" ^
        --templateProperty project.id="PROJECT_ID" ^
        --templateProperty jdbctospanner.jdbc.url="JDBC_CONNECTION_URL" ^
        --templateProperty jdbctospanner.jdbc.driver.class.name="DRIVER" ^
        --templateProperty jdbctospanner.jdbc.fetchsize="FETCHSIZE" ^
        --templateProperty jdbctospanner.jdbc.sessioninitstatement="JDBC_SESSION_INIT" ^
        --templateProperty jdbctospanner.sql="QUERY" ^
        --templateProperty jdbctospanner.sql.file="QUERY_FILE" ^
        --templateProperty jdbctospanner.sql.numPartitions="NUM_PARTITIONS" ^
        --templateProperty jdbctospanner.sql.partitionColumn="INPUT_PARTITION_COLUMN" ^
        --templateProperty jdbctospanner.sql.lowerBound="LOWERBOUND" ^
        --templateProperty jdbctospanner.sql.upperBound="UPPERBOUND" ^
        --templateProperty jdbctospanner.output.instance="INSTANCE" ^
        --templateProperty jdbctospanner.output.database="SPANNER_DATABASE" ^
        --templateProperty jdbctospanner.output.table="TABLE" ^
        --templateProperty jdbctospanner.output.saveMode="MODE" ^
        --templateProperty jdbctospanner.output.primaryKey="PRIMARY_KEY" ^
        --templateProperty jdbctospanner.output.batch.size="BATCHSIZE" ^
        --templateProperty jdbctospanner.temp.table="TEMPVIEW" ^
        --templateProperty jdbctospanner.temp.query="SQL_QUERY" ^
        --templateProperty spanner.jdbc.dialect="SPANNER_JDBC_DIALECT" 

    REST

    Before using any of the request data, make the following replacements:

  • PROJECT_ID: Required. Your Google Cloud project ID listed in the IAM Settings.
  • REGION: Required. Compute Engine region.
  • TEMPLATE_VERSION: Required. Specify latest for the latest template version, or the date of a specific version, for example, 2023-03-17_v0.1.0-beta (visit gs://templates-binaries or run gcloud storage ls gs://templates-binaries to list available template versions).
  • SUBNET: Optional. If a subnet is not specified, the subnet in the specified REGION in the default network is selected.

    Example: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME

  • JDBC_CONNECTOR_CLOUD_STORAGE_PATH: Required. The full Cloud Storage path, including the filename, where the JDBC connector jar is stored. You can use the following commands to download JDBC connectors for uploading to Cloud Storage:
  • The following variables are used to construct the required JDBC_CONNECTION_URL:

    HTTP method and URL:

    POST https://dataproc.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/batches

    Request JSON body:

    
    {
      "environmentConfig": {
        "executionConfig": {
          "subnetworkUri": "SUBNET",
          "kmsKey": "KMS_KEY",
          "serviceAccount": "SERVICE_ACCOUNT"
        }
      },
      "labels": {
        "LABEL": "LABEL_VALUE"
      },
      "runtimeConfig": {
        "version": "1.2",
        "properties": {
          "PROPERTY": "PROPERTY_VALUE"
        }
      },
      "sparkBatch": {
        "mainClass": "com.google.cloud.dataproc.templates.main.DataProcTemplate",
        "args": [
          "--template","JDBCTOSPANNER",
          "--templateProperty","log.level=LOG_LEVEL",
          "--templateProperty","project.id=PROJECT_ID",
          "--templateProperty","jdbctospanner.jdbc.url=JDBC_CONNECTION_URL",
          "--templateProperty","jdbctospanner.jdbc.driver.class.name=DRIVER",
          "--templateProperty","jdbctospanner.jdbc.fetchsize=FETCHSIZE",
          "--templateProperty","jdbctospanner.jdbc.sessioninitstatement=JDBC_SESSION_INIT",
          "--templateProperty","jdbctospanner.sql=QUERY",
          "--templateProperty","jdbctospanner.sql.file=QUERY_FILE",
          "--templateProperty","jdbctospanner.sql.numPartitions=NUM_PARTITIONS",
          "--templateProperty","jdbctospanner.sql.partitionColumn=INPUT_PARTITION_COLUMN",
          "--templateProperty","jdbctospanner.sql.lowerBound=LOWERBOUND",
          "--templateProperty","jdbctospanner.sql.upperBound=UPPERBOUND",
          "--templateProperty","jdbctospanner.output.instance=INSTANCE",
          "--templateProperty","jdbctospanner.output.database=SPANNER_DATABASE",
          "--templateProperty","jdbctospanner.output.table=TABLE",
          "--templateProperty","jdbctospanner.output.saveMode=MODE",
          "--templateProperty","jdbctospanner.output.primaryKey=PRIMARY_KEY",
          "--templateProperty","jdbctospanner.output.batch.size=BATCHSIZE",
          "--templateProperty","jdbctospanner.temp.table=TEMPVIEW",
          "--templateProperty","jdbctospanner.temp.query=SQL_QUERY",
          "--templateProperty spanner.jdbc.dialect=SPANNER_JDBC_DIALECT"
        ],
        "jarFileUris": [
          "gs://templates-binaries/TEMPLATE_VERSION/java/templates.jar"
        ]
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

    
    {
      "name": "projects/PROJECT_ID/regions/REGION/operations/OPERATION_ID",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.dataproc.v1.BatchOperationMetadata",
        "batch": "projects/PROJECT_ID/locations/REGION/batches/BATCH_ID",
        "batchUuid": "de8af8d4-3599-4a7c-915c-798201ed1583",
        "createTime": "2023-02-24T03:31:03.440329Z",
        "operationType": "BATCH",
        "description": "Batch"
      }
    }