Vorlage „Cloud Storage zu JDBC“

Mit der Vorlage „Cloud Storage zu JDBC“ des Managed Service for Apache Spark können Sie Daten aus Cloud Storage in JDBC-Datenbanken extrahieren.

Vorlage verwenden

Führen Sie die Vorlage mit der gcloud CLI oder der Managed Service for Apache Spark API aus.

gcloud

Ersetzen Sie folgende Werte, bevor sie einen der Befehlsdaten verwenden:

  • PROJECT_ID: erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.
  • REGION: erforderlich. Compute Engine -Region.
  • SUBNET: optional. Wenn kein Subnetzwerk angegeben ist, wird das Subnetzwerk in der angegebenen REGION im default Netzwerk ausgewählt.

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

  • JDBC_CONNECTOR_CLOUD_STORAGE_PATH: erforderlich. Der vollständige Cloud Storage Pfad, einschließlich des Dateinamens, in dem die JDBC-Connector-JAR-Datei gespeichert ist. Mit den folgenden Befehlen können Sie JDBC-Connectors zum Hochladen in Cloud Storage herunterladen:
    • MySQL :
      wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.30.tar.gz
            
    • Postgres SQL :
      wget https://jdbc.postgresql.org/download/postgresql-42.2.6.jar
            
    • Microsoft SQL Server :
        
      wget https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/6.4.0.jre8/mssql-jdbc-6.4.0.jre8.jar
            
    • Oracle :
      wget https://repo1.maven.org/maven2/com/oracle/database/jdbc/ojdbc8/21.7.0.0/ojdbc8-21.7.0.0.jar
            
  • CLOUD_STORAGE_PATH: erforderlich. Cloud Storage Pfad, in dem Eingabedateien gespeichert sind.

    Beispiel:gs://templates/cloud_storage_to_jdbc_input

  • FORMAT: erforderlich. Ausgabedatenformat. Optionen: avro, parquet, csv oder orc. Standardeinstellung: avro. Hinweis: Wenn Sie avro verwenden, müssen Sie "file:///usr/lib/spark/connector/spark-avro.jar" dem jars gcloud CLI-Flag oder dem API-Feld hinzufügen.

    Beispiel (das Präfix file:// verweist auf eine Managed Service for Apache Spark-JAR-Datei):

    --jars=file:///usr/lib/spark/connector/spark-avro.jar, [, ... other jars]
  • MODE: optional. Schreibmodus für die Cloud Storage-Ausgabe. Optionen: Append, Overwrite, Ignore, oder ErrorIfExists. Standardeinstellung: ErrorIfExists.
  • Die folgenden Variablen werden verwendet, um die erforderliche JDBC_CONNECTION_URL:
    • JDBC_HOST
    • JDBC_PORT
    • JDBC_DATABASE oder für Oracle JDBC_SERVICE
    • JDBC_USERNAME
    • JDBC_PASSWORD

    Erstellen Sie die JDBC_CONNECTION_URL mit einem der folgenden connectorspezifischen Formate:

    • MySQL :
      jdbc:mysql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
              
    • Postgres SQL :
      jdbc:postgresql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
              
    • Microsoft SQL Server :
      jdbc:sqlserver://JDBC_HOST:JDBC_PORT;databaseName=JDBC_DATABASE;user=JDBC_USERNAME;password=JDBC_PASSWORD
              
    • Oracle :
      jdbc:oracle:thin:@//JDBC_HOST:JDBC_PORT/JDBC_SERVICE?user=JDBC_USERNAME&password=
              
  • JDBC_TABLE: erforderlich. Tabellenname, in den die Ausgabe geschrieben wird.
  • DRIVER: erforderlich. Der JDBC-Treiber, der für die Verbindung verwendet wird:
    • MySQL :
      com.mysql.cj.jdbc.Driver
              
    • Postgres SQL :
      org.postgresql.Driver
              
    • Microsoft SQL Server :
        
      com.microsoft.sqlserver.jdbc.SQLServerDriver
              
    • Oracle :
      oracle.jdbc.driver.OracleDriver
              
  • TEMPLATE_VERSION: erforderlich. Geben Sie latest für die neueste Vorlagenversion oder das Datum einer bestimmten Version an, z. B. 2023-03-17_v0.1.0-beta . Unter gs://templates-binaries oder mit dem Befehl gcloud storage ls gs://templates-binaries können Sie die verfügbaren Vorlagenversionen auflisten.
  • LOG_LEVEL: optional. Protokollierungsstufe. Mögliche Werte: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE oder WARN. Standardeinstellung: INFO.
  • NUM_PARTITIONS: optional. Die maximale Anzahl von Partitionen, die für die Parallelität von Tabellenschreibvorgängen verwendet werden können. Wenn angegeben, wird dieser Wert für die JDBC-Ausgabeverbindung verwendet. Standardmäßig werden die anfänglichen Partitionen verwendet, die von Spark read() festgelegt wurden.
  • BATCH_SIZE: optional. Anzahl der Datensätze, die pro Roundtrip eingefügt werden sollen. Standardeinstellung: 1000.
  • SERVICE_ACCOUNT: optional. Wenn nicht angegeben, wird das Compute Engine-Standarddienstkonto verwendet.
  • PROPERTY und PROPERTY_VALUE: optional. Durch Kommas getrennte Liste von Spark-Eigenschaft=value Paaren.
  • LABEL und LABEL_VALUE: optional. Durch Kommas getrennte Liste von Paaren im Format label=value.
  • KMS_KEY: optional. Der Cloud Key Management Service-Schlüssel, der für die Verschlüsselung verwendet werden soll. Wenn kein Schlüssel angegeben ist, werden inaktive Daten verschlüsselt mit einem Google-owned and Google-managed encryption key.

    Beispiel: projects/PROJECT_ID/regions/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME

Führen Sie folgenden Befehl aus:

Linux, macOS oder Cloud Shell

gcloud dataproc batches submit spark \
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate \
    --project="PROJECT_ID" \
    --region="REGION" \
    --version="1.2" \
    --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=GCSTOJDBC \
    --templateProperty project.id="PROJECT_ID" \
    --templateProperty log.level="LOG_LEVEL" \
    --templateProperty gcs.jdbc.input.location="CLOUD_STORAGE_PATH" \
    --templateProperty gcs.jdbc.input.format="FORMAT" \
    --templateProperty gcs.jdbc.output.saveMode="MODE" \
    --templateProperty gcs.jdbc.output.url="JDBC_CONNECTION_URL" \
    --templateProperty gcs.jdbc.output.table="JDBC_TABLE" \
    --templateProperty gcs.jdbc.output.driver="DRIVER" \
    --templateProperty gcs.jdbc.spark.partitions="NUM_PARTITIONS" \
    --templateProperty gcs.jdbc.output.batchInsertSize="BATCH_SIZE"

Windows (PowerShell)

gcloud dataproc batches submit spark `
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate `
    --project="PROJECT_ID" `
    --region="REGION" `
    --version="1.2" `
    --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=GCSTOJDBC `
    --templateProperty project.id="PROJECT_ID" `
    --templateProperty log.level="LOG_LEVEL" `
    --templateProperty gcs.jdbc.input.location="CLOUD_STORAGE_PATH" `
    --templateProperty gcs.jdbc.input.format="FORMAT" `
    --templateProperty gcs.jdbc.output.saveMode="MODE" `
    --templateProperty gcs.jdbc.output.url="JDBC_CONNECTION_URL" `
    --templateProperty gcs.jdbc.output.table="JDBC_TABLE" `
    --templateProperty gcs.jdbc.output.driver="DRIVER" `
    --templateProperty gcs.jdbc.spark.partitions="NUM_PARTITIONS" `
    --templateProperty gcs.jdbc.output.batchInsertSize="BATCH_SIZE"

Windows (cmd.exe)

gcloud dataproc batches submit spark ^
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ^
    --project="PROJECT_ID" ^
    --region="REGION" ^
    --version="1.2" ^
    --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=GCSTOJDBC ^
    --templateProperty project.id="PROJECT_ID" ^
    --templateProperty log.level="LOG_LEVEL" ^
    --templateProperty gcs.jdbc.input.location="CLOUD_STORAGE_PATH" ^
    --templateProperty gcs.jdbc.input.format="FORMAT" ^
    --templateProperty gcs.jdbc.output.saveMode="MODE" ^
    --templateProperty gcs.jdbc.output.url="JDBC_CONNECTION_URL" ^
    --templateProperty gcs.jdbc.output.table="JDBC_TABLE" ^
    --templateProperty gcs.jdbc.output.driver="DRIVER" ^
    --templateProperty gcs.jdbc.spark.partitions="NUM_PARTITIONS" ^
    --templateProperty gcs.jdbc.output.batchInsertSize="BATCH_SIZE"

REST

Ersetzen Sie diese Werte in den folgenden Anfragedaten:

  • PROJECT_ID: erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.
  • REGION: erforderlich. Compute Engine -Region.
  • SUBNET: optional. Wenn kein Subnetzwerk angegeben ist, wird das Subnetzwerk in der angegebenen REGION im default Netzwerk ausgewählt.

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

  • JDBC_CONNECTOR_CLOUD_STORAGE_PATH: erforderlich. Der vollständige Cloud Storage Pfad, einschließlich des Dateinamens, in dem die JDBC-Connector-JAR-Datei gespeichert ist. Mit den folgenden Befehlen können Sie JDBC-Connectors zum Hochladen in Cloud Storage herunterladen:
    • MySQL :
      wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.30.tar.gz
            
    • Postgres SQL :
      wget https://jdbc.postgresql.org/download/postgresql-42.2.6.jar
            
    • Microsoft SQL Server :
        
      wget https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/6.4.0.jre8/mssql-jdbc-6.4.0.jre8.jar
            
    • Oracle :
      wget https://repo1.maven.org/maven2/com/oracle/database/jdbc/ojdbc8/21.7.0.0/ojdbc8-21.7.0.0.jar
            
  • CLOUD_STORAGE_PATH: erforderlich. Cloud Storage Pfad, in dem Eingabedateien gespeichert sind.

    Beispiel:gs://templates/cloud_storage_to_jdbc_input

  • FORMAT: erforderlich. Ausgabedatenformat. Optionen: avro, parquet, csv oder orc. Standardeinstellung: avro. Hinweis: Wenn Sie avro verwenden, müssen Sie "file:///usr/lib/spark/connector/spark-avro.jar" dem jars gcloud CLI-Flag oder dem API-Feld hinzufügen.

    Beispiel (das Präfix file:// verweist auf eine Managed Service for Apache Spark-JAR-Datei):

    --jars=file:///usr/lib/spark/connector/spark-avro.jar, [, ... other jars]
  • MODE: optional. Schreibmodus für die Cloud Storage-Ausgabe. Optionen: Append, Overwrite, Ignore, oder ErrorIfExists. Standardeinstellung: ErrorIfExists.
  • Die folgenden Variablen werden verwendet, um die erforderliche JDBC_CONNECTION_URL:
    • JDBC_HOST
    • JDBC_PORT
    • JDBC_DATABASE oder für Oracle JDBC_SERVICE
    • JDBC_USERNAME
    • JDBC_PASSWORD

    Erstellen Sie die JDBC_CONNECTION_URL mit einem der folgenden connectorspezifischen Formate:

    • MySQL :
      jdbc:mysql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
              
    • Postgres SQL :
      jdbc:postgresql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
              
    • Microsoft SQL Server :
      jdbc:sqlserver://JDBC_HOST:JDBC_PORT;databaseName=JDBC_DATABASE;user=JDBC_USERNAME;password=JDBC_PASSWORD
              
    • Oracle :
      jdbc:oracle:thin:@//JDBC_HOST:JDBC_PORT/JDBC_SERVICE?user=JDBC_USERNAME&password=
              
  • JDBC_TABLE: erforderlich. Tabellenname, in den die Ausgabe geschrieben wird.
  • DRIVER: erforderlich. Der JDBC-Treiber, der für die Verbindung verwendet wird:
    • MySQL :
      com.mysql.cj.jdbc.Driver
              
    • Postgres SQL :
      org.postgresql.Driver
              
    • Microsoft SQL Server :
        
      com.microsoft.sqlserver.jdbc.SQLServerDriver
              
    • Oracle :
      oracle.jdbc.driver.OracleDriver
              
  • TEMPLATE_VERSION: erforderlich. Geben Sie latest für die neueste Vorlagenversion oder das Datum einer bestimmten Version an, z. B. 2023-03-17_v0.1.0-beta . Unter gs://templates-binaries oder mit dem Befehl gcloud storage ls gs://templates-binaries können Sie die verfügbaren Vorlagenversionen auflisten.
  • LOG_LEVEL: optional. Protokollierungsstufe. Mögliche Werte: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE oder WARN. Standardeinstellung: INFO.
  • NUM_PARTITIONS: optional. Die maximale Anzahl von Partitionen, die für die Parallelität von Tabellenschreibvorgängen verwendet werden können. Wenn angegeben, wird dieser Wert für die JDBC-Ausgabeverbindung verwendet. Standardmäßig werden die anfänglichen Partitionen verwendet, die von Spark read() festgelegt wurden.
  • BATCH_SIZE: optional. Anzahl der Datensätze, die pro Roundtrip eingefügt werden sollen. Standardeinstellung: 1000.
  • SERVICE_ACCOUNT: optional. Wenn nicht angegeben, wird das Compute Engine-Standarddienstkonto verwendet.
  • PROPERTY und PROPERTY_VALUE: optional. Durch Kommas getrennte Liste von Spark-Eigenschaft=value Paaren.
  • LABEL und LABEL_VALUE: optional. Durch Kommas getrennte Liste von Paaren im Format label=value.
  • KMS_KEY: optional. Der Cloud Key Management Service-Schlüssel, der für die Verschlüsselung verwendet werden soll. Wenn kein Schlüssel angegeben ist, werden inaktive Daten verschlüsselt mit einem Google-owned and Google-managed encryption key.

    Beispiel: projects/PROJECT_ID/regions/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME

HTTP-Methode und URL:

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

JSON-Text anfordern:


{
  "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=GCSTOJDBC",
      "--templateProperty","project.id=PROJECT_ID",
      "--templateProperty","log.level=LOG_LEVEL",
      "--templateProperty","gcs.jdbc.input.location=CLOUD_STORAGE_PATH",
      "--templateProperty","gcs.jdbc.input.format=FORMAT",
      "--templateProperty","gcs.jdbc.output.saveMode=MODE",
      "--templateProperty","gcs.jdbc.output.url=JDBC_CONNECTION_URL",
      "--templateProperty","gcs.jdbc.output.table=JDBC_TABLE",
      "--templateProperty","gcs.jdbc.output.driver=DRIVER",
      "--templateProperty","gcs.jdbc.spark.partitions=NUM_PARTITIONS",
      "--templateProperty","gcs.jdbc.output.batchInsertSize=BATCH_SIZE"
    ],
    "jarFileUris": [
      "gs://templates-binaries/TEMPLATE_VERSION/java/templates.jar", "JDBC_CONNECTOR_CLOUD_STORAGE_PATH"
    ]
  }
}

Wenn Sie die Anfrage senden möchten, maximieren Sie eine der folgenden Optionen:

Sie sollten eine JSON-Antwort ähnlich wie diese erhalten:


{
  "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"
  }
}